Skip to content

Commit

Permalink
Add command to generate configmaps using environment variables (#24)
Browse files Browse the repository at this point in the history
* generate configmap

* sorted

* fixes

* only yaml v3

* tudo v2

* Use yaml.v2 in some places to avoid unit tests breaking

* Implement unit test
  • Loading branch information
mniak committed Oct 4, 2021
1 parent f29886e commit fca8cde
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 86 deletions.
51 changes: 51 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"

"github.com/BraspagDevelopers/bpdt/lib"
"github.com/spf13/cobra"
)

var generateCmd = &cobra.Command{
Use: "generate",
Aliases: []string{"gen"},
}

var generateConfigMapCmd = &cobra.Command{
Use: "configmap <name-on-manifest>",
Short: "Generate a ConfigMap manifest",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name := args[0]

fromEnv, err := cmd.Flags().GetBool("env")
handleError(err)

prefix, err := cmd.Flags().GetString("prefix")
handleError(err)

stripPrefix, err := cmd.Flags().GetBool("strip-prefix")
handleError(err)

configMap, err := lib.GenerateConfigMap(lib.GenerateConfigMapParams{
Name: name,
FromEnvironment: fromEnv,
Prefix: prefix,
StripPrefix: stripPrefix,
})
handleError(err)

fmt.Println(configMap)
},
}

func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.AddCommand(generateConfigMapCmd)

generateConfigMapCmd.Flags().Bool("env", false, "Load variables from environment")
generateConfigMapCmd.Flags().Bool("strip-prefix", false, "Strip the variable name prefix")

generateConfigMapCmd.Flags().String("prefix", "", "Filter the variables by this prefix")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/beevik/etree v1.1.0
github.com/brianvoe/gofakeit/v6 v6.9.0
github.com/bxcodec/faker/v3 v3.6.0
github.com/dimchansky/utfbom v1.1.1
github.com/jeremywohl/flatten v1.0.1
Expand All @@ -12,6 +13,8 @@ require (
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gotest.tools v2.2.0+incompatible
muzzammil.xyz/jsonc v0.0.0-20201229145248-615b0916ca38
)

0 comments on commit fca8cde

Please sign in to comment.