Class: EasyAI::ConfigManager
- Inherits:
-
Object
- Object
- EasyAI::ConfigManager
- Defined in:
- lib/easyai/config/config.rb
Constant Summary collapse
- REPO_URL =
"https://gitee.com/goodtools/EasyAISetting.git"- EASYAI_DIR =
File.('~/.easyai')
- CONFIG_REPO_DIR =
File.join(EASYAI_DIR, 'EasyAISetting')
- @@no_keychain =
类变量初始化
false- @@verbose =
false- @@tool_type =
nil
Class Method Summary collapse
-
.download_config_repo ⇒ Object
下载配置仓库到固定位置.
- .download_user_config(user_name = nil, options = {}) ⇒ Object
-
.download_user_config_temp(user_name = nil, options = {}) ⇒ Object
原有的临时下载方式(作为备用方案).
-
.load_from_local_repo(user_name = nil, options = {}) ⇒ Object
从本地配置仓库加载配置.
-
.manage_config_repo ⇒ Object
管理配置仓库:如果存在则更新,不存在则下载.
-
.update_config_repo ⇒ Object
更新现有的配置仓库.
Class Method Details
.download_config_repo ⇒ Object
下载配置仓库到固定位置
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/easyai/config/config.rb', line 69 def self.download_config_repo puts "正在下载配置仓库到 #{CONFIG_REPO_DIR}..." if @@verbose begin # 克隆仓库到固定位置,捕获错误信息 stdout, stderr, status = Open3.capture3("git clone --quiet #{REPO_URL} #{CONFIG_REPO_DIR}") unless status.success? puts "✗ 配置仓库下载失败" puts " 错误信息: #{stderr.chomp}" if stderr && !stderr.empty? puts " 请检查网络连接、git 安装状态" return false end puts "✓ 配置仓库下载成功" return true rescue => e puts "✗ 下载配置仓库失败: #{e.}" return false end end |
.download_user_config(user_name = nil, options = {}) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/easyai/config/config.rb', line 187 def self.download_user_config(user_name = nil, = {}) # 优先尝试使用本地配置仓库 config = load_from_local_repo(user_name, ) # 如果用户取消了授权,返回特殊标识 if config == :user_cancelled return :user_cancelled end return config if config # 如果本地仓库失败,回退到临时下载方式 puts "本地配置仓库不可用,使用临时下载..." download_user_config_temp(user_name, ) end |
.download_user_config_temp(user_name = nil, options = {}) ⇒ Object
原有的临时下载方式(作为备用方案)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/easyai/config/config.rb', line 204 def self.download_user_config_temp(user_name = nil, = {}) puts "正在获取配置文件..." # 设置全局选项 @@no_keychain = [:no_keychain] || false @@verbose = [:verbose] || false @@tool_type = [:tool_type] || nil begin # 创建临时目录 temp_dir = Dir.mktmpdir("EasyAISetting") # 克隆仓库 clone_cmd = "git clone --depth 1 --quiet #{REPO_URL} #{temp_dir}" success = system(clone_cmd, out: File::NULL, err: File::NULL) unless success puts "✗ 配置文件获取失败" puts " 请检查网络连接、git 安装,或使用本地配置文件" cleanup_temp_dir(temp_dir) return nil end puts "✓ 配置文件获取成功" # 检查 index.json 文件,支持加密版本 index_file = File.join(temp_dir, "index.json") encrypted_index_file = File.join(temp_dir, "index.json.encrypted") # 优先尝试加密的 index 文件 if File.exist?(encrypted_index_file) users = parse_encrypted_index_file(encrypted_index_file) elsif File.exist?(index_file) users = parse_index_file(index_file) else puts "未找到 index.json 或 index.json.encrypted,使用默认配置" config_file = File.join(temp_dir, "claude_setting.json") encrypted_config_file = File.join(temp_dir, "claude_setting.json.encrypted") if File.exist?(encrypted_config_file) config_content = load_encrypted_config_file(encrypted_config_file) elsif File.exist?(config_file) config_content = load_config_file(config_file) else puts "✗ 未找到配置文件" cleanup_temp_dir(temp_dir) return nil end cleanup_temp_dir(temp_dir) return config_content end return nil if users.nil? # 检查是否有可用用户(支持新旧格式) if is_users_empty?(users) puts "✗ index.json 中未找到用户" cleanup_temp_dir(temp_dir) return nil end # 获取用户名:优先使用提供的用户名,否则通过 JPS 登录获取 if user_name selected_user = user_name else # 统一使用 JPS 登录获取用户名 selected_user = get_username_from_jps(@@verbose) # 处理用户取消的情况 if selected_user == :user_cancelled puts "✗ 用户取消了授权登录" cleanup_temp_dir(temp_dir) return nil elsif selected_user.nil? || selected_user.strip.empty? puts "✗ JPS 登录失败,无法获取用户名" cleanup_temp_dir(temp_dir) return nil end end # 查找用户配置文件(传入工具类型) config_filename = find_user_config(users, selected_user.strip, @@tool_type) if config_filename.nil? puts "✗ 用户 '#{selected_user}' 未找到" # 不显示可用用户列表,保护配置信息 puts " 请联系管理员确认用户权限" cleanup_temp_dir(temp_dir) return nil end # 加载配置文件,支持加密版本(find_user_config 已确保包含 .json) config_file = File.join(temp_dir, config_filename) encrypted_config_file = File.join(temp_dir, "#{config_filename}.encrypted") puts "✓ 正在为 #{selected_user} 加载配置" # 优先尝试加密文件 if File.exist?(encrypted_config_file) config_content = load_encrypted_config_file(encrypted_config_file) elsif File.exist?(config_file) config_content = load_config_file(config_file) else puts "✗ 配置文件未找到: #{config_filename}" cleanup_temp_dir(temp_dir) return nil end cleanup_temp_dir(temp_dir) config_content rescue => e puts "✗ 配置获取失败: #{e.}" cleanup_temp_dir(temp_dir) if temp_dir nil end end |
.load_from_local_repo(user_name = nil, options = {}) ⇒ Object
从本地配置仓库加载配置
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 |
# File 'lib/easyai/config/config.rb', line 93 def self.load_from_local_repo(user_name = nil, = {}) # 首先管理配置仓库 return nil unless manage_config_repo # 设置全局选项 @@no_keychain = [:no_keychain] || false @@verbose = [:verbose] || false @@tool_type = [:tool_type] || nil puts "正在从本地配置仓库加载配置..." if @@verbose puts " 工具类型: #{@@tool_type || '自动'}" if @@verbose && @@tool_type begin # 检查 index.json 文件,支持加密版本 index_file = File.join(CONFIG_REPO_DIR, "index.json") encrypted_index_file = File.join(CONFIG_REPO_DIR, "index.json.encrypted") # 优先尝试加密的 index 文件 if File.exist?(encrypted_index_file) users = parse_encrypted_index_file(encrypted_index_file) elsif File.exist?(index_file) users = parse_index_file(index_file) else puts "未找到 index.json 或 index.json.encrypted,使用默认配置" config_file = File.join(CONFIG_REPO_DIR, "claude_setting.json") encrypted_config_file = File.join(CONFIG_REPO_DIR, "claude_setting.json.encrypted") if File.exist?(encrypted_config_file) return load_encrypted_config_file(encrypted_config_file) elsif File.exist?(config_file) return load_config_file(config_file) else puts "✗ 未找到配置文件" return nil end end return nil if users.nil? # 检查是否有可用用户(支持新旧格式) if is_users_empty?(users) puts "✗ index.json 中未找到用户" return nil end # 获取用户名:优先使用提供的用户名,否则通过 JPS 登录获取 if user_name selected_user = user_name else # 统一使用 JPS 登录获取用户名 selected_user = get_username_from_jps(@@verbose) # 处理用户取消的情况 if selected_user == :user_cancelled puts "✗ 用户取消了授权登录" return :user_cancelled elsif selected_user.nil? || selected_user.strip.empty? puts "✗ JPS 登录失败,无法获取用户名" return nil end end # 查找用户配置文件(传入工具类型) config_filename = find_user_config(users, selected_user.strip, @@tool_type) if config_filename.nil? puts "✗ 用户 '#{selected_user}' 未找到" # 不显示可用用户列表,保护配置信息 puts " 请联系管理员确认用户权限" return nil end # 加载配置文件,支持加密版本(find_user_config 已确保包含 .json) config_file = File.join(CONFIG_REPO_DIR, config_filename) encrypted_config_file = File.join(CONFIG_REPO_DIR, "#{config_filename}.encrypted") puts "✓ 正在为 #{selected_user} 加载配置" # 优先尝试加密文件 if File.exist?(encrypted_config_file) load_encrypted_config_file(encrypted_config_file) elsif File.exist?(config_file) load_config_file(config_file) else puts "✗ 配置文件未找到: #{config_filename}" return nil end rescue => e puts "✗ 加载本地配置失败: #{e.}" nil end end |
.manage_config_repo ⇒ Object
管理配置仓库:如果存在则更新,不存在则下载
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/easyai/config/config.rb', line 21 def self.manage_config_repo # 确保 ~/.easyai 目录存在 FileUtils.mkdir_p(EASYAI_DIR) unless Dir.exist?(EASYAI_DIR) if Dir.exist?(CONFIG_REPO_DIR) update_config_repo else download_config_repo end end |
.update_config_repo ⇒ Object
更新现有的配置仓库
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/easyai/config/config.rb', line 33 def self.update_config_repo begin # 检查是否是有效的 git 仓库 unless Dir.exist?(File.join(CONFIG_REPO_DIR, '.git')) FileUtils.rm_rf(CONFIG_REPO_DIR) return download_config_repo end # 切换到仓库目录并拉取更新 Dir.chdir(CONFIG_REPO_DIR) do # 重置本地更改 reset_success = system('git reset --hard HEAD', out: File::NULL, err: File::NULL) unless reset_success FileUtils.rm_rf(CONFIG_REPO_DIR) return download_config_repo end # 拉取最新更新,捕获错误信息 stdout, stderr, status = Open3.capture3('git pull origin master') unless status.success? puts "⚠️ 配置更新失败,使用缓存配置" if stderr && !stderr.empty? FileUtils.rm_rf(CONFIG_REPO_DIR) return download_config_repo end end return true rescue => e FileUtils.rm_rf(CONFIG_REPO_DIR) if Dir.exist?(CONFIG_REPO_DIR) return download_config_repo end end |