Class: Retrospec::Puppet::Generators::ModuleDataGenerator

Inherits:
BaseGenerator
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb

Instance Attribute Summary collapse

Attributes inherited from BaseGenerator

#context, #generator_template_dir_name, #plural_name, #singular_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseGenerator

#generate_lib_files, #generate_spec_files, #get_binding, #item_name, #item_path, #item_spec_path, #lib_path, #logger, #spec_path

Constructor Details

#initialize(module_path, spec_object = {}) ⇒ ModuleDataGenerator

Returns a new instance of ModuleDataGenerator.



10
11
12
13
14
15
# 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

Instance Attribute Details

#module_nameObject (readonly)

Returns the value of attribute module_name.



9
10
11
# File 'lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb', line 9

def module_name
  @module_name
end

#spec_objectObject (readonly)

Returns the value of attribute spec_object.



9
10
11
# File 'lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb', line 9

def spec_object
  @spec_object
end

Class Method Details

.run_cli(global_opts, args = ARGV) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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 = Trollop.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])
    Trollop.die :backend_type, "must be one of #{backend_types.join(', ')}"
  end
  unless %w(native v4).include?(sub_command_opts[:function_type])
    Trollop.die :function_type, "must be one of #{%w(native v4).join(', ')}"
  end

  plugin_data = global_opts.merge(sub_command_opts)
  plugin_data
end

Instance Method Details

#backend_function_typeString

Returns function type.

Returns:

  • (String)

    function type



32
33
34
# File 'lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb', line 32

def backend_function_type
  context.function_type
end

#backend_nameString

Returns backend name.

Returns:

  • (String)

    backend name



27
28
29
# File 'lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb', line 27

def backend_name
  context.backend_name
end

#backend_typeString

Returns backend type.

Returns:

  • (String)

    backend type



22
23
24
# File 'lib/retrospec/plugins/v1/plugin/generators/module_data_generator.rb', line 22

def backend_type
  context.backend_type
end

#generate_hiera_fileObject



40
41
42
43
44
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# 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_hieraObject



36
37
38
# 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

#runObject



67
68
69
70
71
72
73
# 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_dirObject



58
59
60
61
62
63
64
65
# 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