Class: EasyAI::Command::Utils::Export
- Inherits:
-
EasyAI::Command::Utils
- Object
- CLAide::Command
- EasyAI::Command
- EasyAI::Command::Utils
- EasyAI::Command::Utils::Export
- Defined in:
- lib/easyai/command/utils/export.rb
Constant Summary
Constants inherited from EasyAI::Command
DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS
Instance Attribute Summary
Attributes inherited from EasyAI::Command
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(argv) ⇒ Export
constructor
A new instance of Export.
- #run ⇒ Object
Methods inherited from EasyAI::Command
Constructor Details
#initialize(argv) ⇒ Export
Returns a new instance of Export.
43 44 45 46 47 |
# File 'lib/easyai/command/utils/export.rb', line 43 def initialize(argv) @output_file = argv.option('output') @verbose = argv.flag?('verbose') super end |
Class Method Details
.options ⇒ Object
36 37 38 39 40 41 |
# File 'lib/easyai/command/utils/export.rb', line 36 def self. [ ['--output=FILE', '指定输出文件名'], ['--verbose', '显示详细信息'] ].concat(super) end |
Instance Method Details
#run ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/easyai/command/utils/export.rb', line 49 def run puts "正在导出 AI 配置信息..." puts "" if @verbose # 收集配置信息 export_config = {} # 1. 读取 ~/.claude.json user_claude_file = File.("~/.claude.json") if File.exist?(user_claude_file) print_status("📄 读取配置", user_claude_file) if @verbose begin user_config = JSON.parse(File.read(user_claude_file)) # 排除 projects 字段 claude_config = user_config.reject { |key, _| key == "projects" } # 添加到导出配置 export_config["claude_json"] = claude_config print_success("读取 ~/.claude.json 成功") if @verbose # 如果未指定输出文件名,尝试从配置中获取邮箱地址 if @output_file.nil? && claude_config["oauthAccount"] email = claude_config["oauthAccount"]["emailAddress"] if email && !email.empty? # 使用邮箱地址作为文件名(替换特殊字符) safe_filename = email.gsub(/[@.]/, '_') @output_file = "#{safe_filename}.json" puts " 使用邮箱生成文件名: #{@output_file}" if @verbose end end rescue JSON::ParserError => e print_error("解析 ~/.claude.json 失败: #{e.}") exit 1 rescue => e print_error("读取 ~/.claude.json 失败: #{e.}") exit 1 end else print_warning("未找到 ~/.claude.json 文件") end # 2. 从 Keychain 读取凭证(macOS) if Base::SystemInfo.macos? print_status("🔐 读取 Keychain", "Claude Code-credentials") if @verbose keychain_data = read_keychain_credentials if keychain_data export_config["key_chain"] = { "service_name" => "Claude Code-credentials", "claudeAiOauth" => keychain_data } print_success("读取 Keychain 凭证成功") if @verbose else print_warning("未找到 Keychain 凭证(可选)") if @verbose end else puts " ⚠️ 非 macOS 系统,跳过 Keychain 读取" if @verbose end # 3. 从环境变量读取令牌(始终包含 env 字段) token_value = ENV['CLAUDE_CODE_OAUTH_TOKEN'] || "" export_config["env"] = { "CLAUDE_CODE_OAUTH_TOKEN" => token_value } if token_value && !token_value.empty? print_status("🔑 读取环境变量", "CLAUDE_CODE_OAUTH_TOKEN") if @verbose print_success("读取环境变量成功") if @verbose else print_warning("CLAUDE_CODE_OAUTH_TOKEN 为空(将导出空值)") if @verbose end # 4. 读取代理配置(始终包含 claude_proxy 字段) proxy_config = {} # 检查 HTTP_PROXY 相关环境变量 http_proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] || "" proxy_config['HTTP_PROXY'] = http_proxy # 检查 HTTPS_PROXY 相关环境变量 https_proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || "" proxy_config['HTTPS_PROXY'] = https_proxy # 始终添加 claude_proxy 字段 export_config["claude_proxy"] = proxy_config # 显示状态信息 if @verbose has_http = !http_proxy.empty? has_https = !https_proxy.empty? if has_http || has_https print_status("🌐 读取代理配置", "环境变量") print_success("读取代理配置成功") puts " HTTP_PROXY: #{has_http ? mask_url(http_proxy) : '(空)'}" puts " HTTPS_PROXY: #{has_https ? mask_url(https_proxy) : '(空)'}" else print_warning("代理配置为空(将导出空值)") end end # 检查是否收集到任何配置 if export_config.empty? print_error("未找到任何配置信息可导出") puts " 请确保已配置 Claude 或运行 'easyai --setup'" exit 1 end # 设置默认输出文件名 @output_file ||= "claude_setting.json" # 写入文件 puts "" if @verbose print_status("💾 写入文件", @output_file) begin # 美化 JSON 输出 json_output = JSON.pretty_generate(export_config) # 写入文件 File.write(@output_file, json_output) print_success("配置成功导出到 #{@output_file}") # 显示导出内容摘要 if @verbose puts "\n导出内容摘要:" puts " ✓ claude.json 配置" if export_config["claude_json"] puts " ✓ Keychain 凭证" if export_config["key_chain"] puts " ✓ 环境变量令牌" if export_config["env"] puts " ✓ 代理配置" if export_config["claude_proxy"] end rescue => e print_error("写入文件失败: #{e.}") exit 1 end end |