Class: ChefCLI::Command::GeneratorCommands::Cookbook
  
  
  
  
  
    - Inherits:
- 
      Base
      
        
          - Object
- Base
- Base
- ChefCLI::Command::GeneratorCommands::Cookbook
 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?
  
  
  
  
  
  
  
  
  
  #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)  ⇒ 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_path  ⇒ Object  
  
  
  
  
    
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 | 
 
    
      
      
      
  
  
    #errors  ⇒ Object  
  
  
  
  
    
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_path  ⇒ Object 
  
  
  
  
    | 
197
198
199 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 197
def cookbook_full_path
  File.expand_path(cookbook_name_or_path, Dir.pwd)
end | 
 
    
      
  
  
    #cookbook_name  ⇒ Object 
  
  
  
  
    | 
189
190
191 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 189
def cookbook_name
  File.basename(cookbook_full_path)
end | 
 
    
      
  
  
    #cookbook_path_in_git_repo?  ⇒ Boolean 
  
  
  
  
    | 
263
264
265
266
267
268 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 263
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_root  ⇒ Object 
  
  
  
  
    | 
193
194
195 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 193
def cookbook_root
  File.dirname(cookbook_full_path)
end | 
 
    
      
  
  
    #create_vscode_dir?  ⇒ Boolean 
  
  
  
  
    | 
270
271
272 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 270
def create_vscode_dir?
  ::File.exist?("/Applications/Visual Studio Code.app") || ::File.exist?("#{ENV["APPDATA"]}\\Code")
end | 
 
    
      
  
  
    #emit_post_create_message  ⇒ Object 
  
  
  
  
    | 
120
121
122
123
124
125
126
127
128
129
130
131
132
133 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 120
def emit_post_create_message
  default_recipe_file = yaml ? "default.yml" : "default.rb"
  if have_delivery_config?
    msg("Your cookbook is ready. To setup the pipeline, type `cd #{cookbook_name_or_path}`, then run `delivery init`")
  else
    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("Type `delivery local --help` to see a full list of local testing commands.")
    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
end | 
 
    
      
  
  
    #enable_workflow?  ⇒ Boolean 
  
  
  
  
    | 
205
206
207 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 205
def enable_workflow?
  config[:workflow]
end | 
 
    
      
  
  
    #have_delivery_config?  ⇒ Boolean 
  
  
  
  
    
Is there a .delivery/cli.toml in the current dir or any of the parent dirs
   
 
  
    | 
222
223
224
225
226
227
228 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 222
def have_delivery_config?
      
  Pathname.pwd.ascend.any? { |path| path.join(".delivery/cli.toml").exist? }
end | 
 
    
      
  
  
    #kitchen  ⇒ Object 
  
  
  
  
    | 
161
162
163 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 161
def kitchen
  config[:kitchen]
end | 
 
    
      
  
  
    #params_valid?  ⇒ Boolean 
  
  
  
  
    | 
259
260
261 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 259
def params_valid?
  @params_valid
end | 
 
    
      
  
  
    #pipeline  ⇒ Object 
  
  
  
  
    | 
165
166
167 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 165
def pipeline
  config[:pipeline]
end | 
 
    
      
  
  
    #policy_mode?  ⇒ Boolean 
  
  
  
  
    | 
201
202
203 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 201
def policy_mode?
  @policy_mode
end | 
 
    
      
  
  
    #policy_name  ⇒ Object 
  
  
  
  
    | 
173
174
175 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 173
def policy_name
  cookbook_name
end | 
 
    
      
  
  
    #policy_run_list  ⇒ Object 
  
  
  
  
    | 
177
178
179 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 177
def policy_run_list
  "#{cookbook_name}::#{recipe_name}"
end | 
 
    
      
  
  
    #read_and_validate_params  ⇒ Object 
  
  
  
  
    | 
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 230
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 config[:berks] && config[:policy]
    err("Berkshelf and Policyfiles are mutually exclusive. Please specify only one.")
    @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 | 
 
    
      
  
  
    #recipe  ⇒ Object 
  
  
  
  
    | 
181
182
183 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 181
def recipe
  "cookbook"
end | 
 
    
      
  
  
    #recipe_name  ⇒ Object 
  
  
  
  
    | 
185
186
187 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 185
def recipe_name
  "default"
end | 
 
    
      
  
  
    #run  ⇒ Object 
  
  
  
  
    | 
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_context  ⇒ Object 
  
  
  
  
    | 
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 135
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(:enable_workflow, enable_workflow?)
  Generator.add_attr_to_context(:workflow_project_dir, cookbook_full_path)
  Generator.add_attr_to_context(:build_cookbook_parent_is_cookbook, true)
  Generator.add_attr_to_context(:workflow_project_git_initialized, have_git? && !cookbook_path_in_git_repo?)
  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(:pipeline, pipeline)
  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 
  
  
  
  
    | 
213
214
215 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 213
def specs?
  @specs
end | 
 
    
      
  
  
    #verbose?  ⇒ Boolean 
  
  
  
  
    | 
209
210
211 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 209
def verbose?
  @verbose
end | 
 
    
      
  
  
    #yaml  ⇒ Object 
  
  
  
  
    | 
169
170
171 | # File 'lib/chef-cli/command/generator_commands/cookbook.rb', line 169
def yaml
  config[:yaml]
end |