Class: Chronicle::ETL::CLI::Secrets

Inherits:
SubcommandBase show all
Defined in:
lib/chronicle/etl/cli/secrets.rb

Overview

CLI commands for working with ETL plugins

Instance Method Summary collapse

Methods inherited from SubcommandBase

banner, help, subcommand_prefix

Instance Method Details

#list(namespace = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chronicle/etl/cli/secrets.rb', line 41

def list(namespace = nil)
  all_secrets = Chronicle::ETL::Secrets.all(namespace)
  cli_exit(message: 'No secrets are stored') unless all_secrets.any?

  rows = []
  all_secrets.each do |namespace, secrets|
    rows += secrets.map do |key, value|
      # hidden_value = (value[0..5] + ("*" * [0, [value.length - 5, 30].min].max)).truncate(30)
      truncated_value = value&.truncate(30)
      [namespace, key, truncated_value]
    end
  end

  headers = %w[namespace key value].map { |h| h.upcase.bold }

  puts 'Available secrets:'
  table = TTY::Table.new(headers, rows)
  puts table.render(indent: 0, padding: [0, 2])
end

#set(namespace, key, value = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chronicle/etl/cli/secrets.rb', line 14

def set(namespace, key, value = nil)
  validate_namespace(namespace)

  if value
    # came as argument
  elsif $stdin.respond_to?(:stat) && $stdin.stat.pipe?
    value = $stdin.read
  else
    prompt = TTY::Prompt.new
    value = prompt.mask("Please enter #{key} for #{namespace}:")
  end

  Chronicle::ETL::Secrets.set(namespace, key, value.strip)
  cli_exit(message: 'Secret set')
rescue TTY::Reader::InputInterrupt
  cli_fail(message: "\nSecret not set")
end

#unset(namespace, key) ⇒ Object



33
34
35
36
37
38
# File 'lib/chronicle/etl/cli/secrets.rb', line 33

def unset(namespace, key)
  validate_namespace(namespace)

  Chronicle::ETL::Secrets.unset(namespace, key)
  cli_exit(message: 'Secret unset')
end