Class: ChefDK::Command::GeneratorCommands::Cookbook

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

Overview

## CookbookFile chef generate cookbook path/to/basename –generator-cookbook=path/to/generator

Generates a basic cookbook directory structure. Most file types are omitted, the user is expected to add additional files as needed using the relevant generators.

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 ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#chefdk_home, #err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(params) ⇒ Cookbook

Returns a new instance of Cookbook.



40
41
42
43
44
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 40

def initialize(params)
  @params_valid = true
  @cookbook_name = nil
  super
end

Instance Attribute Details

#cookbook_name_or_pathObject (readonly)

Returns the value of attribute cookbook_name_or_path.



36
37
38
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 36

def cookbook_name_or_path
  @cookbook_name_or_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



34
35
36
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 34

def errors
  @errors
end

Instance Method Details

#cookbook_full_pathObject



95
96
97
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 95

def cookbook_full_path
  File.expand_path(cookbook_name_or_path, Dir.pwd)
end

#cookbook_nameObject



87
88
89
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 87

def cookbook_name
  File.basename(cookbook_full_path)
end

#cookbook_path_in_git_repo?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 109

def cookbook_path_in_git_repo?
  Pathname.new(cookbook_full_path).ascend do |dir|
    return true if File.directory?(File.join(dir.to_s, ".git"))
  end
  false
end

#cookbook_rootObject



91
92
93
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 91

def cookbook_root
  File.dirname(cookbook_full_path)
end

#params_valid?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 105

def params_valid?
  @params_valid
end

#policy_nameObject



71
72
73
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 71

def policy_name
  cookbook_name
end

#policy_run_listObject



75
76
77
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 75

def policy_run_list
  "#{cookbook_name}::#{recipe_name}"
end

#read_and_validate_paramsObject



99
100
101
102
103
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 99

def read_and_validate_params
  arguments = parse_options(params)
  @cookbook_name_or_path = arguments[0]
  @params_valid = false unless @cookbook_name_or_path
end

#recipeObject



79
80
81
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 79

def recipe
  "cookbook"
end

#recipe_nameObject



83
84
85
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 83

def recipe_name
  "default"
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 46

def run
  read_and_validate_params
  if params_valid?
    setup_context
    chef_runner.converge
  else
    err(opt_parser)
    1
  end
rescue ChefDK::ChefRunnerError => e
  err("ERROR: #{e}")
  1
end

#setup_contextObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 60

def setup_context
  super
  Generator.add_attr_to_context(:skip_git_init, cookbook_path_in_git_repo?)
  Generator.add_attr_to_context(:cookbook_root, cookbook_root)
  Generator.add_attr_to_context(:cookbook_name, cookbook_name)
  Generator.add_attr_to_context(:recipe_name, recipe_name)
  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, ".")
end