Class: Hackpad::Cli::Config

Inherits:
OpenStruct
  • Object
show all
Includes:
Cliprompt
Defined in:
lib/hackpad/cli/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, input = STDIN, output = STDOUT) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hackpad/cli/config.rb', line 10

def initialize(options = nil, input = STDIN, output = STDOUT)
  super(options)
  @@input = input
  @@output = output
  self.configdir ||= File.join(ENV['HOME'], '.hackpad-cli')
  self.refresh ||= false
  self.urls ||= false
  self.output = output
  setio input, output
  patch_1
  addvalues 'config'
  self.workspace ||= 'default'
  self.workspacedir = File.join(configdir, workspace)
  addvalues 'workspace'
end

Instance Method Details

#addvalues(type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/hackpad/cli/config.rb', line 26

def addvalues(type)
  dir = send("#{type}dir".to_sym)
  file = File.join(dir, 'config.yml')
  FileUtils.mkdir_p dir unless Dir.exist? dir
  send("setup_#{type}".to_sym, file) unless File.exist? file
  YAML.load_file(file).each do |k, v|
    new_ostruct_member(k)
    send("#{k}=", v)
  end
end

#change_defaultObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/hackpad/cli/config.rb', line 63

def change_default
  values = {}
  values['use_colors'] = use_colors
  values['workspace'] = ask 'What workspace do you want to use as default from now on?',
    choices: workspaces.map(&:name),
    default: workspace,
    aslist: true
  file = File.join(configdir, 'config.yml')
  write(file, values)
end

#patch_1Object



74
75
76
77
78
# File 'lib/hackpad/cli/config.rb', line 74

def patch_1
  if File.exist? File.join(configdir, "#{workspace}.yml")
    FileUtils.mv File.join(configdir, "#{workspace}.yml"), File.join(configdir, workspace, 'config.yml')
  end
end

#setup_config(file) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/hackpad/cli/config.rb', line 45

def setup_config(file)
  values = {}
  output.puts Paint['Create a new hackpad-cli configuration:', :blue]
  values['use_colors'] = guess 'HPCLI_COLORS', 'Do you want a colored output?', 'Yn'
  values['workspace'] = guess 'HPCLI_WORKSPACE', 'What is the name of the default workspace?', 'default'
  write(file, values)
end

#setup_workspace(file) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/hackpad/cli/config.rb', line 53

def setup_workspace(file)
  values = {}
  output.puts Paint['Workspace configuration.', :blue]
  output.puts Paint['Gather your information from https://<workspace>.hackpad.com/ep/account/settings/', :bold]
  values['client_id'] = guess 'HPCLI_CLIENTID', 'What is your Client ID?'
  values['secret'] = guess 'HPCLI_SECRET', 'What is your Secret Key?'
  values['site'] = guess('HPCLI_URL', 'What is the URI of your workspace? (ex. https://xxx.hackapd.com)').gsub(/\/$/, '')
  write(file, values)
end

#workspacesObject



37
38
39
40
41
42
43
# File 'lib/hackpad/cli/config.rb', line 37

def workspaces
  w = Dir.glob(File.join(configdir, '*', 'config.yml')).reduce([]) do |a, path|
    a << OpenStruct.new(name: File.basename(File.dirname(path)), site: YAML.load_file(path)['site'])
    a
  end
  w.sort_by { |s| s.name }
end

#write(file, values) ⇒ Object



80
81
82
83
84
# File 'lib/hackpad/cli/config.rb', line 80

def write(file, values)
  File.open(file, 'w') do |f|
    f.write YAML.dump(values)
  end
end