Class: CivoCLI::Config

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

Class Method Summary collapse

Class Method Details

.currentObject



51
52
53
54
55
# File 'lib/config.rb', line 51

def self.current
  @config ||= JSON.parse(File.read(filename))
rescue
  @config = {"apikeys" => {}, "meta" => {"current_apikey" => nil, "default_region" => "lon1", "latest_release_check" => "2017-06-01T15:35:42+01:00", "url" => "https://api.civo.com"}}
end

.delete_apikey(key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/config.rb', line 28

def self.delete_apikey(key)
  set_current_apikey_name(key)
  current["apikeys"].delete(key)
  if get_current_apikey_name == key
    if get_apikeys.any?
      set_current_apikey_name(get_apikeys.keys.first)
    else
      set_current_apikey_name(nil)
    end
  end
end

.filenameObject



65
66
67
# File 'lib/config.rb', line 65

def self.filename
  "#{ENV['HOME']}/.civo.json"
end

.get_apikey(key) ⇒ Object



7
8
9
# File 'lib/config.rb', line 7

def self.get_apikey(key)
  current["apikeys"][key]
end

.get_apikeysObject



3
4
5
# File 'lib/config.rb', line 3

def self.get_apikeys
  current["apikeys"]
end

.get_current_apikeyObject



11
12
13
# File 'lib/config.rb', line 11

def self.get_current_apikey
  current["apikeys"][get_current_apikey_name]
end

.get_current_apikey_nameObject



15
16
17
# File 'lib/config.rb', line 15

def self.get_current_apikey_name
  get_meta(:current_apikey)
end

.get_meta(key) ⇒ Object



40
41
42
43
44
# File 'lib/config.rb', line 40

def self.get_meta(key)
  current["meta"].transform_keys{ |key| key.to_sym rescue key }[key]
rescue
  nil
end

.resetObject



57
58
59
# File 'lib/config.rb', line 57

def self.reset
  @config = nil
end

.saveObject



61
62
63
# File 'lib/config.rb', line 61

def self.save
  File.write(filename, @config.to_json)
end

.set_api_authObject



69
70
71
72
73
74
75
76
77
# File 'lib/config.rb', line 69

def self.set_api_auth
  ENV["CIVO_API_VERSION"] = "2"
  ENV["CIVO_TOKEN"] = CivoCLI::Config.get_current_apikey
  if ENV["CIVO_TOKEN"].nil? || ENV["CIVO_TOKEN"] == ""
    puts "#{"Unable to connect:".colorize(:red)} No valid API key is set"
    exit 1
  end
  ENV["CIVO_URL"] = CivoCLI::Config.get_meta(:url) || "https://api.civo.com"
end

.set_apikey(key, value) ⇒ Object



23
24
25
26
# File 'lib/config.rb', line 23

def self.set_apikey(key, value)
  current["apikeys"][key] = value
  save
end

.set_current_apikey_name(name) ⇒ Object



19
20
21
# File 'lib/config.rb', line 19

def self.set_current_apikey_name(name)
  set_meta(:current_apikey, name)
end

.set_meta(key, value) ⇒ Object



46
47
48
49
# File 'lib/config.rb', line 46

def self.set_meta(key, value)
  current["meta"][key.to_s] = value
  save
end