Class: Fp::Config
- Inherits:
-
Object
- Object
- Fp::Config
- Defined in:
- lib/fp/config.rb
Overview
Handles configuration: profiles, API keys, API URL.
API URL resolution priority (highest wins):
1. --api-url CLI flag (passed via constructor)
2. FP_API_URL env var
3. Profile-specific api_url (in profile config)
4. Global api_url setting (top-level in config.yml)
5. DEFAULT_API_URL
API key resolution priority:
1. FP_API_KEY env var
2. Profile api_key (--profile flag > FP_PROFILE env var)
Constant Summary collapse
- DEFAULT_API_URL =
'https://api.featureparity.dev'- CONFIG_DIR =
File.('~/.config/fp')
- CONFIG_FILE =
File.join(CONFIG_DIR, 'config.yml')
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#profile_name ⇒ Object
readonly
Returns the value of attribute profile_name.
Class Method Summary collapse
-
.get_setting(key) ⇒ Object
Get a global setting (top-level key in config.yml).
- .list_profiles ⇒ Object
- .load_config ⇒ Object
- .load_profiles ⇒ Object
- .profile_exists?(name) ⇒ Boolean
- .save_profile(name, api_key:, api_url: nil) ⇒ Object
-
.set_setting(key, value) ⇒ Object
Set a global setting (top-level key in config.yml).
Instance Method Summary collapse
-
#initialize(profile: nil, api_url: nil) ⇒ Config
constructor
A new instance of Config.
- #valid? ⇒ Boolean
- #validation_error ⇒ Object
Constructor Details
#initialize(profile: nil, api_url: nil) ⇒ Config
Returns a new instance of Config.
26 27 28 29 30 31 |
# File 'lib/fp/config.rb', line 26 def initialize(profile: nil, api_url: nil) @profile_name = resolve_profile_name(profile) @explicit_api_url = api_url @api_key = resolve_api_key @api_url = resolve_api_url end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
24 25 26 |
# File 'lib/fp/config.rb', line 24 def api_key @api_key end |
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
24 25 26 |
# File 'lib/fp/config.rb', line 24 def api_url @api_url end |
#profile_name ⇒ Object (readonly)
Returns the value of attribute profile_name.
24 25 26 |
# File 'lib/fp/config.rb', line 24 def profile_name @profile_name end |
Class Method Details
.get_setting(key) ⇒ Object
Get a global setting (top-level key in config.yml)
74 75 76 77 |
# File 'lib/fp/config.rb', line 74 def get_setting(key) config = load_config config[key.to_s] end |
.list_profiles ⇒ Object
94 95 96 |
# File 'lib/fp/config.rb', line 94 def list_profiles load_profiles.keys end |
.load_config ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/fp/config.rb', line 47 def load_config return {} unless File.exist?(CONFIG_FILE) YAML.safe_load_file(CONFIG_FILE, permitted_classes: [Symbol]) || {} rescue StandardError => e warn "Warning: Could not load config file: #{e.}" {} end |
.load_profiles ⇒ Object
56 57 58 59 |
# File 'lib/fp/config.rb', line 56 def load_profiles config = load_config config['profiles'] || {} end |
.profile_exists?(name) ⇒ Boolean
98 99 100 |
# File 'lib/fp/config.rb', line 98 def profile_exists?(name) load_profiles.key?(name) end |
.save_profile(name, api_key:, api_url: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fp/config.rb', line 61 def save_profile(name, api_key:, api_url: nil) FileUtils.mkdir_p(CONFIG_DIR) ensure_config_file! config = load_config config['profiles'] ||= {} config['profiles'][name] = { 'api_key' => api_key } config['profiles'][name]['api_url'] = api_url if api_url write_config!(config) end |
.set_setting(key, value) ⇒ Object
Set a global setting (top-level key in config.yml)
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fp/config.rb', line 80 def set_setting(key, value) FileUtils.mkdir_p(CONFIG_DIR) ensure_config_file! config = load_config if value.nil? config.delete(key.to_s) else config[key.to_s] = value end write_config!(config) end |
Instance Method Details
#valid? ⇒ Boolean
33 34 35 |
# File 'lib/fp/config.rb', line 33 def valid? !@api_key.nil? && !@api_key.empty? end |
#validation_error ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/fp/config.rb', line 37 def validation_error return nil if valid? <<~MSG No API key found. Set FP_API_KEY environment variable or add a profile: fp profile add <name> --api-key fp_... MSG end |