Skip to content

Commit

Permalink
only use product-version and product-name when parsing YAML for stage…
Browse files Browse the repository at this point in the history
…-product

Signed-off-by: Kira Boyle <[email protected]>
  • Loading branch information
jtarchie authored and Kira Boyle committed Oct 7, 2020
1 parent 762180b commit 4b2e9c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ can be found in [Pivotal Documentation](docs.pivotal.io/platform-automation).
### Features
- When using `stage-product`, the `--product-version` can use the placeholder `latest`.
This finds the highest semvered available product (of `--product-name`) version to stage.
- When using `stage-product` the config file provided can be _any_ config file,
just as long as it has `product-version` and `product-name`.

### Bug Fixes
- tl;dr: Collections are hard.
Expand Down
40 changes: 34 additions & 6 deletions commands/stage_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package commands

import (
"fmt"
"os"

"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"gopkg.in/yaml.v2"
"io/ioutil"
)

type StageProduct struct {
Expand All @@ -14,8 +14,8 @@ type StageProduct struct {
Options struct {
interpolateConfigFileOptions

Product string `long:"product-name" short:"p" required:"true" description:"name of product"`
Version string `long:"product-version" required:"true" description:"version of product"`
Product string `yaml:"product-name" long:"product-name" short:"p" required:"true" description:"name of product"`
Version string `yaml:"product-version" long:"product-version" required:"true" description:"version of product"`
}
}

Expand All @@ -37,9 +37,9 @@ func NewStageProduct(service stageProductService, logger logger) StageProduct {
}

func (sp StageProduct) Execute(args []string) error {
err := loadConfigFile(args, &sp.Options, os.Environ)
err := sp.loadConfig(args)
if err != nil {
return fmt.Errorf("could not parse stage-product flags: %s", err)
return err
}

productName := sp.Options.Product
Expand Down Expand Up @@ -114,3 +114,31 @@ func (sp StageProduct) Usage() jhanda.Usage {
Flags: sp.Options,
}
}

func (sp *StageProduct) loadConfig(args []string) error {
_, err := jhanda.Parse(&sp.Options, args)

if sp.Options.ConfigFile == "" && err != nil {
return fmt.Errorf("could not parse stage-product flags: %s", err)
}

if sp.Options.ConfigFile != "" {
contents, err := ioutil.ReadFile(sp.Options.ConfigFile)
if err != nil {
return err
}

err = yaml.Unmarshal(contents, &sp.Options)
if err != nil {
return err
}
}

productName := sp.Options.Product
productVersion := sp.Options.Version
if productName == "" || productVersion == "" {
return fmt.Errorf("--product-name (%s) and --product-version (%s) are required", productName, productVersion)
}

return nil
}
4 changes: 3 additions & 1 deletion commands/stage_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ var _ = Describe("StageProduct", func() {
It("stages a product", func() {
config := `---
product-name: some-product
product-version: some-version`
product-version: some-version
unnecessary-field: ((some-value))
`

configFile, err := ioutil.TempFile("", "config.yml")
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 4b2e9c6

Please sign in to comment.