Class: SchwarmCli::Config
- Inherits:
-
Object
- Object
- SchwarmCli::Config
- Defined in:
- lib/schwarm_cli/config.rb
Overview
Loads CLI configuration from multiple sources with priority:
-
Explicit constructor arguments (highest)
-
Environment variables (SCHWARM_URL, SCHWARM_TOKEN)
-
Config file (~/.config/schwarm.json)
Constant Summary collapse
- DEFAULT_PATH =
File.("~/.config/schwarm.json")
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url: nil, api_key: nil, config_path: DEFAULT_PATH) ⇒ Config
constructor
A new instance of Config.
- #save ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(url: nil, api_key: nil, config_path: DEFAULT_PATH) ⇒ Config
Returns a new instance of Config.
16 17 18 19 20 21 |
# File 'lib/schwarm_cli/config.rb', line 16 def initialize(url: nil, api_key: nil, config_path: DEFAULT_PATH) @config_path = config_path file_config = load_file @url = strip_slash(url || ENV["SCHWARM_URL"] || file_config["url"]) @api_key = api_key || ENV["SCHWARM_TOKEN"] || file_config["api_key"] end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/schwarm_cli/config.rb', line 14 def api_key @api_key end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
14 15 16 |
# File 'lib/schwarm_cli/config.rb', line 14 def config_path @config_path end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
14 15 16 |
# File 'lib/schwarm_cli/config.rb', line 14 def url @url end |
Instance Method Details
#save ⇒ Object
27 28 29 30 |
# File 'lib/schwarm_cli/config.rb', line 27 def save FileUtils.mkdir_p(File.dirname(@config_path)) File.write(@config_path, JSON.pretty_generate(url: @url, api_key: @api_key)) end |
#valid? ⇒ Boolean
23 24 25 |
# File 'lib/schwarm_cli/config.rb', line 23 def valid? !@url.nil? && !@url.empty? && !@api_key.nil? && !@api_key.empty? end |