Class: CommitGpt::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/commitgpt/config_manager.rb

Overview

Manages configuration files for CommitGPT

Class Method Summary collapse

Class Method Details

.config_dirObject

Get the config directory path



13
14
15
# File 'lib/commitgpt/config_manager.rb', line 13

def config_dir
  File.expand_path('~/.config/commitgpt')
end

.config_exists?Boolean

Check if config files exist



33
34
35
# File 'lib/commitgpt/config_manager.rb', line 33

def config_exists?
  File.exist?(main_config_path)
end

.configured_providersObject

Get list of configured providers (with API keys)



110
111
112
113
114
115
116
# File 'lib/commitgpt/config_manager.rb', line 110

def configured_providers
  config = load_config
  return [] if config.nil?

  providers = config['providers'] || []
  providers.select { |p| p['api_key'] && !p['api_key'].empty? }
end

.ensure_config_dirObject

Ensure config directory exists



28
29
30
# File 'lib/commitgpt/config_manager.rb', line 28

def ensure_config_dir
  FileUtils.mkdir_p(config_dir)
end

.generate_default_configsObject

Generate default configuration files



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
# File 'lib/commitgpt/config_manager.rb', line 71

def generate_default_configs
  ensure_config_dir

  # Generate main config with all providers but empty models
  providers = PROVIDER_PRESETS.map do |preset|
    {
      'name' => preset[:value],
      'model' => '',
      'diff_len' => 32_768,
      'base_url' => preset[:base_url]
    }
  end

  main_config = {
    'providers' => providers,
    'active_provider' => ''
  }

  # Generate local config with empty API keys
  local_providers = PROVIDER_PRESETS.map do |preset|
    {
      'name' => preset[:value],
      'api_key' => ''
    }
  end

  local_config = {
    'providers' => local_providers
  }

  save_main_config(main_config)
  save_local_config(local_config)

  # Remind user to add config.local.yml to .gitignore
  puts '✔ Generated default configuration files.'.green
  puts '⚠ Remember to add ~/.config/commitgpt/config.local.yml to your .gitignore'.yellow
end

.get_active_provider_configObject

Get active provider configuration



48
49
50
51
52
53
54
55
56
# File 'lib/commitgpt/config_manager.rb', line 48

def get_active_provider_config
  config = load_config
  return nil if config.nil? || config['active_provider'].nil?

  active_provider = config['active_provider']
  providers = config['providers'] || []

  providers.find { |p| p['name'] == active_provider }
end

.get_commit_formatObject

Get commit format configuration



145
146
147
148
149
150
# File 'lib/commitgpt/config_manager.rb', line 145

def get_commit_format
  return 'simple' unless config_exists?

  main_config = YAML.load_file(main_config_path)
  main_config['commit_format'] || 'simple'
end

.load_configObject

Load and merge configuration files



38
39
40
41
42
43
44
45
# File 'lib/commitgpt/config_manager.rb', line 38

def load_config
  return nil unless config_exists?

  main_config = YAML.load_file(main_config_path) || {}
  local_config = File.exist?(local_config_path) ? YAML.load_file(local_config_path) : {}

  merge_configs(main_config, local_config)
end

.local_config_pathObject

Get local config file path



23
24
25
# File 'lib/commitgpt/config_manager.rb', line 23

def local_config_path
  File.join(config_dir, 'config.local.yml')
end

.main_config_pathObject

Get main config file path



18
19
20
# File 'lib/commitgpt/config_manager.rb', line 18

def main_config_path
  File.join(config_dir, 'config.yml')
end

.save_local_config(config) ⇒ Object

Save local config



65
66
67
68
# File 'lib/commitgpt/config_manager.rb', line 65

def save_local_config(config)
  ensure_config_dir
  File.write(local_config_path, config.to_yaml)
end

.save_main_config(config) ⇒ Object

Save main config



59
60
61
62
# File 'lib/commitgpt/config_manager.rb', line 59

def save_main_config(config)
  ensure_config_dir
  File.write(main_config_path, config.to_yaml)
end

.set_active_provider(provider_name) ⇒ Object

Set active provider



138
139
140
141
142
# File 'lib/commitgpt/config_manager.rb', line 138

def set_active_provider(provider_name)
  main_config = YAML.load_file(main_config_path)
  main_config['active_provider'] = provider_name
  save_main_config(main_config)
end

.set_commit_format(format) ⇒ Object

Set commit format configuration



153
154
155
156
157
158
# File 'lib/commitgpt/config_manager.rb', line 153

def set_commit_format(format)
  ensure_config_dir
  main_config = config_exists? ? YAML.load_file(main_config_path) : { 'providers' => [], 'active_provider' => '' }
  main_config['commit_format'] = format
  save_main_config(main_config)
end

.update_provider(provider_name, main_attrs = {}, local_attrs = {}) ⇒ Object

Update provider configuration



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/commitgpt/config_manager.rb', line 119

def update_provider(provider_name, main_attrs = {}, local_attrs = {})
  # Update main config
  main_config = YAML.load_file(main_config_path)
  provider = main_config['providers'].find { |p| p['name'] == provider_name }
  provider&.merge!(main_attrs)
  save_main_config(main_config)

  # Update local config
  local_config = File.exist?(local_config_path) ? YAML.load_file(local_config_path) : { 'providers' => [] }
  local_provider = local_config['providers'].find { |p| p['name'] == provider_name }
  if local_provider
    local_provider.merge!(local_attrs)
  else
    local_config['providers'] << { 'name' => provider_name }.merge(local_attrs)
  end
  save_local_config(local_config)
end