Class: InspecPlugins::Init::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/inspec-init/lib/inspec-init/cli.rb,
lib/plugins/inspec-init/lib/inspec-init/cli_plugin.rb,
lib/plugins/inspec-init/lib/inspec-init/cli_profile.rb,
lib/plugins/inspec-init/lib/inspec-init/cli_resource.rb

Constant Summary collapse

TEMPLATES_PATH =
File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "templates"))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid_profile_platformsObject

——————————————————————-#

inspec init profile

——————————————————————-#



10
11
12
13
14
# File 'lib/plugins/inspec-init/lib/inspec-init/cli_profile.rb', line 10

def self.valid_profile_platforms
  # Look in the 'template/profiles' directory and detect which platforms are available.
  profile_templates_dir = File.join(TEMPLATES_PATH, "profiles")
  Dir.glob(File.join(profile_templates_dir, "*")).select { |p| File.directory?(p) }.map { |d| File.basename(d) }
end

Instance Method Details

#plugin(plugin_name) ⇒ Object



29
30
31
32
33
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
# File 'lib/plugins/inspec-init/lib/inspec-init/cli_plugin.rb', line 29

def plugin(plugin_name)
  Inspec.with_feature("inspec-cli-init-plugin") {
    plugin_type = determine_plugin_type(plugin_name)
    snake_case = plugin_name.tr("-", "_")

    # Handle deprecation of option --hook
    unless options[:hook].nil?
      Inspec.deprecate "cli_option_hook"
      options[:activator] = options.delete(:hook)
    end

    template_vars = {
      name: plugin_name,
      plugin_name: plugin_name,
      snake_case: snake_case,
    }.merge(plugin_vars_from_opts)

    template_path = File.join("plugins", plugin_type + "-plugin-template")

    render_opts = {
      templates_path: TEMPLATES_PATH,
      overwrite: options[:overwrite],
      file_rename_map: make_rename_map(plugin_type, plugin_name, snake_case),
      skip_files: make_skip_list(template_vars["activators"].keys),
    }

    renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)

    renderer.render_with_values(template_path, plugin_type + " plugin", template_vars)
  }
end

#profile(new_profile_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plugins/inspec-init/lib/inspec-init/cli_profile.rb', line 27

def profile(new_profile_name)
  Inspec.with_feature("inspec-cli-init-profile") {
    unless valid_profile_platforms.include?(options[:platform])
      ui.error "Unable to generate profile: No template available for platform '#{options[:platform]}' (expected one of: #{valid_profile_platforms.join(", ")})"
      ui.exit(:usage_error)
    end
    template_path = File.join("profiles", options[:platform])

    render_opts = {
      templates_path: TEMPLATES_PATH,
      overwrite: options[:overwrite],
    }
    renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)

    vars = {
      name: new_profile_name,
    }
    renderer.render_with_values(template_path, "profile", vars)
  }
end

#resource(resource_name) ⇒ Object

Wishlist:

Make make_rename_map_resource dynamic:
 + Add a --path option which defaults to ., which will create the tree under that path
 + Add a --layout option which changes all the tree to act as placing the files in core inspec (lib/inspec/resources, docs-chef-io/)
 - Add a --template=plural option which changes the templates to use a set of Filtertable based templates
 - Add a --template=inherit option which provides a template for inheriting from the core resources
 - Add a template=aws
+ Generate properties and matchers:
 + generate a has_bells? matcher => it { should have_bells }
 + generate a is_purple? matcher => it { should be_purple }
 + generate a shoe_size => its('shoe_size') { should cmp 10 }
+ Generate unit tests for above properties and matchers
+ Generate docs for properties and matchers
+ Add --overwrite option


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/plugins/inspec-init/lib/inspec-init/cli_resource.rb', line 37

def resource(resource_name)
  Inspec.with_feature("inspec-cli-init-resource") {
    resource_vars_from_opts_resource
    template_vars = {
      name: options[:path], # This is used for the path prefix
      resource_name: resource_name,
    }
    template_vars.merge!(options)
    template_path = File.join("resources", template_vars["template"])

    render_opts = {
      templates_path: TEMPLATES_PATH,
      overwrite: options[:overwrite],
      file_rename_map: make_rename_map_resource(template_vars),
    }
    renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)
    renderer.render_with_values(template_path, "resource", template_vars)
  }
end