Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'runlist-comma'
Browse files Browse the repository at this point in the history
* runlist-comma:
  Update docs
  Allow to separate recipes by comma
  Let Chef expand foo to foo::default
  Extract expanding of recipe names
  • Loading branch information
mlafeldt committed Sep 21, 2014
2 parents 6248aa5 + 2b10806 commit 66223c5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

IMPROVEMENTS:

* Support standard Chef syntax for composing the run list: entries may be
separated by comma and an entry named `foo` will now expand to `foo::default`
(see BREAKING CHANGES for more information). As a result, something like
`chef-runner recipe[cats],dogs::bar` now does what Chef users would expect.
* Install Chef using a smart shell wrapper around Omnibus Installer instead of
running complicated shell commands over SSH. Move installer logic from Chef
Solo provisioner to new [omnibus package].
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ Use chef-runner for local cookbook development with Vagrant:
Compose Chef run list using flexible recipe syntax:

$ chef-runner recipes/foo.rb
$ chef-runner ::foo # same as above
$ chef-runner ::foo # same as above
$ chef-runner dogs::bar
$ chef-runner dogs # same as dogs::default
$ chef-runner recipes/foo.rb bar dogs::baz # will run recipes in given order
$ chef-runner recipe[cats],dogs::bar # standard Chef syntax

Provision a specific Vagrant machine in a multi-machine environment:

Expand Down Expand Up @@ -175,6 +177,10 @@ your run list in multiple ways.

$ chef-runner recipes/foo.rb bar dogs::baz

6) Of course, standard Chef syntax is supported as well:

$ chef-runner recipe[cats],dogs::bar

**Note: When defining recipes in a format other than 4), you must be inside a
cookbook directory with a `metadata.rb` file for chef-runner to know the
cookbook's name.**
Expand Down
42 changes: 24 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,35 @@ func findDriver(flags *Flags) (driver.Driver, error) {
return vagrant.NewDriver(flags.Machine)
}

func expand(recipe, cookbook string) (string, error) {
if strings.HasPrefix(recipe, "::") {
if cookbook == "" {
log.Errorf("cannot add local recipe \"%s\" to run list\n",
strings.TrimPrefix(recipe, "::"))
return "", errors.New("cookbook name required")
}
return cookbook + recipe, nil
}
if path.Dir(recipe) == "recipes" && path.Ext(recipe) == ".rb" {
if cookbook == "" {
log.Errorf("cannot add local recipe \"%s\" to run list\n", recipe)
return "", errors.New("cookbook name required")
}
return cookbook + "::" + util.BaseName(recipe, ".rb"), nil
}
return recipe, nil
}

func buildRunList(recipes []string, cookbook string) ([]string, error) {
runList := []string{}
for _, r := range recipes {
var recipe string
if strings.HasPrefix(r, "::") {
if cookbook == "" {
log.Errorf("cannot add local recipe \"%s\" to run list\n",
strings.TrimPrefix(r, "::"))
return nil, errors.New("cookbook name required")
for _, r := range strings.Split(r, ",") {
recipe, err := expand(r, cookbook)
if err != nil {
return nil, err
}
recipe = cookbook + r
} else if path.Dir(r) == "recipes" && path.Ext(r) == ".rb" {
if cookbook == "" {
log.Errorf("cannot add local recipe \"%s\" to run list\n", r)
return nil, errors.New("cookbook name required")
}
recipe = cookbook + "::" + util.BaseName(r, ".rb")
} else if strings.Contains(r, "::") {
recipe = r
} else {
recipe = r + "::default"
runList = append(runList, recipe)
}
runList = append(runList, recipe)
}
return runList, nil
}
Expand Down
7 changes: 6 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ func TestBuildRunList(t *testing.T) {
{
cookbook: "",
recipes: []string{"dogs"},
runList: []string{"dogs::default"},
runList: []string{"dogs"},
},
{
cookbook: "cats",
recipes: []string{"recipes/foo.rb", "::bar", "dogs::baz"},
runList: []string{"cats::foo", "cats::bar", "dogs::baz"},
},
{
cookbook: "cats",
recipes: []string{"recipes/foo.rb,::bar,dogs::baz"},
runList: []string{"cats::foo", "cats::bar", "dogs::baz"},
},
// Check for errors
{
cookbook: "",
Expand Down

0 comments on commit 66223c5

Please sign in to comment.