Class: EasyAI::Command::Restore::Codex

Inherits:
Restore
  • Object
show all
Defined in:
lib/easyai/command/restore/codex.rb

Constant Summary collapse

AUTH_KEY =
'auth'.freeze
CONFIG_KEY =
'config_toml'.freeze

Instance Method Summary collapse

Instance Method Details

#auth_targetObject

注意:路径用方法动态 expand_path,避免常量在加载期冻结真实 HOME



32
# File 'lib/easyai/command/restore/codex.rb', line 32

def auth_target   = File.expand_path('~/.codex/auth.json')

#backup_pathObject



35
# File 'lib/easyai/command/restore/codex.rb', line 35

def backup_path   = File.expand_path('~/.easyai/backup/.codex.json')

#codex_dirObject



34
# File 'lib/easyai/command/restore/codex.rb', line 34

def codex_dir     = File.expand_path('~/.codex')

#config_targetObject



33
# File 'lib/easyai/command/restore/codex.rb', line 33

def config_target = File.expand_path('~/.codex/config.toml')

#restore_auth_onlyObject

仅还原 auth.json(供 easyai codex 选择 ChatGPT Subscribe 时取回官方 token), 不触碰 config.toml。返回是否实际还原。异常由调用方处理。



56
57
58
59
60
61
62
63
64
65
# File 'lib/easyai/command/restore/codex.rb', line 56

def restore_auth_only
  return false unless File.exist?(backup_path)

  auth = read_json(backup_path)[AUTH_KEY]
  return false unless auth.is_a?(Hash) && !auth.empty?

  FileUtils.mkdir_p(codex_dir)
  restore_auth(auth)
  true
end

#runObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/easyai/command/restore/codex.rb', line 43

def run
  FileUtils.mkdir_p(codex_dir)
  backup = read_json(backup_path)
  ensure_file_credentials_supported!(backup)

  auth_summary = restore_auth(backup[AUTH_KEY])
  config_summary = restore_config(backup[CONFIG_KEY])

  report(auth_summary, config_summary)
end

#validate!Object



37
38
39
40
41
# File 'lib/easyai/command/restore/codex.rb', line 37

def validate!
  super
  help! "备份文件不存在: #{backup_path}\n请先运行 easyai backup codex 创建备份。" unless File.exist?(backup_path)
  ensure_file_credentials_supported!
end