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

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

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



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

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

#cookbook_nameObject



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

def cookbook_name
  File.basename(cookbook_full_path)
end

#cookbook_path_in_git_repo?Boolean

Returns:

  • (Boolean)


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

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



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

def cookbook_root
  File.dirname(cookbook_full_path)
end

#params_valid?Boolean

Returns:

  • (Boolean)


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

def params_valid?
  @params_valid
end

#read_and_validate_paramsObject



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

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



68
69
70
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 68

def recipe
  "cookbook"
end

#recipe_nameObject



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

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
    msg(banner)
    1
  end
rescue ChefDK::ChefRunnerError => e
  err("ERROR: #{e}")
  1
end

#setup_contextObject



60
61
62
63
64
65
66
# 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)
end