class ChefDK::Command::GeneratorCommands::Policyfile

Attributes

new_file_basename[R]
policy_name[R]
policy_run_list[R]
policyfile_dir[R]

Public Class Methods

new(*args) click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 36
def initialize(*args)
  super
  @new_file_basename = nil
  @policyfile_dir = nil
  @policy_name = nil
  @policy_run_list = nil
  @params_valid = true
end

Public Instance Methods

read_and_validate_params() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 71
def read_and_validate_params
  arguments = parse_options(params)

  case arguments.size
  when 0
    if chef_repo_mode?
      err("ERROR: You must give a policy name when generating a policy in a chef-repo.")
      @params_valid = false
      return false
    else
      use_default_policy_settings
    end
  when 1
    derive_policy_settings_from_args(arguments[0])
  else
    @params_valid = false
    err("ERROR: too many arguments")
    return false
  end
end
recipe() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 45
def recipe
  "policyfile"
end
run() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 59
def run
  read_and_validate_params
  if params_valid?
    setup_context
    chef_runner.converge
    0
  else
    err(opt_parser)
    1
  end
end
setup_context() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 49
def setup_context
  super
  Generator.add_attr_to_context(:policyfile_dir, policyfile_dir)
  Generator.add_attr_to_context(:new_file_basename, new_file_basename)
  Generator.add_attr_to_context(:include_chef_repo_source, chef_repo_mode?)
  Generator.add_attr_to_context(:policy_name, policy_name)
  Generator.add_attr_to_context(:policy_run_list, policy_run_list)
  Generator.add_attr_to_context(:policy_local_cookbook, nil)
end

Private Instance Methods

chef_repo_mode?() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 118
def chef_repo_mode?
  File.exist?(File.expand_path(".chef-repo.txt"))
end
derive_policy_settings_from_args(new_file_path) click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 101
def derive_policy_settings_from_args(new_file_path)
  @new_file_basename = File.basename(new_file_path, ".rb")
  @policy_name = @new_file_basename
  @policy_run_list = "#{policy_name}::default"
  given_policy_dirname = File.expand_path(File.dirname(new_file_path))
  @policyfile_dir =
    if chef_repo_mode? && (given_policy_dirname == Dir.pwd)
      File.expand_path("policyfiles")
    else
      given_policy_dirname
    end
end
params_valid?() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 114
def params_valid?
  @params_valid
end
use_default_policy_settings() click to toggle source
# File lib/chef-dk/command/generator_commands/policyfile.rb, line 94
def use_default_policy_settings
  @new_file_basename = "Policyfile"
  @policy_name = "example-application-service"
  @policy_run_list = "example_cookbook::default"
  @policyfile_dir = Dir.pwd
end