Class: EasyAI::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/easyai/config/config.rb

Constant Summary collapse

REPO_URL =
"https://gitee.com/goodtools/EasyAISetting.git"
EASYAI_DIR =
File.expand_path('~/.easyai')
CONFIG_REPO_DIR =
File.join(EASYAI_DIR, 'EasyAISetting')
@@no_keychain =

类变量初始化

false

Class Method Summary collapse

Class Method Details

.download_config_repoObject

下载配置仓库到固定位置



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/easyai/config/config.rb', line 67

def self.download_config_repo
  puts "正在下载配置仓库到 #{CONFIG_REPO_DIR}..."
  
  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.message}"
    return false
  end
end

.download_user_config(user_name = nil, options = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/easyai/config/config.rb', line 176

def self.download_user_config(user_name = nil, options = {})
  # 优先尝试使用本地配置仓库
  config = load_from_local_repo(user_name, options)
  return config if config
  
  # 如果本地仓库失败,回退到临时下载方式
  puts "本地配置仓库不可用,使用临时下载..."
  download_user_config_temp(user_name, options)
end

.download_user_config_temp(user_name = nil, options = {}) ⇒ Object

原有的临时下载方式(作为备用方案)



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
# File 'lib/easyai/config/config.rb', line 187

def self.download_user_config_temp(user_name = nil, options = {})
  puts "正在获取配置文件..."
  
  # 设置全局选项
  @@no_keychain = options[:no_keychain] || false
  
  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 users.empty?
      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
      if 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)
    
    if config_filename.nil?
      puts "✗ 用户 '#{selected_user}' 未找到"
      puts "  可用用户: #{users.keys.join(', ')}"
      cleanup_temp_dir(temp_dir)
      return nil
    end
    
    # 加载配置文件,支持加密版本
    config_filename += '.json' unless config_filename.end_with?('.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.message}"
    cleanup_temp_dir(temp_dir) if temp_dir
    nil
  end
end

.load_from_local_repo(user_name = nil, options = {}) ⇒ Object

从本地配置仓库加载配置



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
# File 'lib/easyai/config/config.rb', line 91

def self.load_from_local_repo(user_name = nil, options = {})
  # 首先管理配置仓库
  return nil unless manage_config_repo
  
  # 设置全局选项
  @@no_keychain = options[:no_keychain] || false
  
  puts "正在从本地配置仓库加载配置..."
  
  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 users.empty?
      puts "✗ index.json 中未找到用户"
      return nil
    end
    
    # 获取用户名:优先使用提供的用户名,否则通过 JPS 登录获取
    if user_name
      selected_user = user_name
    else
      # 统一使用 JPS 登录获取用户名
      selected_user = get_username_from_jps
      if selected_user.nil? || selected_user.strip.empty?
        puts "✗ JPS 登录失败,无法获取用户名"
        return nil
      end
    end
    
    # 查找用户配置文件
    config_filename = find_user_config(users, selected_user.strip)
    
    if config_filename.nil?
      puts "✗ 用户 '#{selected_user}' 未找到"
      puts "  可用用户: #{users.keys.join(', ')}"
      return nil
    end
    
    # 加载配置文件,支持加密版本
    config_filename += '.json' unless config_filename.end_with?('.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.message}"
    nil
  end
end

.manage_config_repoObject

管理配置仓库:如果存在则更新,不存在则下载



19
20
21
22
23
24
25
26
27
28
# File 'lib/easyai/config/config.rb', line 19

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_repoObject

更新现有的配置仓库



31
32
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
# File 'lib/easyai/config/config.rb', line 31

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