Class: S3sec::Commands::Config

Inherits:
S3sec::Command show all
Defined in:
lib/s3sec/commands/config.rb

Instance Method Summary collapse

Methods inherited from S3sec::Command

#command, #config, #cursor, #editor, #exec_exist?, #generator, #pager, #pastel, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



9
10
11
# File 'lib/s3sec/commands/config.rb', line 9

def initialize(options)
  @options = options
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/s3sec/commands/config.rb', line 13

def execute(input: $stdin, output: $stdout)
  id, key = nil, nil

  id = config.fetch(:AWS_ACCESS_KEY_ID) if config.exist?
  key = config.fetch(:AWS_SECRET_ACCESS_KEY) if config.exist?

  result = prompt.collect do
    key(:AWS_ACCESS_KEY_ID).ask("AWS_ACCESS_KEY_ID #{id}:", default: ENV['AWS_ACCESS_KEY_ID'])
    key(:AWS_SECRET_ACCESS_KEY).ask("AWS_SECRET_ACCESS_KEY #{key}:", default: ENV['AWS_SECRET_ACCESS_KEY'])
  end

  config.set(:AWS_ACCESS_KEY_ID, value: result[:AWS_ACCESS_KEY_ID]) if !result[:AWS_ACCESS_KEY_ID].nil?
  config.set(:AWS_SECRET_ACCESS_KEY, value: result[:AWS_SECRET_ACCESS_KEY]) if !result[:AWS_SECRET_ACCESS_KEY].nil?

  client = Aws::S3::Client.new(
   credentials: Aws::Credentials.new(config.fetch(:AWS_ACCESS_KEY_ID), config.fetch(:AWS_SECRET_ACCESS_KEY)),
   region: 'eu-central-1'
  )

  begin
    output.puts 'Testing ...'
    client.list_buckets
    config.write(force: true)
    output.puts pastel.green('Done!')
  rescue => e
    output.puts pastel.red(e)
  end
end