Class: CommitGpt::ConfigManager
- Inherits:
-
Object
- Object
- CommitGpt::ConfigManager
- Defined in:
- lib/commitgpt/config_manager.rb
Overview
Manages configuration files for CommitGPT
Constant Summary collapse
- DEFAULT_MODEL_FETCH_TIMEOUT =
Seconds to wait when fetching a provider's model list
30- COMMIT_DETAILS =
How much the commit message says: a subject line only, or a subject followed by an explanatory body
%w[concise detailed].freeze
- DEFAULT_COMMIT_DETAIL =
'concise'
Class Method Summary collapse
-
.config_dir ⇒ Object
Get the config directory path.
-
.config_exists? ⇒ Boolean
Check if config files exist.
-
.configured_providers ⇒ Object
Get list of configured providers (with API keys).
-
.ensure_config_dir ⇒ Object
Ensure config directory exists.
-
.generate_default_configs ⇒ Object
Generate default configuration files.
-
.get_active_provider_config ⇒ Object
Get active provider configuration.
-
.get_commit_detail ⇒ Object
Get commit detail level ('concise' or 'detailed').
-
.get_commit_format ⇒ Object
Get commit format configuration.
-
.get_model_fetch_timeout ⇒ Object
Get the model list fetch timeout (seconds).
-
.load_config ⇒ Object
Load and merge configuration files.
-
.local_config_path ⇒ Object
Get local config file path.
-
.main_config_path ⇒ Object
Get main config file path.
-
.save_local_config(config) ⇒ Object
Save local config.
-
.save_main_config(config) ⇒ Object
Save main config.
-
.set_active_provider(provider_name) ⇒ Object
Set active provider.
-
.set_commit_detail(detail) ⇒ Object
Set commit detail level.
-
.set_commit_format(format) ⇒ Object
Set commit format configuration.
-
.set_model_fetch_timeout(seconds) ⇒ Object
Set the model list fetch timeout (seconds).
-
.update_provider(provider_name, main_attrs = {}, local_attrs = {}) ⇒ Object
Update provider configuration.
Class Method Details
.config_dir ⇒ Object
Get the config directory path
21 22 23 |
# File 'lib/commitgpt/config_manager.rb', line 21 def config_dir File.('~/.config/commitgpt') end |
.config_exists? ⇒ Boolean
Check if config files exist
41 42 43 |
# File 'lib/commitgpt/config_manager.rb', line 41 def config_exists? File.exist?(main_config_path) end |
.configured_providers ⇒ Object
Get list of configured providers (with API keys)
118 119 120 121 122 123 124 |
# File 'lib/commitgpt/config_manager.rb', line 118 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_dir ⇒ Object
Ensure config directory exists
36 37 38 |
# File 'lib/commitgpt/config_manager.rb', line 36 def ensure_config_dir FileUtils.mkdir_p(config_dir) end |
.generate_default_configs ⇒ Object
Generate default configuration files
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 |
# File 'lib/commitgpt/config_manager.rb', line 79 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_config ⇒ Object
Get active provider configuration
56 57 58 59 60 61 62 63 64 |
# File 'lib/commitgpt/config_manager.rb', line 56 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_detail ⇒ Object
Get commit detail level ('concise' or 'detailed')
177 178 179 180 181 182 183 |
# File 'lib/commitgpt/config_manager.rb', line 177 def get_commit_detail return DEFAULT_COMMIT_DETAIL unless config_exists? main_config = YAML.load_file(main_config_path) detail = main_config['commit_detail'] COMMIT_DETAILS.include?(detail) ? detail : DEFAULT_COMMIT_DETAIL end |
.get_commit_format ⇒ Object
Get commit format configuration
161 162 163 164 165 166 |
# File 'lib/commitgpt/config_manager.rb', line 161 def get_commit_format return 'simple' unless config_exists? main_config = YAML.load_file(main_config_path) main_config['commit_format'] || 'simple' end |
.get_model_fetch_timeout ⇒ Object
Get the model list fetch timeout (seconds)
194 195 196 197 198 199 200 |
# File 'lib/commitgpt/config_manager.rb', line 194 def get_model_fetch_timeout return DEFAULT_MODEL_FETCH_TIMEOUT unless config_exists? main_config = YAML.load_file(main_config_path) timeout = main_config['model_fetch_timeout'].to_i timeout.positive? ? timeout : DEFAULT_MODEL_FETCH_TIMEOUT end |
.load_config ⇒ Object
Load and merge configuration files
46 47 48 49 50 51 52 53 |
# File 'lib/commitgpt/config_manager.rb', line 46 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_path ⇒ Object
Get local config file path
31 32 33 |
# File 'lib/commitgpt/config_manager.rb', line 31 def local_config_path File.join(config_dir, 'config.local.yml') end |
.main_config_path ⇒ Object
Get main config file path
26 27 28 |
# File 'lib/commitgpt/config_manager.rb', line 26 def main_config_path File.join(config_dir, 'config.yml') end |
.save_local_config(config) ⇒ Object
Save local config
73 74 75 76 |
# File 'lib/commitgpt/config_manager.rb', line 73 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
67 68 69 70 |
# File 'lib/commitgpt/config_manager.rb', line 67 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
154 155 156 157 158 |
# File 'lib/commitgpt/config_manager.rb', line 154 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_detail(detail) ⇒ Object
Set commit detail level
186 187 188 189 190 191 |
# File 'lib/commitgpt/config_manager.rb', line 186 def set_commit_detail(detail) ensure_config_dir main_config = config_exists? ? YAML.load_file(main_config_path) : { 'providers' => [], 'active_provider' => '' } main_config['commit_detail'] = detail save_main_config(main_config) end |
.set_commit_format(format) ⇒ Object
Set commit format configuration
169 170 171 172 173 174 |
# File 'lib/commitgpt/config_manager.rb', line 169 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 |
.set_model_fetch_timeout(seconds) ⇒ Object
Set the model list fetch timeout (seconds)
203 204 205 206 207 208 |
# File 'lib/commitgpt/config_manager.rb', line 203 def set_model_fetch_timeout(seconds) ensure_config_dir main_config = config_exists? ? YAML.load_file(main_config_path) : { 'providers' => [], 'active_provider' => '' } main_config['model_fetch_timeout'] = seconds save_main_config(main_config) end |
.update_provider(provider_name, main_attrs = {}, local_attrs = {}) ⇒ Object
Update provider configuration
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 |
# File 'lib/commitgpt/config_manager.rb', line 127 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 } if provider provider.merge!(main_attrs) else # Preset added after this config file was generated preset = PROVIDER_PRESETS.find { |p| p[:value] == provider_name } defaults = { 'name' => provider_name, 'model' => '', 'diff_len' => 32_768 } defaults['base_url'] = preset[:base_url] if preset main_config['providers'] << defaults.merge(main_attrs) end 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 |