Class: Maquina::Credentials::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/maquina/credentials/cli.rb

Constant Summary collapse

EDIT_TEMPLATE =
<<~YAML
  # Edit your credentials below as YAML, then save and close.
  # Example:
  #   database:
  #     password: s3cret
  #   gh_token: ghp_xxx
YAML

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout:, stderr:, stdin:, editor: nil) ⇒ CLI

Returns a new instance of CLI.



24
25
26
27
28
29
30
31
# File 'lib/maquina/credentials/cli.rb', line 24

def initialize(argv, stdout:, stderr:, stdin:, editor: nil)
  @argv = argv.dup
  @stdout = stdout
  @stderr = stderr
  @stdin = stdin
  @editor = editor || method(:default_editor)
  @credentials_path = nil
end

Class Method Details

.start(argv, stdout: $stdout, stderr: $stderr, stdin: $stdin, editor: nil) ⇒ Object



20
21
22
# File 'lib/maquina/credentials/cli.rb', line 20

def self.start(argv, stdout: $stdout, stderr: $stderr, stdin: $stdin, editor: nil)
  new(argv, stdout: stdout, stderr: stderr, stdin: stdin, editor: editor).run
end

Instance Method Details

#runObject



33
34
35
36
37
38
39
40
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
# File 'lib/maquina/credentials/cli.rb', line 33

def run
  command = parser.parse!(@argv).first

  case command
  when "help", nil
    @stdout.puts parser
    0
  when "generate"
    generate
  when "read"
    read(@argv[1])
  when "write"
    write
  when "edit"
    edit
  else
    @stderr.puts parser
    command ? 1 : 0
  end
rescue OptionParser::ParseError => error
  @stderr.puts error.message
  @stderr.puts parser
  1
rescue MasterKeyMissing
  @stderr.puts "MAQUINA_MASTER_KEY is required"
  1
rescue DecryptionFailed
  @stderr.puts "Failed to decrypt credentials"
  1
rescue Psych::Exception
  @stderr.puts "Input must be a valid YAML hash"
  1
end