Class: Hiera::Backend::Eyaml::Subcommand

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/eyaml/subcommand.rb

Constant Summary collapse

@@global_options =
[
  { name: :encrypt_method,
    description: 'Override default encryption and decryption method (default is PKCS7)',
    short: 'n',
    default: 'pkcs7', },
  { name: :version,
    description: 'Show version information', },
  { name: :verbose,
    description: 'Be more verbose',
    short: 'v', },
  { name: :trace,
    description: 'Enable trace debug',
    short: 't', },
  { name: :quiet,
    description: 'Be less verbose',
    short: 'q', },
  { name: :help,
    description: 'Information on how to use this command',
    short: 'h', },
]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.global_optionsObject

Returns the value of attribute global_options.



10
11
12
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 10

def global_options
  @global_options
end

.helptextObject

Returns the value of attribute helptext.



10
11
12
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 10

def helptext
  @helptext
end

.optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 10

def options
  @options
end

Class Method Details

.all_optionsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 64

def self.all_options
  options = @@global_options.dup
  options += self.options if self.options
  options += Plugins.options
  # merge in defaults from configuration files
  config_file = load_config_file
  options.map!  do |opt|
    key_name = "#{opt[:name]}"
    if config_file[:options].has_key? key_name
      opt[:default] = config_file[:options][key_name]
      opt
    else
      opt
    end
  end
  { options: options, sources: config_file[:sources] || [] }
end

.attach_option(opt) ⇒ Object



82
83
84
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 82

def self.attach_option(opt)
  self.suboptions += opt
end

.descriptionObject



142
143
144
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 142

def self.description
  'no description'
end

.executeObject

Raises:

  • (StandardError)


150
151
152
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 150

def self.execute
  raise StandardError, "This command is not implemented yet (#{to_s.split('::').last})"
end

.find(commandname = 'unknown_command') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 86

def self.find(commandname = 'unknown_command')
  begin
    require "hiera/backend/eyaml/subcommands/#{commandname.downcase}"
  rescue Exception => e
    require 'hiera/backend/eyaml/subcommands/unknown_command'
    return Hiera::Backend::Eyaml::Subcommands::UnknownCommand
  end
  command_module = Module.const_get(:Hiera).const_get(:Backend).const_get(:Eyaml).const_get(:Subcommands)
  command_class = Utils.find_closest_class parent_class: command_module, class_name: commandname
  command_class || Hiera::Backend::Eyaml::Subcommands::UnknownCommand
end

.hidden?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 158

def self.hidden?
  false
end

.load_config_fileObject



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
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 34

def self.load_config_file
  config = { options: {}, sources: [] }

  config_paths = []
  # Global
  config_paths += ['/etc/eyaml/config.yaml']
  # Home directory
  env_home = ENV.fetch('HOME', nil)
  config_paths += ["#{env_home}/.eyaml/config.yaml"] if env_home
  # Relative to current directory
  config_paths += ['.eyaml/config.yaml']
  # Explicit ENV variable.
  env_eyaml_config = ENV.fetch('EYAML_CONFIG', nil)
  config_paths += [env_eyaml_config] if env_eyaml_config

  # Load each path and stack configs.
  config_paths.each do |config_file|
    next unless config_file and File.file? config_file

    begin
      yaml_contents = YAML.load_file(config_file)
      config[:options].merge! yaml_contents
      config[:sources].push(config_file)
    rescue StandardError
      raise StandardError, "Could not open config file \"#{config_file}\" for reading"
    end
  end
  config
end

.parseObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 98

def self.parse
  me = self
  all = all_options

  options = Optimist.options do
    version 'Hiera-eyaml version ' + Hiera::Backend::Eyaml::VERSION.to_s
    banner ["eyaml #{me.prettyname}: #{me.description}", me.helptext, 'Options:'].compact.join("\n\n")

    all[:options].each do |available_option|
      skeleton = { description: '',
                   short: :none, }

      skeleton.merge! available_option
      opt skeleton[:name],
          skeleton[:desc] || skeleton[:description], # legacy plugins
          short: skeleton[:short],
          default: skeleton[:default],
          type: skeleton[:type]
    end

    stop_on Eyaml.subcommands
  end

  Hiera::Backend::Eyaml.verbosity_level += 1 if options[:verbose]

  Hiera::Backend::Eyaml.verbosity_level += 2 if options[:trace]

  Hiera::Backend::Eyaml.verbosity_level = 0 if options[:quiet]

  Hiera::Backend::Eyaml.default_encryption_scheme = options[:encrypt_method] if options[:encrypt_method]

  if all[:sources]
    all[:sources].each do |source|
      LoggingHelper.debug "Loaded config from #{source}"
    end
  end

  options
end

.prettynameObject



154
155
156
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 154

def self.prettyname
  Utils.snakecase to_s.split('::').last
end

.validate(args) ⇒ Object



138
139
140
# File 'lib/hiera/backend/eyaml/subcommand.rb', line 138

def self.validate(args)
  args
end