How do I configure defaults for Chef generator attributes?

When using chef generate cookbook, it generates with some default attributes for copyright, license, and email. I went looking for how to override those, and it doesn’t appear to be documented anywhere. The attributes that worked for knife cookbook create:

cookbook_copyright "MyCo"
cookbook_email "me@example.com"

are ignored by ChefDK’s generators.

After a little spelunking through the ChefDK source code, I found that if I put the following in ~/.chef/config.rb, it worked:

chefdk.generator.copyright_holder "MyCo"
chefdk.generator.license   "apache2"
chefdk.generator.email     "me@example.com"

Yay for progress! I can run chef generate cookbook and everything works great. Until I run Berkshelf, that is…

PS C:\Users\l.wright\Documents\code\mycookbook> berks
C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/chef-config-12.7.2/lib/chef-config/workstation_config_loader.rb:161:in `rescue in read_config': You have an error in your config file C:/Users/l.wright/.chef/config.rb (ChefConfig::ConfigurationError)

NoMethodError: undefined method `copyright_holder' for nil:NilClass
  C:/Users/l.wright/.chef/config.rb:1:in `from_string'

Is there a way to set these defaults that will make both ChefDK and berks happy?

You can generate your own generator cookbook with “chef generate generator”

  • and just enable it in knife.rb (chefdk.generator_cookbook
    "path-to-your-generator-cookbook/chef_generator"

Cool.

I’m familiar with that, but it seems like this should work without having to have my own generator cookbook.

How about something like this in your knife.rb/config.rb:
if File.basename($PROGRAM_NAME).eql?('chef') && ARGV[0].eql?('generate') chefdk.generator.copyright_holder "MyCo" chefdk.generator.license "apache2" chefdk.generator.email "me@example.com" end

1 Like

That’s a good workaround, and I’ll probably use something like that for now. It still feels like this should Just Work out of the box, though.

The following is working for me, for both chef generate and knife cookbook create:

knife[:cookbook_copyright] = 'MyCo'
knife[:cookbook_email] = 'my_email'

Regards,
Christine

1 Like