Class: ChefCLI::Command::GeneratorCommands::Cookbook

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-cli/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 ChefCLI::Configurable

#chef_config, #chefcli_config, #config_loader, #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) ⇒ Cookbook

Returns a new instance of Cookbook.



93
94
95
96
97
98
99
100
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 93

def initialize(params)
  @params_valid = true
  @cookbook_name = nil
  @policy_mode = true
  @verbose = false
  @specs = false
  super
end

Instance Attribute Details

#cookbook_name_or_pathObject (readonly)

Returns the value of attribute cookbook_name_or_path.



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

def cookbook_name_or_path
  @cookbook_name_or_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Instance Method Details

#cookbook_full_pathObject



182
183
184
185
186
187
188
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 182

def cookbook_full_path
  if !cookbook_name_or_path.nil? && !cookbook_name_or_path.empty?
    File.expand_path(cookbook_name_or_path, Dir.pwd)
  else
    ""
  end
end

#cookbook_nameObject



174
175
176
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 174

def cookbook_name
  File.basename(cookbook_full_path)
end

#cookbook_path_in_git_repo?Boolean

Returns:

  • (Boolean)


248
249
250
251
252
253
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 248

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



178
179
180
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 178

def cookbook_root
  File.dirname(cookbook_full_path)
end

#create_vscode_dir?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 255

def create_vscode_dir?
  ::File.exist?("/Applications/Visual Studio Code.app") || ::File.exist?("#{ENV["APPDATA"]}\\Code")
end

#emit_post_create_messageObject



120
121
122
123
124
125
126
127
128
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 120

def emit_post_create_message
  default_recipe_file = yaml ? "default.yml" : "default.rb"
  msg("Your cookbook is ready. Type `cd #{cookbook_name_or_path}` to enter it.")
  msg("\nThere are several commands you can run to get started locally developing and testing your cookbook.")
  msg("\nWhy not start by writing an InSpec test? Tests for the default recipe are stored at:\n")
  msg("test/integration/default/default_test.rb")
  msg("\nIf you'd prefer to dive right in, the default recipe can be found at:")
  msg("\nrecipes/#{default_recipe_file}\n")
end

#kitchenObject



150
151
152
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 150

def kitchen
  config[:kitchen]
end

#params_valid?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 244

def params_valid?
  @params_valid
end

#policy_mode?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 190

def policy_mode?
  @policy_mode
end

#policy_nameObject



158
159
160
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 158

def policy_name
  cookbook_name
end

#policy_run_listObject



162
163
164
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 162

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

#read_and_validate_paramsObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 202

def read_and_validate_params
  arguments = parse_options(params)
  @cookbook_name_or_path = arguments[0]

  if !@cookbook_name_or_path
    @params_valid = false
  elsif File.basename(@cookbook_name_or_path).include?("-")
    msg("Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/workstation/ctl_chef/#chef-generate-cookbook for more information.")
  end

  if !generator_cookbook_path.empty? &&
      !cookbook_full_path.empty? &&
      File.identical?(Pathname.new(cookbook_full_path).parent, generator_cookbook_path)
    err("The generator and the cookbook cannot be in the same directory. Please specify a cookbook directory that is different from the generator's parent.")
    @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[:workflow] || config[:pipeline]
    err("[DEPRECATION] Chef Workflow (Delivery) is end of life (EOL) as of December 31, 2020 and the --workflow and --pipeline flags have been removed")
    @params_valid = false
  end

  if config[:berks]
    @policy_mode = false
  end

  if config[:verbose]
    @verbose = true
  end

  if config[:specs]
    @specs = true
  end

  true
end

#recipeObject



166
167
168
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 166

def recipe
  "cookbook"
end

#recipe_nameObject



170
171
172
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 170

def recipe_name
  "default"
end

#runObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 102

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

#setup_contextObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 130

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(:verbose, verbose?)
  Generator.add_attr_to_context(:specs, specs?)

  Generator.add_attr_to_context(:use_policyfile, policy_mode?)
  Generator.add_attr_to_context(:kitchen, kitchen)
  Generator.add_attr_to_context(:vscode_dir, create_vscode_dir?)
  Generator.add_attr_to_context(:yaml, yaml)
end

#specs?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 198

def specs?
  @specs
end

#verbose?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 194

def verbose?
  @verbose
end

#yamlObject



154
155
156
# File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 154

def yaml
  config[:yaml]
end