Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
Overrides via command line parameters (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Yarshevich <[email protected]>
  • Loading branch information
ghen authored Jun 12, 2023
1 parent 7e959b2 commit a144c49
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/score-spec/score-go v0.0.0-20221019054335-3510902b5f8b
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.0
github.com/tidwall/sjson v1.2.5
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -17,5 +18,8 @@ require (
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.14.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
49 changes: 46 additions & 3 deletions internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ The Apache Software Foundation (http://www.apache.org/).
package command

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"

"github.com/imdario/mergo"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"github.com/tidwall/sjson"
"gopkg.in/yaml.v3"

loader "github.com/score-spec/score-go/loader"
score "github.com/score-spec/score-go/types"
helm "github.com/score-spec/score-helm/internal/helm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

const (
Expand All @@ -33,6 +37,8 @@ var (
outFile string
valuesFile string

overrideParams []string

verbose bool
)

Expand All @@ -42,6 +48,8 @@ func init() {
runCmd.Flags().StringVarP(&valuesFile, "values", "", "", "Imported values file (in YAML format)")
runCmd.Flags().StringVarP(&outFile, "output", "o", "", "Output file")

runCmd.Flags().StringArrayVarP(&overrideParams, "property", "p", nil, "Overrides selected property value")

runCmd.Flags().BoolVar(&verbose, "verbose", false, "Enable diagnostic messages (written to STDERR)")

rootCmd.AddCommand(runCmd)
Expand Down Expand Up @@ -76,7 +84,7 @@ func run(cmd *cobra.Command, args []string) error {
return err
}

// Apply overrides (optional)
// Apply overrides from file (optional)
//
if overridesFile != "" {
log.Printf("Checking '%s'...\n", overridesFile)
Expand All @@ -96,6 +104,41 @@ func run(cmd *cobra.Command, args []string) error {
}
}

// Apply overrides from command line (optional)
//
for _, pstr := range overrideParams {
log.Print("Applying SCORE properties overrides...\n")

jsonBytes, err := json.Marshal(srcMap)
if err != nil {
return fmt.Errorf("marshalling score spec: %w", err)
}

pmap := strings.SplitN(pstr, "=", 2)
if len(pmap) <= 1 {
var path = pmap[0]
log.Printf("removing '%s'", path)
if jsonBytes, err = sjson.DeleteBytes(jsonBytes, path); err != nil {
return fmt.Errorf("removing '%s': %w", path, err)
}
} else {
var path = pmap[0]
var val interface{}
if err := yaml.Unmarshal([]byte(pmap[1]), &val); err != nil {
val = pmap[1]
}

log.Printf("overriding '%s' = '%s'", path, val)
if jsonBytes, err = sjson.SetBytes(jsonBytes, path, val); err != nil {
return fmt.Errorf("overriding '%s': %w", path, err)
}
}

if err = json.Unmarshal(jsonBytes, &srcMap); err != nil {
return fmt.Errorf("unmarshalling score spec: %w", err)
}
}

// Load imported values (optional)
//
var values = make(map[string]interface{})
Expand Down

0 comments on commit a144c49

Please sign in to comment.