Class: Civo::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/civo/cli/token.rb

Class Method Summary collapse

Class Method Details

.defaultObject



43
44
45
46
47
48
49
50
51
# File 'lib/civo/cli/token.rb', line 43

def self.default
  tokens = read_all
  token = tokens[tokens["**DEFAULT**"]]
  if token.nil?
    puts "No default token has been set.  Use 'civo tokens:save --name NAME --token TOKEN' to set one"
    exit 2
  end
  token
end

.read_allObject



4
5
6
7
# File 'lib/civo/cli/token.rb', line 4

def self.read_all
  config = Civo::Config.parse
  config["tokens"] || {}
end

.remove(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/civo/cli/token.rb', line 31

def self.remove(name)
  tokens = read_all
  tokens.delete(name)
  if tokens["**DEFAULT**"] == name
    tokens["**DEFAULT**"] = tokens.keys.first
    if tokens["**DEFAULT**"] == "**DEFAULT**"
      tokens.delete("**DEFAULT**")
    end
  end
  write_all(tokens)
end

.save(name, key) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/civo/cli/token.rb', line 15

def self.save(name, key)
  tokens = read_all
  tokens[name] = key
  if tokens["**DEFAULT**"] == nil
    tokens["**DEFAULT**"] = name
  end
  write_all(tokens)
end

.set_default(name) ⇒ Object



24
25
26
27
28
29
# File 'lib/civo/cli/token.rb', line 24

def self.set_default(name)
  puts "Setting default token to be #{name}"
  tokens = read_all
  tokens["**DEFAULT**"] = name
  write_all(tokens)
end

.write_all(tokens) ⇒ Object



9
10
11
12
13
# File 'lib/civo/cli/token.rb', line 9

def self.write_all(tokens)
  config = Civo::Config.parse
  config["tokens"] = tokens
  Civo::Config.update(config).save
end