Class: SchwarmCli::Config

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

Overview

Loads CLI configuration from multiple sources with priority:

  1. Explicit constructor arguments (highest)

  2. Environment variables (SCHWARM_URL, SCHWARM_TOKEN)

  3. Config file (~/.config/schwarm.json)

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("~/.config/schwarm.json")

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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_pathObject (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

#urlObject (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

#saveObject



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

Returns:

  • (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