Class: Hiera::Backend::Eyaml::Subcommands::Decrypt

Inherits:
Hiera::Backend::Eyaml::Subcommand show all
Defined in:
lib/hiera/backend/eyaml/subcommands/decrypt.rb

Class Method Summary collapse

Methods inherited from Hiera::Backend::Eyaml::Subcommand

all_options, attach_option, find, hidden?, load_config_file, parse, prettyname

Class Method Details

.descriptionObject



33
34
35
# File 'lib/hiera/backend/eyaml/subcommands/decrypt.rb', line 33

def self.description
  "decrypt some data"
end

.executeObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hiera/backend/eyaml/subcommands/decrypt.rb', line 56

def self.execute
  parser = Parser::ParserFactory.encrypted_parser
  tokens = parser.parse(Eyaml::Options[:input_data])
  case Eyaml::Options[:source]
    when :eyaml
      decrypted = tokens.map{ |token| token.to_decrypted }
      decrypted.join
    else
      yamled = false
      decrypted = tokens.map{ |token|
        case token.class.name
          when /::EncToken$/
            if (yamled) then
              yamled = false
              token.to_plain_text.match(/[\r\n]/) ? "|\n  " + token.to_plain_text.gsub(/([\r\n]+)/, '\1  ') : token.to_plain_text
            else
              token.to_plain_text
            end
          else
            yamled = true
            token.match
        end
      }
      decrypted.join
  end
end

.optionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hiera/backend/eyaml/subcommands/decrypt.rb', line 14

def self.options
  [{:name => :string,
    :description => "Source input is a string provided as an argument",
    :short => 's', 
    :type => :string},
   {:name => :file,
    :description => "Source input is a regular file",
    :short => 'f',
    :type => :string},
   {:name => :eyaml,
    :description => "Source input is an eyaml file",
    :short => 'e',
    :type => :string},
   {:name => :stdin,
    :description => "Source input is taken from stdin",
    :short => :none}
  ]
end

.validate(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hiera/backend/eyaml/subcommands/decrypt.rb', line 37

def self.validate options
  sources = [:eyaml, :password, :string, :file, :stdin].collect {|x| x if options[x]}.compact
  Optimist::die "You must specify a source" if sources.count.zero?
  Optimist::die "You can only specify one of (#{sources.join(', ')})" if sources.count > 1
  options[:source] = sources.first

  options[:input_data] = case options[:source]
  when :stdin
    STDIN.read
  when :string
    options[:string]
  when :file
    File.read options[:file]
  when :eyaml
    File.read options[:eyaml]
  end
  options
end