chef / chef/chef-cli

How to add additional "generator" commands?

Open
#44 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Aspect: UX documentation Triage: Confirmed Triage: Feature Request Triage: Needs Information Type: Design Proposal Type: Enhancement
Dominant language
Ruby
Stars
18
Forks
23
Avg merge
5d 17h
Merged PRs (30d)
1

Description

Description

I am looking to extend the ChefDK generator so that we can do things like chef generate library helpers which will generate the libraries directory and the libraries/helper.rb file with all of our standard boiler plate and the standard module layout we use for Coobook Libraries.

ChefDK Version

working with 1.6.1, but have confirmed this in a current 2.4 ChefDK

Platform Version

CentOS 6.9, but confirmed on other platforms as well.

Replication Case

Have attempted to add the following to .chef/knife.rb (though not all at once)

if defined?(ChefDK)
  puts 'this is a test'
  chefdk.send(:generator, :xxxxxxx, :MyLibrary, "generate my lib")
end

...

ChefDK::Command::Generate.send(:generator, :library4, :MyLibrary3, "Generate a libarray!")

...

require_relative './test_gen'
require 'chef-dk/generator'
include ChefDK::Generator::Context
include ChefDK::Command::Generate
include ChefDK::Command::MyGenerate
generator(:xxxxxxxx, :MyLibrary, "generate my lib")

...

before do
  ChefDK::Command::Generate.send(:generator, :library, :MyLibrary, "generate my lib")
  ChefDK::Generator::Context.add_attr(:zzzzzzzz)
  ChefDK::Generator::Generate.class_eval{ generator(:yyyyyy, :MyLib, 'whatever') }
end

...

# Following the Nordstrom Julia Pattern:  https://github.com/Nordstrom/chefdk-julia/blob/master/lib/chefdk/julia.rb
# Created: lib/chefdk/addcommands.rb
Module ChefDK
  Module AddCommands
    require ChefDK::Command::Generate

    generator(:xxxxxxx, :MyLibrary, "generate my lib")
    ChefDK::Command::Generate.generator(:yyyyyyyyy, :MyLib, "yyyyyyyyyyyyyy")

    def self.path
      File.expand_path(File.join(File.dirname(__FILE__), *%w(.. ..)))
    end
  end
end

# then generated a gem, installed it and included the following in knife.rb

if defined?(ChefDK)
  require 'chefdk/AddCommands'
  chefdk.generator_cookbook ChefDK::AddCommands.path
end

...

# in same gem, created lib/chefdk/command/generate.rb
module ChefDK
  module Command
    class Generate < Base

     generator(:xxxxxxx, :MyLibrary, "generate my lib")

    end
  end
end

When I run chef generate I would expect to see xxxxxxx or library or yyyyyyyy as options, and if not displayed there, be able to run chef generate xxxxxxx and have it load my library.

If I edit /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-dk-1.6.1/lib/chef-dk/command/generate.rb

I am able to add

generator(:xxxxxxx, :MyLibrary, "generate my lib")

And then run chef generate and see my xxxxxxx option. (That then tries to load ChefDK::Command::GeneratorCommands::MyLibrary, which I think is a whole other problem and, I think, easily dealt with if you create a lib/chefdk/command/generator_commands/my_library.rb and require_relative...)

Stacktrace

Not really applicable as no error message is generated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the generator registration in lib/chef-dk/command/generate.rb and compare it with the extension pattern in chefdk-julia's lib/chefdk/julia.rb. Review the attempted knife.rb and generator_commands integration described in the issue. Done means a documented extension path lets a custom command appear in chef generate help and run without editing the installed gem.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.