Skip to content

Commit

Permalink
consolidate all cmd const in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
liangrog committed Oct 3, 2019
1 parent 1ead4db commit eadc43c
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
cobra.OnInitialize(initConfig)

Cmds.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("config file (default is $HOME/%s%s)", cfgFileName, cfgFileExtension))
Cmds.PersistentFlags().StringP("output", "o", "json", "output type. Default to json. Use 'yaml' for yaml output.")
Cmds.PersistentFlags().StringP(CMD_ROOT_OUTPUT, "o", "json", "output type. Default to json. Use 'yaml' for yaml output.")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
74 changes: 74 additions & 0 deletions cmd/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

const (
// Root
CMD_ROOT_OUTPUT = "output"

// Valut

// Environment variable name for vault password.
ENV_VAULT_PASSWORD = "CFCTL_VAULT_PASSWORD"

// Environment variable name for vault password file.
ENV_VAULT_PASSWORD_FILE = "CFCTL_VAULT_PASSWORD_FILE"

// Command line flag for vault password.
CMD_VAULT_PASSWORD = "vault-password"

// Command line flag for vault password file.
CMD_VAULT_PASSWORD_FILE = "vault-password-file"

// Default vault password file name.
DEFAULT_VAULT_PASSWORD_FILE = ".cfctl_vault_password"

// S3

// Command line flag for bucket.
CMD_S3_UPLOAD_BUCKET = "bucket"

// Command line flag for bucket prefix.
CMD_S3_UPLOAD_PREFIX = "prefix"

// Command line flag for recursive.
CMD_S3_UPLOAD_RECURSIVE = "recursive"

// Command line flag for excluding files.
CMD_S3_UPLOAD_EXCLUDE_FILES = "exclude-files"

// Stack

// Command line flag for stacks.
CMD_STACK_DEPLOY_STACK = "stack"

// Command line flag for configuration file.
CMD_STACK_DEPLOY_FILE = "file"

// Command line flag for dry run.
CMD_STACK_DEPLOY_DRY_RUN = "dry-run"

// Command line flag for envoirnment folder.
CMD_STACK_DEPLOY_ENV = "env"

// Parameter parsing.
CMD_STACK_DEPLOY_PARAM_ONLY = "param-only"

// Variable override
CMD_STACK_DEPLOY_VARS = "vars"

// Default environment folder name.
STACK_DEPLOY_ENV_DEFAULT_FOLDER = "default"

// Command line flag for stack delete all.
CMD_STACK_DELETE_ALL = "all"

// Command line flag for stack get name.
CMD_STACK_GET_NAME = "name"

// Command line flag for stack list status.
CMD_STACK_LIST_STATUS = "status"

// Template

// Command line flag for template validate recursively.
CMD_TEMPLATE_VALIDATE_RECURSIVE = "recursive"
)
16 changes: 8 additions & 8 deletions cmd/s3-upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func init() {
}

func addFlagsS3Upload(cmd *cobra.Command) {
cmd.Flags().String("bucket", "", "S3 bucket name")
cmd.Flags().String("prefix", "", "The path prefix for S3 bucket that the objects will be uploaded to")
cmd.Flags().BoolP("recursive", "r", false, "Recursively travel the given directory for all objects")
cmd.Flags().String("exclude-files", "", "Exclude files with matching file names from upload. Multiple file names seperate by comma")
cmd.Flags().String(CMD_S3_UPLOAD_BUCKET, "", "S3 bucket name")
cmd.Flags().String(CMD_S3_UPLOAD_PREFIX, "", "The path prefix for S3 bucket that the objects will be uploaded to")
cmd.Flags().BoolP(CMD_S3_UPLOAD_RECURSIVE, "r", false, "Recursively travel the given directory for all objects")
cmd.Flags().String(CMD_S3_UPLOAD_EXCLUDE_FILES, "", "Exclude files with matching file names from upload. Multiple file names seperate by comma")
}

// cmd: upload
Expand All @@ -45,15 +45,15 @@ func getCmdS3Upload() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
recursive, _ := cmd.Flags().GetBool("recursive")
recursive, _ := cmd.Flags().GetBool(CMD_S3_UPLOAD_RECURSIVE)

for _, arg := range args {
err := s3Upload(
arg,
cmd.Flags().Lookup("bucket").Value.String(),
cmd.Flags().Lookup("prefix").Value.String(),
cmd.Flags().Lookup(CMD_S3_UPLOAD_BUCKET).Value.String(),
cmd.Flags().Lookup(CMD_S3_UPLOAD_PREFIX).Value.String(),
recursive,
cmd.Flags().Lookup("exclude-files").Value.String(),
cmd.Flags().Lookup(CMD_S3_UPLOAD_EXCLUDE_FILES).Value.String(),
)

silenceUsageOnError(cmd, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/stack-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
}

func addFlagsStackDelete(cmd *cobra.Command) {
cmd.Flags().BoolP("all", "", false, "Delete all the stacks in the stack configuration file")
cmd.Flags().BoolP(CMD_STACK_DELETE_ALL, "", false, "Delete all the stacks in the stack configuration file")
cmd.Flags().String(CMD_STACK_DEPLOY_FILE, "", "Alternative stack configuration file (Default is './stacks.yaml')")
}

Expand All @@ -31,7 +31,7 @@ func getCmdStackDelete() *cobra.Command {
Short: "delete cloudformation stacks",
Long: `delete cloudformation stacks`,
Args: func(cmd *cobra.Command, args []string) error {
all, _ := cmd.Flags().GetBool("all")
all, _ := cmd.Flags().GetBool(CMD_STACK_DELETE_ALL)

if !all && len(args) < 1 {
return errors.New("Minimum one stack name is required")
Expand All @@ -40,7 +40,7 @@ func getCmdStackDelete() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
all, _ := cmd.Flags().GetBool("all")
all, _ := cmd.Flags().GetBool(CMD_STACK_DELETE_ALL)
err := stackDelete(
args,
all,
Expand Down
25 changes: 1 addition & 24 deletions cmd/stack-deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,6 @@ import (
"gopkg.in/yaml.v2"
)

const (
// Command line flag for stacks.
CMD_STACK_DEPLOY_STACK = "stack"

// Command line flag for configuration file.
CMD_STACK_DEPLOY_FILE = "file"

// Command line flag for dry run.
CMD_STACK_DEPLOY_DRY_RUN = "dry-run"

// Command line flag for envoirnment folder.
CMD_STACK_DEPLOY_ENV = "env"

// Parameter parsing
CMD_STACK_DEPLOY_PARAM_ONLY = "param-only"

// Variable override
CMD_STACK_DEPLOY_VARS = "vars"

// Default environment folder name
STACK_DEPLOY_ENV_DEFAULT_FOLDER = "default"
)

// Register sub commands.
func init() {
cmd := getCmdStackDeploy()
Expand Down Expand Up @@ -89,7 +66,7 @@ func getCmdStackDeploy() *cobra.Command {
passes,
dryRun,
paramOnly,
cmd.Flags().Lookup("output").Value.String(),
cmd.Flags().Lookup(CMD_ROOT_OUTPUT).Value.String(),
cmd.Flags().Lookup(CMD_STACK_DEPLOY_VARS).Value.String(),
)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/stack-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

func addFlagsStackGet(cmd *cobra.Command) {
cmd.Flags().String("name", "n", "Get stack's details for given stack name")
cmd.Flags().String(CMD_STACK_GET_NAME, "n", "Get stack's details for given stack name")
}

// cmd: get
Expand All @@ -31,8 +31,8 @@ func getCmdStackGet() *cobra.Command {
only return the detail for that stack`,
RunE: func(cmd *cobra.Command, args []string) error {
err := stackGet(
cmd.Flags().Lookup("output").Value.String(),
cmd.Flags().Lookup("name").Value.String(),
cmd.Flags().Lookup(CMD_ROOT_OUTPUT).Value.String(),
cmd.Flags().Lookup(CMD_STACK_GET_NAME).Value.String(),
)

silenceUsageOnError(cmd, err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/stack-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
}

func addFlagsStackList(cmd *cobra.Command) {
cmd.Flags().String("status", "s", "cloudformation status filter, multiple values seperate by ','. Allowed values 'REVIEW_IN_PROGRESS, CREATE_FAILED, UPDATE_ROLLBACK_FAILED, UPDATE_ROLLBACK_IN_PROGRESS, CREATE_IN_PROGRESS, UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS, ROLLBACK_IN_PROGRESS, DELETE_COMPLETE, UPDATE_COMPLETE, UPDATE_IN_PROGRESS, DELETE_FAILED, DELETE_IN_PROGRESS, ROLLBACK_COMPLETE, ROLLBACK_FAILED, UPDATE_COMPLETE_CLEANUP_IN_PROGRESS, CREATE_COMPLETE, UPDATE_ROLLBACK_COMPLETE'")
cmd.Flags().String(CMD_STACK_LIST_STATUS, "s", "cloudformation status filter, multiple values seperate by ','. Allowed values 'REVIEW_IN_PROGRESS, CREATE_FAILED, UPDATE_ROLLBACK_FAILED, UPDATE_ROLLBACK_IN_PROGRESS, CREATE_IN_PROGRESS, UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS, ROLLBACK_IN_PROGRESS, DELETE_COMPLETE, UPDATE_COMPLETE, UPDATE_IN_PROGRESS, DELETE_FAILED, DELETE_IN_PROGRESS, ROLLBACK_COMPLETE, ROLLBACK_FAILED, UPDATE_COMPLETE_CLEANUP_IN_PROGRESS, CREATE_COMPLETE, UPDATE_ROLLBACK_COMPLETE'")
}

// cmd: list
Expand All @@ -31,14 +31,14 @@ func getCmdStackList() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
var status []string

if cmd.Flags().Changed("status") {
if cmd.Flags().Changed(CMD_STACK_LIST_STATUS) {
status = strings.Split(
cmd.Flags().Lookup("status").Value.String(),
cmd.Flags().Lookup(CMD_STACK_LIST_STATUS).Value.String(),
",",
)
}

err := listStacks(cmd.Flags().Lookup("output").Value.String(), status...)
err := listStacks(cmd.Flags().Lookup(CMD_ROOT_OUTPUT).Value.String(), status...)

silenceUsageOnError(cmd, err)

Expand Down
6 changes: 3 additions & 3 deletions cmd/template-validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
}

func addFlagsTemplateValidate(cmd *cobra.Command) {
cmd.Flags().BoolP("recursive", "r", true, "Recursively validate templates for given path")
cmd.Flags().BoolP(CMD_TEMPLATE_VALIDATE_RECURSIVE, "r", true, "Recursively validate templates for given path")
}

// cmd: validate
Expand All @@ -38,10 +38,10 @@ This command can be run recursively by using '-r' option`,
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
recursive, _ := cmd.Flags().GetBool("recursive")
recursive, _ := cmd.Flags().GetBool(CMD_TEMPLATE_VALIDATE_RECURSIVE)

err := templateValidate(
cmd.Flags().Lookup("output").Value.String(),
cmd.Flags().Lookup(CMD_ROOT_OUTPUT).Value.String(),
args,
recursive,
)
Expand Down
10 changes: 0 additions & 10 deletions cmd/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ import (

var CmdVault = getCmdVault()

const (
ENV_VAULT_PASSWORD = "CFCTL_VAULT_PASSWORD"
ENV_VAULT_PASSWORD_FILE = "CFCTL_VAULT_PASSWORD_FILE"

CMD_VAULT_PASSWORD = "vault-password"
CMD_VAULT_PASSWORD_FILE = "vault-password-file"

DEFAULT_VAULT_PASSWORD_FILE = ".cfctl_vault_password"
)

// Register sub commands
func init() {
addFlagsVault(CmdVault)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ require (
github.com/google/uuid v1.1.1
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/liangrog/ds v0.0.0-20190409010058-ef878b422520
github.com/liangrog/vault v0.0.0-20190211035740-3b79a35232c5
github.com/liangrog/vault v0.0.0-20191003051523-b14798d78aaf
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 // indirect
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc
gopkg.in/yaml.v2 v2.2.2
)

go 1.13
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ github.com/liangrog/ds v0.0.0-20190409010058-ef878b422520 h1:+aQWK33wp7KA6QEKh9M
github.com/liangrog/ds v0.0.0-20190409010058-ef878b422520/go.mod h1:QP7T2jM9h6RvyPzHeeKRuw/phXqiFnMQgkSN8XLSLeA=
github.com/liangrog/vault v0.0.0-20190211035740-3b79a35232c5 h1:JrG4a9323S/N0giRhHAafvoewIJEjN4tsL0d9H4gqi0=
github.com/liangrog/vault v0.0.0-20190211035740-3b79a35232c5/go.mod h1:KLUwdzlxM0iwmKQx9oFsMmPc1hnTPMsW62BLT03qjb4=
github.com/liangrog/vault v0.0.0-20190211040256-47f19a596a8b h1:YPnN6IA1ajdTSINed90OptVt77IlMzx9ewdJXaYalWM=
github.com/liangrog/vault v0.0.0-20190211040256-47f19a596a8b/go.mod h1:KLUwdzlxM0iwmKQx9oFsMmPc1hnTPMsW62BLT03qjb4=
github.com/liangrog/vault v0.0.0-20191003051523-b14798d78aaf h1:x4DAV3f9lkA9X4p3g+YSmVoH0I9M6cPq7nzrMhhrDBo=
github.com/liangrog/vault v0.0.0-20191003051523-b14798d78aaf/go.mod h1:KLUwdzlxM0iwmKQx9oFsMmPc1hnTPMsW62BLT03qjb4=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
Expand Down Expand Up @@ -54,10 +58,17 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 h1:MQ/ZZiDsUapFFiMS+vzwXkCTeEKaum+Do5rINYJDmxc=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc h1:c0o/qxkaO2LF5t6fQrT4b5hzyggAkLLlCUjqfRxd8Q4=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 h1:bfLnR+k0tq5Lqt6dflRLcZiz6UaXCMt3vhYJ1l4FQ80=
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
4 changes: 4 additions & 0 deletions pkg/conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conf

import (
"io/ioutil"
"os"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -58,4 +59,7 @@ func TestLoadVaules(t *testing.T) {
v, ok = result["secretVaule"]
assert.True(t, ok)
assert.Equal(t, "EFGH", v)

// Clean up
os.RemoveAll(tmpDir)
}

0 comments on commit eadc43c

Please sign in to comment.