Skip to content

Commit

Permalink
Merge pull request #3 from kevinglasson/support-multiple-import-formats
Browse files Browse the repository at this point in the history
Enable the additional parsers to support multiple import formats.
  • Loading branch information
elidhu authored Sep 29, 2020
2 parents 7376e77 + 71441c2 commit 7e11609
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
62 changes: 54 additions & 8 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,48 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
kdotenv "github.com/knadh/koanf/parsers/dotenv"
kjson "github.com/knadh/koanf/parsers/json"
ktoml "github.com/knadh/koanf/parsers/toml"
kyaml "github.com/knadh/koanf/parsers/yaml"
kfile "github.com/knadh/koanf/providers/file"
"github.com/spf13/cobra"
)

type unmarshaller interface {
Unmarshal(b []byte) (map[string]interface{}, error)
}

var (
// For flags
file string
importType string
importPath string
importOWrite bool
format string

// importCmd represents the import command
importCmd = &cobra.Command{
Use: "import",
Short: "Import parameters from a file",
Run: func(cmd *cobra.Command, args []string) {
err := importParameters(file, importType, importPath, importOWrite)

// Define the map of string to parsers
parsers := map[string]unmarshaller{
"json": kjson.Parser(),
"toml": ktoml.Parser(),
"yaml": kyaml.Parser(),
"dotenv": kdotenv.Parser(),
}

// Check the format arg is for a supported format
err := isValidFormat(format)
if err != nil {
utils.PrintErrorAndExit(err)
}

parser := parsers[format]

err = importParameters(file, importType, importPath, importOWrite, parser)
if err != nil {
utils.PrintErrorAndExit(err)
}
Expand All @@ -37,25 +62,32 @@ func init() {
rootCmd.AddCommand(importCmd)

importCmd.Flags().StringVarP(
&file, "file", "f", "", "path to the file",
&file, "file", "f", "", "path to the file to import",
)
importCmd.MarkFlagRequired("file")

importCmd.Flags().StringVarP(
&importType, "type", "t", "", "parameter type",
&importType, "type", "t", "", "aws parameter type to use when importing",
)
importCmd.MarkFlagRequired("type")

importCmd.Flags().StringVarP(
&importPath, "path", "p", "", "parameter path",
&importPath, "path", "p", "", "base path to import the parameters to",
)
importCmd.MarkFlagRequired("type")
importCmd.MarkFlagRequired("path")

importCmd.Flags().BoolVarP(
&importOWrite, "overwrite", "o", false, "overwrite parameters if they exist",
&importOWrite, "overwrite", "o", false, "overwrite parameters if they exist, i.e. update them",
)

importCmd.Flags().StringVarP(
&format, "format", "", "dotenv", "file format. [json toml yaml dotenv]",
)
}

func importParameters(file string, typ string, path string, overwrite bool) error {
func importParameters(file string, typ string, path string, overwrite bool, parser unmarshaller) error {

provider := kfile.Provider(file)
parser := kdotenv.Parser()

// Get the raw bytest from the provider
b, err := provider.ReadBytes()
Expand Down Expand Up @@ -103,3 +135,17 @@ func importParameters(file string, typ string, path string, overwrite bool) erro

return nil
}

func isValidFormat(format string) error {
// Supported formats
supported := []string{"json", "toml", "yaml", "dotenv"}

for _, v := range supported {
if v == format {
return nil
}
}
return fmt.Errorf(
"Unsupported input file format `%s`. Supported formats are: %v", format, supported,
)
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down

0 comments on commit 7e11609

Please sign in to comment.