Class: LocalVault::StdinSecretInput

Inherits:
Object
  • Object
show all
Defined in:
lib/localvault/stdin_secret_input.rb

Defined Under Namespace

Classes: InteractiveInput, InvalidEncoding

Constant Summary collapse

PIPE_EXAMPLE =
%(printf '%s' "$SECRET" | localvault set KEY --stdin).freeze

Class Method Summary collapse

Class Method Details

.read(input = $stdin) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/localvault/stdin_secret_input.rb', line 8

def self.read(input = $stdin)
  if input.respond_to?(:tty?) && input.tty?
    raise InteractiveInput, "Refusing to read secret from interactive stdin. " \
      "Pipe it instead: #{PIPE_EXAMPLE}"
  end

  input.binmode if input.respond_to?(:binmode)
  value = input.read || ""
  value = value.dup.force_encoding(Encoding::UTF_8)
  raise InvalidEncoding, "Secret value must be valid UTF-8" unless value.valid_encoding?

  value
end