Skip to content

Commit

Permalink
chore: prompt secrets with correct values
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Oct 3, 2022
1 parent fe0c0e4 commit 59ef5af
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
35 changes: 28 additions & 7 deletions cmd/run.go → cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ 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")
}

configsToDeploy := []store.ConfigInput{}

// prompt for missing secrets
if prompt == "missing" {
for _, c := range missing {
if c.Value == "" {
Expand All @@ -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)
}
}
}
Expand All @@ -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
}

Expand All @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -76,8 +82,6 @@ func list(cmd *cobra.Command, args []string) error {
}

w.Flush()

return nil
}

type ByName []store.Config
Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 0 additions & 1 deletion example/safebox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ config:
secret:
API_KEY: "key of the api endpoint"
DB_SECRET: "database secret"
TEST: "database secret"

0 comments on commit 59ef5af

Please sign in to comment.