Class: PI::Cli::Config

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

Constant Summary collapse

DEFAULT_TARGET =

DEFAULT_TARGET = ‘api.staging.samsungcloud.org’

'staging.samsungcloud.org'
DEFAULT_SUGGEST =
'samsungcloud.org'
TARGET_FILE =
'~/.pi_target'
TOKEN_FILE =
'~/.pi_token'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



92
93
94
# File 'lib/cli/config.rb', line 92

def initialize(work_dir = Dir.pwd)
  @work_dir = work_dir
end

Class Attribute Details

.colorizeObject

Returns the value of attribute colorize.



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

def colorize
  @colorize
end

.outputObject

Returns the value of attribute output.



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

def output
  @output
end

.suggest_urlObject (readonly)

Returns the value of attribute suggest_url.



21
22
23
# File 'lib/cli/config.rb', line 21

def suggest_url
  @suggest_url
end

.traceObject

Returns the value of attribute trace.



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

def trace
  @trace
end

Class Method Details

.all_tokensObject Also known as: targets



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

def all_tokens
  token_file = File.expand_path(TOKEN_FILE)
  return nil unless File.exists? token_file
  contents = lock_and_read(token_file).strip
  JSON.parse(contents)
end

.auth_tokenObject



54
55
56
57
58
# File 'lib/cli/config.rb', line 54

def auth_token
  return @token if @token
  tokens = all_tokens
  @token = tokens[target_url] if tokens
end

.lock_and_read(file) ⇒ Object



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

def lock_and_read(file)
  File.open(file, "r") {|f|
    f.flock(File::LOCK_EX)
    contents = f.read
    f.flock(File::LOCK_UN)
    contents
  }
end

.lock_and_write(file, contents) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/cli/config.rb', line 80

def lock_and_write(file, contents)
  File.open(file, File::RDWR | File::CREAT, 0600) {|f|
    f.flock(File::LOCK_EX)
    f.rewind
    f.puts contents
    f.flush
    f.truncate(f.pos)
    f.flock(File::LOCK_UN)
  }
end

.remove_token_fileObject



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

def remove_token_file
  FileUtils.rm_f(File.expand_path(TOKEN_FILE))
end

.store_target(target_host) ⇒ Object



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

def store_target(target_host)
  target_file = File.expand_path(TARGET_FILE)
  lock_and_write(target_file, target_host)
end

.store_token(token) ⇒ Object



64
65
66
67
68
69
# File 'lib/cli/config.rb', line 64

def store_token(token)
  tokens = all_tokens || {}
  tokens[target_url] = token
  token_file = File.expand_path(TOKEN_FILE)
  lock_and_write(token_file, tokens.to_json)
end

.target_urlObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cli/config.rb', line 23

def target_url
  target_file = File.expand_path(TARGET_FILE)
  if File.exists? target_file
    @target_url = lock_and_read(target_file).strip!
    ha = @target_url.split('//')
    ha.shift
    @suggest_url = ha.join('.')
    @suggest_url = DEFAULT_SUGGEST if @suggest_url.empty?
  else
    @target_url  = DEFAULT_TARGET
    @suggest_url = DEFAULT_SUGGEST
  end
  @target_url = "http://#{@target_url}" unless /^https?/ =~ @target_url
  @target_url = @target_url.gsub(/\/+$/, '')
  @target_url
end