Class: Tools::Default

Inherits:
Thor
  • Object
show all
Defined in:
lib/slicehost-tools/tools.rb

Direct Known Subclasses

DNS, Slice

Instance Method Summary collapse

Instance Method Details

#apikey(apikey = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/slicehost-tools/tools.rb', line 7

def apikey(apikey = nil)
  @write_file = false
  unless File.exists? File.join(ENV['HOME'], '.slicehost-tools')
    @write_file = true
  else
    # it does exist, ask.
    print "This will replace the stored key, are you sure? [y/N] "
    case STDIN.gets.chomp
    when /y/i
      @write_file = true      
    end
  end
  
  # ask for api key if none is given
  if apikey.nil? && @write_file
    puts "Please enter your API key since you did not provide one: "
    apikey = STDIN.gets.chomp
  end
  
  # write the api key to file
  if @write_file
    File.open( File.join(ENV['HOME'], '.slicehost-tools'), File::RDWR|File::TRUNC|File::CREAT, 0664 ) do |f|
      f.puts "SlicehostSecretKey = '#{apikey}'"
      f.close
    end
  end
end