From 2cb24d191296839495f66f2ab1cf8c4018f3c11d Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 25 Sep 2023 12:19:19 -0400 Subject: [PATCH] creating secrets using -f --- src/cmd/input.go | 4 +++- src/cmd/secret.go | 36 ++++++++++++++---------------------- src/data.yaml | 3 +++ src/submodules/opslevel-go | 2 +- 4 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 src/data.yaml diff --git a/src/cmd/input.go b/src/cmd/input.go index 23e27c0a..8ee28690 100644 --- a/src/cmd/input.go +++ b/src/cmd/input.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "github.com/rs/zerolog/log" @@ -11,6 +12,8 @@ import ( var dataFile string func readInputConfig() { + viper.SetConfigType("yaml") + fmt.Printf("dataFile is %s\n", dataFile) switch dataFile { case ".": viper.SetConfigFile("./data.yaml") @@ -18,7 +21,6 @@ func readInputConfig() { if isStdInFromTerminal() { log.Info().Msg("Reading input directly from command line...") } - viper.SetConfigType("yaml") viper.ReadConfig(os.Stdin) default: viper.SetConfigFile(dataFile) diff --git a/src/cmd/secret.go b/src/cmd/secret.go index f808a995..01759bb8 100644 --- a/src/cmd/secret.go +++ b/src/cmd/secret.go @@ -3,17 +3,15 @@ package cmd import ( "encoding/json" "fmt" - "io" - "os" "github.com/creasty/defaults" "github.com/opslevel/opslevel-go/v2023" - "gopkg.in/yaml.v3" "github.com/opslevel/cli/common" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var secretAlias string @@ -24,11 +22,13 @@ var createSecretCmd = &cobra.Command{ Long: `Create a team-owned secret`, Example: ` cat << EOF | opslevel create secret --alias=my-secret-alias -f - -owner: "devs" +owner: + alias: "devs" value: "my-really-secure-secret-shhhh" EOF`, Run: func(cmd *cobra.Command, args []string) { input, err := readSecretInput() + fmt.Printf("%+v\n", input) cobra.CheckErr(err) newSecret, err := getClientGQL().CreateSecret(secretAlias, *input) cobra.CheckErr(err) @@ -85,10 +85,10 @@ var updateSecretCmd = &cobra.Command{ Long: `Update an OpsLevel secret`, Example: ` cat << EOF | opslevel update secret XXX_secret_id_XXX -f - - owner: "platform" + owner: + alias: "platform" value: "09sdf09werlkewlkjs0-9sdf - EOF - `, +EOF`, Args: cobra.ExactArgs(1), ArgAliases: []string{"ID"}, Run: func(cmd *cobra.Command, args []string) { @@ -102,8 +102,8 @@ var updateSecretCmd = &cobra.Command{ var deleteSecretCmd = &cobra.Command{ Use: "secret ID|ALIAS", - Short: "Delete a system", - Long: `Delete a system from OpsLevel`, + Short: "Delete a secret", + Long: `Delete a secret from OpsLevel`, Args: cobra.ExactArgs(1), ArgAliases: []string{"ID", "ALIAS"}, Run: func(cmd *cobra.Command, args []string) { @@ -115,21 +115,13 @@ var deleteSecretCmd = &cobra.Command{ } func readSecretInput() (*opslevel.SecretInput, error) { - file, err := io.ReadAll(os.Stdin) - cobra.CheckErr(err) - var evt struct { - Owner string `yaml:"owner"` - Value string `yaml:"value"` - } - cobra.CheckErr(yaml.Unmarshal(file, &evt)) - secretInput := &opslevel.SecretInput{} - if err := defaults.Set(secretInput); err != nil { + readInputConfig() + evt := &opslevel.SecretInput{} + viper.Unmarshal(&evt) + if err := defaults.Set(evt); err != nil { return nil, err } - - secretInput.Value = evt.Value - secretInput.Owner = *opslevel.NewIdentifier(evt.Owner) - return secretInput, nil + return evt, nil } func init() { diff --git a/src/data.yaml b/src/data.yaml new file mode 100644 index 00000000..79bf6a09 --- /dev/null +++ b/src/data.yaml @@ -0,0 +1,3 @@ +owner: + alias: "platform" +value: "hello world 3" diff --git a/src/submodules/opslevel-go b/src/submodules/opslevel-go index c305d102..36500528 160000 --- a/src/submodules/opslevel-go +++ b/src/submodules/opslevel-go @@ -1 +1 @@ -Subproject commit c305d1026fbe451be4488ebe33f95e2c10f5d0c9 +Subproject commit 365005282443a6a22ce338d2dfac5ca01c7427c3