Class: EasyAI::Command::Backup::Codex

Inherits:
Backup
  • Object
show all
Defined in:
lib/easyai/command/backup/codex.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#auth_sourceObject

注意:路径用方法动态 expand_path,避免常量在加载期冻结真实 HOME, 让 spec 切换 ENV 后定位失败(同 LocalConfig.config_path 的处理)



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

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

#backup_dirObject



37
# File 'lib/easyai/command/backup/codex.rb', line 37

def backup_dir    = File.expand_path('~/.easyai/backup')

#backup_has_official_token?Boolean

备份文件 .codex.json 中是否含 ChatGPT 官方 OAuth token, 供 easyai codex 运行时决定是否提供 ChatGPT Subscribe 选项。

Returns:

  • (Boolean)


85
86
87
88
89
90
91
# File 'lib/easyai/command/backup/codex.rb', line 85

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

  oauth_token?(read_json(backup_path)[AUTH_KEY])
rescue StandardError
  false
end

#backup_if_absentObject

供 easyai codex 运行前调用:仅当备份不存在、源文件存在且含官方 token 时自动备份一次。 返回是否执行了备份;任何异常降级为 warning,绝不阻塞启动。



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/easyai/command/backup/codex.rb', line 68

def backup_if_absent
  return false if File.exist?(backup_path)
  return false unless File.exist?(auth_source)
  # keyring 模式下凭证不在文件里,自动备份会一路触发 help! → 每次启动刷屏;此处静默跳过
  return false if File.exist?(config_source) && keyring_credentials_store?(File.read(config_source))
  # 只备份真正的 ChatGPT 官方登录态;EasyAI 给 longcat/deepseek 写的纯 API Key auth.json 不在此列
  return false unless official_token_present?

  perform_backup
  true
rescue StandardError => e
  warn "  ⚠ 自动备份 Codex 登录信息失败(#{e.message}),继续启动"
  false
end

#backup_if_officialObject

供 easyai setup 选择 ChatGPT Subscribe 时复用:仅当存在官方 token 才更新备份, 效果等同 easyai backup codex。返回是否实际执行了备份。



59
60
61
62
63
64
# File 'lib/easyai/command/backup/codex.rb', line 59

def backup_if_official
  return false unless official_token_present?

  perform_backup
  true
end

#backup_pathObject



38
# File 'lib/easyai/command/backup/codex.rb', line 38

def backup_path   = File.join(backup_dir, '.codex.json')

#config_sourceObject



36
# File 'lib/easyai/command/backup/codex.rb', line 36

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

#runObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/easyai/command/backup/codex.rb', line 46

def run
  ensure_file_credentials_supported!
  unless official_token_present?
    puts '⚠ 未在 ~/.codex/auth.json 检测到 ChatGPT 官方登录 token(OAuth),跳过备份。'
    puts '  如需备份,请先运行 codex 完成 ChatGPT 订阅账户登录后重试。'
    return
  end

  perform_backup
end

#validate!Object



40
41
42
43
44
# File 'lib/easyai/command/backup/codex.rb', line 40

def validate!
  super
  ensure_file_credentials_supported!
  help! "源文件不存在: #{auth_source}\n请先登录 Codex 后再备份。" unless File.exist?(auth_source)
end