Class: ChefCLI::Command::GeneratorCommands::CookbookCodeFile

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-cli/command/generator_commands/cookbook_code_file.rb

Overview

## CookbookCodeFile A base class for generators that add individual files to existing cookbooks.

Instance Attribute Summary collapse

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#chef_runner, #generator_cookbook_name, #generator_cookbook_path, #have_git?

Methods included from ChefCLI::Configurable

#chef_config, #chefcli_config, #config_loader, #default_chef_server_http_client, #generator_config, #knife_config, #reset_config!

Methods inherited from Base

#check_license_acceptance, #needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(params) ⇒ CookbookCodeFile

Returns a new instance of CookbookCodeFile.



34
35
36
37
38
39
40
41
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 34

def initialize(params)
  @params_valid = true
  @cookbook_full_path = nil
  @new_file_basename = nil
  @errors = []
  @params = params
  super
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



29
30
31
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 29

def cookbook_path
  @cookbook_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



28
29
30
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 28

def errors
  @errors
end

#new_file_basenameObject (readonly)

Returns the value of attribute new_file_basename.



30
31
32
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 30

def new_file_basename
  @new_file_basename
end

Instance Method Details

#cookbook_nameObject



68
69
70
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 68

def cookbook_name
  File.basename(cookbook_path)
end

#cookbook_rootObject



64
65
66
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 64

def cookbook_root
  File.dirname(cookbook_path)
end

#params_valid?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 94

def params_valid?
  @params_valid
end

#read_and_validate_paramsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 72

def read_and_validate_params
  arguments = parse_options(params)
  case arguments.size
  when 1
    @new_file_basename = arguments[0]
    @cookbook_path = Dir.pwd
    validate_cookbook_path
  when 2
    @cookbook_path = arguments[0]
    @new_file_basename = arguments[1]
  else
    @params_valid = false
  end
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 43

def run
  read_and_validate_params
  if params_valid?
    setup_context
    chef_runner.converge
  else
    errors.each { |error| err("Error: #{error}") }
    parse_options(params)
    msg(opt_parser)
    1
  end
end

#setup_contextObject



56
57
58
59
60
61
62
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 56

def setup_context
  super
  Generator.add_attr_to_context(:cookbook_root, cookbook_root)
  Generator.add_attr_to_context(:cookbook_name, cookbook_name)
  Generator.add_attr_to_context(:new_file_basename, new_file_basename)
  Generator.add_attr_to_context(:recipe_name, new_file_basename)
end

#validate_cookbook_pathObject



87
88
89
90
91
92
# File 'lib/chef-cli/command/generator_commands/cookbook_code_file.rb', line 87

def validate_cookbook_path
  unless File.file?(File.join(cookbook_path, "metadata.rb"))
    @errors << "Directory #{cookbook_path} is not a cookbook"
    @params_valid = false
  end
end