From 59ef5af4153ea4757b742b77d24a041cb7096653 Mon Sep 17 00:00:00 2001 From: subash Date: Mon, 3 Oct 2022 11:07:58 +1100 Subject: [PATCH] chore: prompt secrets with correct values --- cmd/{run.go => deploy.go} | 35 ++++++++++++++++++++++++++++------- cmd/export.go | 2 +- cmd/list.go | 10 +++++++--- config/config.go | 8 ++++++-- config/config_test.go | 12 ++++++++++++ example/safebox.yml | 1 - 6 files changed, 54 insertions(+), 14 deletions(-) rename cmd/{run.go => deploy.go} (75%) create mode 100644 config/config_test.go diff --git a/cmd/run.go b/cmd/deploy.go similarity index 75% rename from cmd/run.go rename to cmd/deploy.go index cb0fc3c..17e809d 100644 --- a/cmd/run.go +++ b/cmd/deploy.go @@ -49,8 +49,9 @@ func deploy(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "failed to instantiate store") } - all, _ := st.GetMany(config.Configs) - missing := getMissing(config.Configs, all) + all, _ := st.GetMany(config.All) + + missing := getMissing(config.Secrets, all) if len(missing) > 0 && prompt == "" { return errors.New("config values missing. run deploy with \"--prompt\" flag") @@ -58,6 +59,7 @@ func deploy(cmd *cobra.Command, args []string) error { configsToDeploy := []store.ConfigInput{} + // prompt for missing secrets if prompt == "missing" { for _, c := range missing { if c.Value == "" { @@ -66,13 +68,30 @@ func deploy(cmd *cobra.Command, args []string) error { } } + // prompt for all secrets and provide existing value as default if prompt == "all" { - for _, c := range config.Configs { - if c.Secret { - for _, a := range all { + for _, c := range config.Secrets { + var existingValue string + for _, a := range all { + if c.Name == *a.Name { + existingValue = *a.Value c.Value = *a.Value } - configsToDeploy = append(configsToDeploy, promptConfig(c)) + } + + userInput := promptConfig(c) + + if userInput.Value != existingValue { + configsToDeploy = append(configsToDeploy, userInput) + } + } + } + + // filter configs with changed values + for _, c := range config.Configs { + for _, a := range all { + if c.Name == *a.Name && c.Value != *a.Value { + configsToDeploy = append(configsToDeploy, c) } } } @@ -83,6 +102,8 @@ func deploy(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "failed to write param") } + fmt.Printf("%d new configs deployed", len(configsToDeploy)) + return nil } @@ -97,7 +118,7 @@ func promptConfig(config store.ConfigInput) store.ConfigInput { log.Printf("value %s", config.Value) prompt := promptui.Prompt{ - Label: config.Name, + Label: config.Key(), Validate: validate, Default: config.Value, } diff --git a/cmd/export.go b/cmd/export.go index 55d0001..115d5a5 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -52,7 +52,7 @@ func export(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "failed to instantiate store") } - toExport, err := configsToExport(config.Configs) + toExport, err := configsToExport(config.All) if err != nil { return err diff --git a/cmd/list.go b/cmd/list.go index 496ec31..aeab64e 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -44,7 +44,7 @@ func list(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "failed to instantiate store") } - configs, err := store.GetMany(config.Configs) + configs, err := store.GetMany(config.All) if err != nil { return errors.Wrap(err, "failed to list params") @@ -58,6 +58,12 @@ func list(cmd *cobra.Command, args []string) error { sort.Sort(ByName(configs)) } + printList(configs) + + return nil +} + +func printList(configs []store.Config) { w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) fmt.Fprint(w, "Name\tValue\tType\tVersion\tLastModified") @@ -76,8 +82,6 @@ func list(cmd *cobra.Command, args []string) error { } w.Flush() - - return nil } type ByName []store.Config diff --git a/config/config.go b/config/config.go index 064bd6d..8a45a56 100644 --- a/config/config.go +++ b/config/config.go @@ -19,7 +19,9 @@ type rawConfig struct { type Config struct { Provider string Service string + All []store.ConfigInput Configs []store.ConfigInput + Secrets []store.ConfigInput } type LoadParam struct { @@ -88,12 +90,14 @@ func parseConfig(rc rawConfig, c *Config, param LoadParam) { } for key, value := range rc.Secret { - c.Configs = append(c.Configs, store.ConfigInput{ - Name: key, + c.Secrets = append(c.Secrets, store.ConfigInput{ + Name: formatPath(param.Stage, c.Service, key), Description: value, Secret: true, }) } + + c.All = append(c.Secrets, c.Configs...) } func formatSharedPath(stage string, key string) string { diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..f000ccd --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,12 @@ +package config + +import "testing" + +func Test_LoadConfig_InvalidPath(t *testing.T) { + _, err := Load(LoadParam{Path: "invalid file"}) + expected := "missing safebox config file invalid file" + + if err.Error() != expected { + t.Errorf("Error actual = %v, and Expected = %v", err, expected) + } +} diff --git a/example/safebox.yml b/example/safebox.yml index a58a336..9339754 100644 --- a/example/safebox.yml +++ b/example/safebox.yml @@ -20,4 +20,3 @@ config: secret: API_KEY: "key of the api endpoint" DB_SECRET: "database secret" - TEST: "database secret"