Module: AwsReporting::Command::Config

Defined in:
lib/aws-reporting/command/config.rb

Constant Summary collapse

DEFAULT_PATH =
'~/.aws-reporting/config.yaml'
MESSAGE_ALREADY_EXIST =
" already exists. If you want to overwrite this, use '-f' option."

Class Method Summary collapse

Class Method Details

.run(opts, args) ⇒ 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
34
35
# File 'lib/aws-reporting/command/config.rb', line 7

def run(opts, args)
  Signal.trap(:INT){
    puts
    puts "interrupted."
    exit
  }

  help = opts['h']
  if help
    puts opts.help
    return
  end

  begin
    access = opts['access-key-id']
    secret = opts['secret-access-key']
    force = opts['f']

    if access and secret
      run_batch(access, secret, force)
    elsif !!!access and !!!secret
      run_interactive(force)
    else
      raise CommandArgumentError.new
    end
  rescue AwsReporting::Error::CommandArgumentError
    puts opts.help
  end
end

.run_batch(access, secret, force) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/aws-reporting/command/config.rb', line 37

def run_batch(access, secret, force)
  path = File.expand_path(DEFAULT_PATH)
  if force or !File.exist?(path)
    update_config(access, secret, path)
    puts 'done.'
  else
    puts path + MESSAGE_ALREADY_EXIST
  end
end

.run_interactive(force) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aws-reporting/command/config.rb', line 47

def run_interactive(force)
  path = File.expand_path(DEFAULT_PATH)
  if force or !File.exist?(path)
    print 'Access Key ID    :'
    access = $stdin.gets.chomp
    print 'Secret Access Key:'
    secret = $stdin.gets.chomp

    update_config(access, secret, path)
    puts 'done.'
  else
    puts path + MESSAGE_ALREADY_EXIST
  end
end

.update_config(access, secret, path) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/aws-reporting/command/config.rb', line 62

def update_config(access, secret, path)
  yaml = YAML.dump(:access_key_id => access, :secret_access_key => secret)

  FileUtils.mkdir_p(File.dirname(path))

  open(path, 'w'){|f|
    f.print yaml
  }
end