Skip to content

Commit

Permalink
Merge pull request #44 from cloudfoundry/dk-out-flag
Browse files Browse the repository at this point in the history
--path flag to pull out values out of manifest
  • Loading branch information
cppforlife authored Nov 11, 2016
2 parents 5e0b544 + cd7d585 commit 210bc19
Show file tree
Hide file tree
Showing 15 changed files with 627 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cmd/build_manifest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"github.com/cppforlife/go-patch/patch"

boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
Expand All @@ -20,6 +22,13 @@ func (c BuildManifestCmd) Run(opts BuildManifestOpts) error {
ops := opts.OpsFlags.AsOps()
evalOpts := boshtpl.EvaluateOpts{ExpectAllKeys: opts.VarErrors}

if opts.Path != nil {
ops = patch.Ops{ops, patch.FindOp{Path: *opts.Path}}

// Printing YAML indented multiline strings (eg SSH key) is not useful
evalOpts.UnescapedMultiline = true
}

bytes, err := tpl.Evaluate(vars, ops, evalOpts)
if err != nil {
return err
Expand Down
55 changes: 55 additions & 0 deletions cmd/build_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,61 @@ var _ = Describe("BuildManifestCmd", func() {
Expect(ui.Blocks).To(Equal([]string{bytes}))
})

It("returns portion of the template if out path is given", func() {
opts.Args.Manifest = FileBytesArg{
Bytes: []byte("name1: ((name1))\nname2: ((name2))"),
}

opts.VarKVs = []boshtpl.VarKV{
{Name: "name1", Value: "val1-from-kv"},
}

opts.VarsFiles = []boshtpl.VarsFileArg{
{Vars: boshtpl.Variables(map[string]interface{}{"var": "var-val"})},
}

opts.OpsFiles = []OpsFileArg{
{
Ops: patch.Ops([]patch.Op{
patch.ReplaceOp{Path: patch.MustNewPointerFromString("/name2"), Value: "((var))"},
}),
},
}

ptr := patch.MustNewPointerFromString("/name2")
opts.Path = &ptr

err := act()
Expect(err).ToNot(HaveOccurred())
Expect(ui.Blocks).To(Equal([]string{"var-val\n"}))
})

It("returns portion of the template formatting multiline string without YAML indent", func() {
opts.Args.Manifest = FileBytesArg{
Bytes: []byte(`key: "line1\nline2"`),
}

ptr := patch.MustNewPointerFromString("/key")
opts.Path = &ptr

err := act()
Expect(err).ToNot(HaveOccurred())
Expect(ui.Blocks).To(Equal([]string{"line1\nline2\n"}))
})

It("returns portion of the template formatting result as regular YAML", func() {
opts.Args.Manifest = FileBytesArg{
Bytes: []byte("key:\n subkey:\n subsubkey: key"),
}

ptr := patch.MustNewPointerFromString("/key")
opts.Path = &ptr

err := act()
Expect(err).ToNot(HaveOccurred())
Expect(ui.Blocks).To(Equal([]string{"subkey:\n subsubkey: key\n"}))
})

It("returns error if variables are not found in templated manifest if var-errors flag is set", func() {
opts.Args.Manifest = FileBytesArg{
Bytes: []byte("name1: ((name1))\nname2: ((name2))"),
Expand Down
1 change: 1 addition & 0 deletions cmd/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ var _ = Describe("Factory", func() {
boshOpts.ExportRelease = ExportReleaseOpts{}
boshOpts.RunErrand = RunErrandOpts{}
boshOpts.Logs = LogsOpts{}
boshOpts.BuildManifest = BuildManifestOpts{}
boshOpts.InitRelease = InitReleaseOpts{}
boshOpts.ResetRelease = ResetReleaseOpts{}
boshOpts.GenerateJob = GenerateJobOpts{}
Expand Down
8 changes: 7 additions & 1 deletion cmd/opts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"github.com/cppforlife/go-patch/patch"

boshdir "github.com/cloudfoundry/bosh-cli/director"
boshrel "github.com/cloudfoundry/bosh-cli/release"
)
Expand Down Expand Up @@ -272,9 +274,13 @@ type AttachDiskArgs struct {

type BuildManifestOpts struct {
Args BuildManifestArgs `positional-args:"true" required:"true"`

VarFlags
OpsFlags
VarErrors bool `long:"var-errs" description:"Expect all variables to be found, otherwise error"`

Path *patch.Pointer `long:"path" value-name:"OP-PATH" description:"Extract value out of template (e.g.: /private_key)"`
VarErrors bool `long:"var-errs" description:"Expect all variables to be found, otherwise error"`

cmd
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,14 @@ var _ = Describe("Opts", func() {
})
})

Describe("Path", func() {
It("contains desired values", func() {
Expect(getStructTagForName("Path", opts)).To(Equal(
`long:"path" value-name:"OP-PATH" description:"Extract value out of template (e.g.: /private_key)"`,
))
})
})

Describe("VarErrors", func() {
It("contains desired values", func() {
Expect(getStructTagForName("VarErrors", opts)).To(Equal(
Expand Down
2 changes: 1 addition & 1 deletion deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ github.com/jmespath/go-jmespath:0b12d6b
github.com/charlievieth/fs/...:1c3b3f1
github.com/hashicorp/errwrap/...:7554cd9
github.com/bmatcuk/doublestar/...:044df0a
github.com/cppforlife/go-patch/patch:661a67c
github.com/cppforlife/go-patch/patch:a15438b
github.com/pivotal-golang/s3cli:90561c0
github.com/golang/protobuf/proto:98fa357
github.com/aws/aws-sdk-go:1a651d9
Expand Down
9 changes: 8 additions & 1 deletion director/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Template struct {
}

type EvaluateOpts struct {
ExpectAllKeys bool
ExpectAllKeys bool
UnescapedMultiline bool
}

func NewTemplate(bytes []byte) Template {
Expand Down Expand Up @@ -53,6 +54,12 @@ func (t Template) Evaluate(vars Variables, ops patch.Ops, opts EvaluateOpts) ([]
return []byte{}, fmt.Errorf("Expected to find variables: %s", strings.Join(missingVarKeys, ", "))
}

if opts.UnescapedMultiline {
if _, ok := obj.(string); ok {
return []byte(fmt.Sprintf("%s\n", obj)), nil
}
}

bytes, err := yaml.Marshal(obj)
if err != nil {
return []byte{}, err
Expand Down
12 changes: 12 additions & 0 deletions director/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,16 @@ array:
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Expected to find a map key 'x' for path '/x'"))
})

It("returns raw bytes of a string if UnescapedMultiline is true", func() {
template := NewTemplate([]byte("value"))

result, err := template.Evaluate(Variables{}, patch.Ops{}, EvaluateOpts{})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("value\n")))

result, err = template.Evaluate(Variables{}, patch.Ops{}, EvaluateOpts{UnescapedMultiline: true})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("value\n")))
})
})
122 changes: 122 additions & 0 deletions vendor/github.com/cppforlife/go-patch/patch/find_op.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 210bc19

Please sign in to comment.