Module: Formatron::CLI::Generators::Credentials

Defined in:
lib/formatron/cli/generators/credentials.rb

Overview

CLI command for credentials generator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_credentials(directory) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/formatron/cli/generators/credentials.rb', line 21

def self.default_credentials(directory)
  local = local_credentials directory
  if File.file?(local)
    local
  else
    global_credentials
  end
end

.default_generated_credentials(directory) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/formatron/cli/generators/credentials.rb', line 30

def self.default_generated_credentials(directory)
  if File.file?(File.join(directory, 'Formatronfile'))
    local_credentials(directory)
  else
    global_credentials
  end
end

.dot_credentialsObject



9
10
11
# File 'lib/formatron/cli/generators/credentials.rb', line 9

def self.dot_credentials
  File.join Formatron::WORKING_DIRECTORY, 'credentials.json'
end

.global_credentialsObject



13
14
15
# File 'lib/formatron/cli/generators/credentials.rb', line 13

def self.global_credentials
  File.join Dir.home, dot_credentials
end

.local_credentials(directory) ⇒ Object



17
18
19
# File 'lib/formatron/cli/generators/credentials.rb', line 17

def self.local_credentials(directory)
  File.join directory, dot_credentials
end

Instance Method Details

#credentials_access_key_id(options) ⇒ Object



69
70
71
# File 'lib/formatron/cli/generators/credentials.rb', line 69

def credentials_access_key_id(options)
  options.access_key_id || ask('Access Key ID? ')
end

#credentials_action(c) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/formatron/cli/generators/credentials.rb', line 77

def credentials_action(c)
  c.action do |_args, options|
    Formatron::Generators::Credentials.generate(
      credentials_credentials(options),
      credentials_region(options),
      credentials_access_key_id(options),
      credentials_secret_access_key(options)
    )
  end
end

#credentials_credentials(options) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/formatron/cli/generators/credentials.rb', line 52

def credentials_credentials(options)
  options.credentials ||
    ask('Credentials file? ') do |q|
      q.default =
        Credentials.default_generated_credentials(
          credentials_directory(options)
        )
    end
end

#credentials_directory(options) ⇒ Object



48
49
50
# File 'lib/formatron/cli/generators/credentials.rb', line 48

def credentials_directory(options)
  options.directory || Dir.pwd
end

#credentials_formatron_commandObject



88
89
90
91
92
93
94
95
96
# File 'lib/formatron/cli/generators/credentials.rb', line 88

def credentials_formatron_command
  command :'generate credentials' do |c|
    c.syntax = 'formatron generate credentials [options]'
    c.summary = 'Generate a credentials JSON file'
    c.description = 'Generate a credentials JSON file'
    credentials_options c
    credentials_action c
  end
end

#credentials_options(c) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/formatron/cli/generators/credentials.rb', line 38

def credentials_options(c)
  c.option '-r', '--region STRING', 'The AWS region'
  c.option '-a', '--access-key-id STRING', 'The AWS access key ID'
  c.option(
    '-s',
    '--secret-access-key STRING',
    'The AWS secret access key'
  )
end

#credentials_region(options) ⇒ Object



62
63
64
65
66
67
# File 'lib/formatron/cli/generators/credentials.rb', line 62

def credentials_region(options)
  options.region || choose(
    'Region:',
    *Formatron::AWS::REGIONS.keys
  )
end

#credentials_secret_access_key(options) ⇒ Object



73
74
75
# File 'lib/formatron/cli/generators/credentials.rb', line 73

def credentials_secret_access_key(options)
  options.secret_access_key || password('Secret Access Key? ')
end