Class: Smartdc::CliConfigure

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ CliConfigure

Returns a new instance of CliConfigure.



5
6
7
8
9
10
11
12
13
14
# File 'lib/smartdc/cli_configure.rb', line 5

def initialize(path=nil)
  if path
    @path =path
  else
    [Smartdc.root, ENV['HOME']].each do |path|
      @path = File.join(path, '.sdccfg')
      break if File.exist?(@path)
    end
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#initObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smartdc/cli_configure.rb', line 41

def init
  output = <<__EOS__
            .
            |
   .-.  .--. .-.|  .-.
  : + : `--.(   | (
   `-'  `--' `-'`- `-'
    Smart Data Center Command Line Interface
    https://apidocs.joyent.com/sdcapidoc/cloudapi/
__EOS__
  puts output
  puts

  options = self.read
  options = option(options, :Hostname, :hostname, 'api.example.com')
  options = option(options, :Version, :version, '~7.0')
  options = option(options, :Username, :username, ENV['USER'])
  options = option(options, :Fingerprint, :use_key, 'none')
  options = option(options, :SSLVerify, :ssl_verify, true)

  puts
  puts "New settings!"
  puts "Hostname: #{options[:hostname]}"
  puts "Version: #{options[:version]}"
  puts "Username: #{options[:username]}"
  puts "Fingerprint: #{options[:use_key]}"
  puts "SSLVerify: #{options[:ssl_verify]}"
  puts

  self.write options
end

#readObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smartdc/cli_configure.rb', line 16

def read
  begin
    options = {}
    File.open(path, 'r') do |file|
      file.each do |row|
        row.chomp!
        cols = row.split(/\s+=\s+/)
        options[cols[0].to_sym] = cols[1]
      end
    end
    options
  rescue
    {}
  end
end

#write(options) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/smartdc/cli_configure.rb', line 32

def write(options)
  File.open(path, 'w') do |file|
    options.each do |key, value|
      file.puts "#{key} = #{value}"
    end
  end
  options
end