Class: Chef::Knife::CookbookCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::CookbookCreate
- Defined in:
- lib/chef/knife/cookbook_create_extension.rb
Overview
Cookbook class
Instance Method Summary collapse
-
#cookbook_copyright ⇒ String
Get cookbook copyright.
-
#cookbook_email ⇒ String
Get maintener email.
-
#cookbook_license ⇒ String
Get license name.
-
#cookbook_license_name ⇒ String
Public: Retrieve license name.
-
#cookbook_readme_format ⇒ String
Get readme format.
-
#copy_file(cookbook_path, cookbook_name, file_name) ⇒ Void
Copy files.
-
#create_cookbook_directories(cookbook_path, cookbook_name) ⇒ Void
Create cookbook directories.
-
#create_cookbook_files(cookbook_path, cookbook_name) ⇒ Void
Copy all files into the cookbook.
-
#create_cookbook_templates(params) ⇒ Void
Create cookbook files from templates.
-
#files_directory ⇒ String
Get files directory.
-
#parameter_empty?(parameter) ⇒ String
Test if parameter is empty.
-
#render_template(file_name, params) ⇒ void
Render template.
-
#run ⇒ Void
Public: Knife skeleton create runner.
-
#templates_directory ⇒ String
Get templates directory.
Instance Method Details
#cookbook_copyright ⇒ String
Get cookbook copyright
280 281 282 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 280 def cookbook_copyright config[:cookbook_copyright] || 'YOUR_COMPANY_NAME' end |
#cookbook_email ⇒ String
Get maintener email
288 289 290 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 288 def cookbook_email config[:cookbook_email] || 'YOUR_EMAIL' end |
#cookbook_license ⇒ String
Get license name
296 297 298 299 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 296 def cookbook_license ((config[:cookbook_license] != 'false') && config[:cookbook_license]) || 'none' end |
#cookbook_license_name ⇒ String
Public: Retrieve license name
Examples:
# With mit license
cookbook_license_name
# => 'MIT'
# With apachev2 license
cookbook_license_name
# => 'Apache 2.0'
# With gplv3 license
cookbook_license_name
# => 'GNU Public LIcense 3.0'
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 101 def cookbook_license_name case cookbook_license when 'apachev2' 'Apache 2.0' when 'gplv2' 'GNU Public License 2.0' when 'gplv3' 'GNU Public License 3.0' when 'mit' 'MIT' when 'none' 'All rights reserved' end end |
#cookbook_readme_format ⇒ String
Get readme format
305 306 307 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 305 def cookbook_readme_format ((config[:readme_format] != 'false') && config[:readme_format]) || 'md' end |
#copy_file(cookbook_path, cookbook_name, file_name) ⇒ Void
Copy files
Examples:
copy_file('/tmp', '/cookbooks', 'my-cookbook', 'README.md')
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 216 def copy_file(cookbook_path, cookbook_name, file_name) dst = File.join(cookbook_path, cookbook_name, file_name) if File.exist?(dst) ui.warn("'#{file_name}' already exists") else ui.msg("** Create '#{file_name}'") FileUtils.cp(File .join(files_directory, file_name.gsub(/^\./, '')), dst) end end |
#create_cookbook_directories(cookbook_path, cookbook_name) ⇒ Void
Create cookbook directories
Examples:
create_cookbook_directories('/tmp', 'my-cookbook')
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 126 def create_cookbook_directories(cookbook_path, cookbook_name) ui.msg("** Create cookbook #{cookbook_name} into #{cookbook_path}") %w( attributes definitions libraries providers recipes resources spec files/default templates/default test/integration/default/serverspec).each do |dir| FileUtils.mkdir_p(File.join(cookbook_path, cookbook_name, dir)) end end |
#create_cookbook_files(cookbook_path, cookbook_name) ⇒ Void
Copy all files into the cookbook
Examples:
create_cookbook_files('/tmp', 'my-cookbook')
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 189 def create_cookbook_files(cookbook_path, cookbook_name) %w( Berksfile Gemfile Guardfile .gitignore .rspec .rubocop.yml .travis.yml .chefignore Rakefile ).each do |file_name| copy_file(cookbook_path, cookbook_name, file_name) end end |
#create_cookbook_templates(params) ⇒ Void
Create cookbook files from templates
Examples:
create_cookbook_templates({ cookbook_path: '/tmp', title: 'GoT' })
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 155 def create_cookbook_templates(params) params[:license_content] = File.read(File.join(files_directory, 'licenses', params[:license]) ) if params[:license] != 'none' params[:license_content] = '' unless params[:license] != 'none' %W( metadata.rb CHANGELOG.#{params[:readme_format]} README.#{params[:readme_format]} .kitchen.yml attributes/default.rb recipes/default.rb spec/default_spec.rb spec/spec_helper.rb test/integration/default/serverspec/spec_helper.rb test/integration/default/serverspec/default_spec.rb ).each do |file_name| render_template(file_name, params) end end |
#files_directory ⇒ String
Get files directory
313 314 315 316 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 313 def files_directory File.('../../../../files', Pathname.new(__FILE__).realpath) end |
#parameter_empty?(parameter) ⇒ String
Test if parameter is empty
Examples:
parameter_empty?('my string')
# => false
parameter_empty?('')
# => true
272 273 274 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 272 def parameter_empty?(parameter) parameter.nil? || parameter.empty? end |
#render_template(file_name, params) ⇒ void
This method returns an undefined value.
Render template
Examples:
render_template('/tmp', 'my-file.rb', { title: 'GoT' })
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 242 def render_template(file_name, params) dst = File.join(params[:cookbook_path], params[:cookbook_name], file_name) if File.exist?(dst) ui.warn("'#{file_name}' already exists") else ui.msg("** Create '#{file_name}'") File.open(dst, 'w+') do |file| file.write(KnifeSkeleton::Template .render(File.read(File.join(templates_directory, file_name + '.erb')), params)) end end end |
#run ⇒ Void
Public: Knife skeleton create runner
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 53 def run self.config = Chef::Config.merge!(config) if @name_args.length < 1 show_usage ui.fatal('You must specify a cookbook name') exit 1 end if parameter_empty?(config[:cookbook_path]) fail ArgumentError, <<-eos Default cookbook_path is not specified in the knife.rb config file, and a value to -o is not provided. Nowhere to write the new cookbook to. eos end params = { cookbook_path: File.(Array(config[:cookbook_path]).first), cookbook_name: @name_args.first, copyright: cookbook_copyright, email: cookbook_email, license: cookbook_license, license_name: cookbook_license_name, readme_format: cookbook_readme_format } create_cookbook_directories(params[:cookbook_path], params[:cookbook_name]) create_cookbook_files(params[:cookbook_path], params[:cookbook_name]) create_cookbook_templates(params) end |
#templates_directory ⇒ String
Get templates directory
322 323 324 325 |
# File 'lib/chef/knife/cookbook_create_extension.rb', line 322 def templates_directory File.('../../../../templates', Pathname.new(__FILE__).realpath) end |