Class: ChefDK::Command::GeneratorCommands::CookbookCodeFile

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

Overview

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

Direct Known Subclasses

Attribute, CookbookFile, LWRP, Recipe, Template

Instance Attribute Summary collapse

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#chef_runner, #generator_context, #generator_cookbook_path, #have_git?

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_root, #stderr, #stdout, #system_command

Constructor Details

#initialize(params) ⇒ CookbookCodeFile

Returns a new instance of CookbookCodeFile.



267
268
269
270
271
272
273
# File 'lib/chef-dk/command/generator_commands.rb', line 267

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

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



262
263
264
# File 'lib/chef-dk/command/generator_commands.rb', line 262

def cookbook_path
  @cookbook_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



261
262
263
# File 'lib/chef-dk/command/generator_commands.rb', line 261

def errors
  @errors
end

#new_file_basenameObject (readonly)

Returns the value of attribute new_file_basename.



263
264
265
# File 'lib/chef-dk/command/generator_commands.rb', line 263

def new_file_basename
  @new_file_basename
end

Instance Method Details

#cookbook_nameObject



299
300
301
# File 'lib/chef-dk/command/generator_commands.rb', line 299

def cookbook_name
  File.basename(cookbook_path)
end

#cookbook_rootObject



295
296
297
# File 'lib/chef-dk/command/generator_commands.rb', line 295

def cookbook_root
  File.dirname(cookbook_path)
end

#params_valid?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/chef-dk/command/generator_commands.rb', line 325

def params_valid?
  @params_valid
end

#read_and_validate_paramsObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/chef-dk/command/generator_commands.rb', line 303

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



275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/chef-dk/command/generator_commands.rb', line 275

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

#setup_contextObject



288
289
290
291
292
293
# File 'lib/chef-dk/command/generator_commands.rb', line 288

def setup_context
  super
  generator_context.cookbook_root = cookbook_root
  generator_context.cookbook_name = cookbook_name
  generator_context.new_file_basename = new_file_basename
end

#validate_cookbook_pathObject



318
319
320
321
322
323
# File 'lib/chef-dk/command/generator_commands.rb', line 318

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