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, #generator_config, #knife_config

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.



54
55
56
57
58
59
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 54

def initialize(params)
  @params_valid = true
  @cookbook_name = nil
  @berks_mode = true
  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

#berks_mode?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 118

def berks_mode?
  @berks_mode
end

#cookbook_full_pathObject



114
115
116
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 114

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

#cookbook_nameObject



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

def cookbook_name
  File.basename(cookbook_full_path)
end

#cookbook_path_in_git_repo?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 143

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



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

def cookbook_root
  File.dirname(cookbook_full_path)
end

#params_valid?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 139

def params_valid?
  @params_valid
end

#policy_nameObject



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

def policy_name
  cookbook_name
end

#policy_run_listObject



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

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

#read_and_validate_paramsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 122

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

  if config[:berks] && config[:policy]
    err("Berkshelf and Policyfiles are mutually exclusive. Please specify only one.")
    @params_valid = false
  end

  if config[:policy]
    @berks_mode = false
  end
end

#recipeObject



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

def recipe
  "cookbook"
end

#recipe_nameObject



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

def recipe_name
  "default"
end

#runObject



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

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

#setup_contextObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 76

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(:include_chef_repo_source, false)
  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, ".")

  Generator.add_attr_to_context(:use_berkshelf, berks_mode?)
end