Class: DiscourseCli::Commands::Config

Inherits:
Base
  • Object
show all
Defined in:
lib/discourse_cli/commands/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?

Class Method Details

.config_pathObject



9
10
11
# File 'lib/discourse_cli/commands/config.rb', line 9

def self.config_path
  DiscourseCli::Config.config_path
end

Instance Method Details

#listObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/discourse_cli/commands/config.rb', line 33

def list
  data = load_file
  sites = data["sites"] || {}
  default_site = data["default"]
  if sites.empty?
    puts "No sites configured. Run `dsc config set` to add one."
    return
  end
  sites.each do |name, cfg|
    marker = name == default_site ? " (default)" : ""
    puts "#{name}#{marker}"
    puts "  host:         #{cfg["host"]}"
    puts "  api_username: #{cfg["api_username"]}"
    puts
  end
end

#setObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/discourse_cli/commands/config.rb', line 18

def set
  data = load_file
  data["sites"] ||= {}
  data["default"] ||= options[:site]
  data["sites"][options[:site]] = {
    "host"         => options[:host],
    "api_key"      => options[:"api-key"],
    "api_username" => options[:"api-username"],
  }
  FileUtils.mkdir_p(File.dirname(self.class.config_path))
  File.write(self.class.config_path, YAML.dump(data))
  formatter.print_success("Saved config for site '#{options[:site]}'")
end