class Retrospec::Puppet::Generators::ModuleDataGenerator

Attributes

module_name[R]
spec_object[R]

Public Class Methods

new(module_path, spec_object = {}) click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 10
def initialize(module_path, spec_object = {})
  super
  @plural_name = 'module_data'
  @spec_object = spec_object
  @singular_name = @plural_name
end
run_cli(global_opts, args = ARGV) click to toggle source

used to display subcommand options to the cli the global options are passed in for your usage optimist.rubyforge.org all options here are available in the config passed into config object returns the parameters

# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 80
        def self.run_cli(global_opts, args = ARGV)
          backend_types = %w(hiera data_hash lookup_key data_dig)
          sub_command_opts = Optimist.options(args) do
            banner <<-EOS
Generate the scaffolding required to use data in a module from hiera or a custom hiera backend.
 * Data in module is only available in puppet 4.7+
 * Hiera 5 backends are only available in puppet 4.9+
    * https://docs.puppet.com/puppet/4.10/hiera_custom_backends.html

Examples:
  retrospec puppet module_data -b data_hash -n my_custom_hash -t native
  retrospec puppet module_data  (uses defaults which is the hiera backend type, most people will want this )

Options:

            EOS
            opt :backend_type, "Which hiera backend type to use (#{backend_types.join(', ')})",
                :type => :string, :required => false, default: 'hiera', :short => '-b'
            opt :backend_name, 'The name of the custom backend',
                :type => :string, :required => false, default: 'custom', :short => '-n'
            opt :function_type, 'What type of function to create the backend type with (native or v4)',
                :type => :string, :required => false, default: 'native', :short => '-t'
          end
          unless backend_types.include?(sub_command_opts[:backend_type])
            Optimist.die :backend_type, "must be one of #{backend_types.join(', ')}"
          end
          unless %w(native v4).include?(sub_command_opts[:function_type])
            Optimist.die :function_type, "must be one of #{%w(native v4).join(', ')}"
          end

          plugin_data = global_opts.merge(sub_command_opts)
          plugin_data
        end

Public Instance Methods

backend_function_type() click to toggle source

@return [String] function type

# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 32
def backend_function_type
  context.function_type
end
backend_name() click to toggle source

@return [String] backend name

# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 27
def backend_name
  context.backend_name
end
backend_type() click to toggle source

@return [String] backend type

# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 22
def backend_type
  context.backend_type
end
generate_hiera_file() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 40
def generate_hiera_file
  path = File.join(module_path, 'hiera.yaml')
  template = File.join(template_dir, 'hiera.yaml.retrospec.erb')
  safe_create_template_file(path, template, self)
end
generate_module_data_for_function(_type) click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 46
def generate_module_data_for_function(_type)
  data = { :name => backend_name,
           :type => backend_function_type,
           :test_type => 'rspec',
           :module_name => module_name }.merge(spec_object)
  f = FunctionGenerator.new(module_path, data)
  ext = backend_function_type == 'native' ? 'pp' : 'rb'
  template_path = File.join(template_dir, 'functions', backend_function_type, "function_#{backend_type}.#{ext}.retrospec.erb")
  f.generate_function_file(template_path)
  generate_hiera_file
end
generate_module_data_for_hiera() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 36
def generate_module_data_for_hiera
  safe_create_directory_files(template_dir, module_path, context, /functions/)
end
run() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 67
def run
  if backend_type == 'hiera'
    generate_module_data_for_hiera
  else
    generate_module_data_for_function(backend_type)
  end
end
template_dir() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb, line 58
def template_dir
  external_templates = File.expand_path(File.join(config_data[:template_dir], @plural_name))
  if File.exist?(external_templates)
    File.join(config_data[:template_dir], 'module_data')
  else
    File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), 'templates', @plural_name))
  end
end