Skip to content

Commit

Permalink
List secrets with last updated, input bugfix, create/update instructi…
Browse files Browse the repository at this point in the history
…ons update (#177)

* creating secrets using -f

* some debugging input

* checkout main for input stuff

* input enhancements

* forgot line

* suggestion

* minor fixes to doc/err check

* output updated at in list

* restore the submodule

* changieeee
  • Loading branch information
taimoor ahmad authored Sep 25, 2023
1 parent c958b53 commit cbddbba
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Bugfix-20230925-130703.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Bugfix
body: fix bug not reading files when using -f
time: 2023-09-25T13:07:03.642418-04:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20230925-130715.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: show last updated when listing secrets
time: 2023-09-25T13:07:15.386994-04:00
6 changes: 4 additions & 2 deletions src/cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
var dataFile string

func readInputConfig() {
viper.SetConfigType("yaml")
switch dataFile {
case ".":
// TODO: does this block ever actually ever run?
viper.SetConfigFile("./data.yaml")
case "-":
if isStdInFromTerminal() {
log.Info().Msg("Reading input directly from command line...")
// TODO: this can take up to half a second to output which interrupts the user's experience
log.Info().Msg("Reading input directly from command line... Press CTRL+D to stop typing")
}
viper.SetConfigType("yaml")
viper.ReadConfig(os.Stdin)
default:
viper.SetConfigFile(dataFile)
Expand Down
44 changes: 18 additions & 26 deletions src/cmd/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +22,8 @@ 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) {
Expand Down Expand Up @@ -70,9 +69,9 @@ var listSecretsCmd = &cobra.Command{
if isJsonOutput() {
common.JsonPrint(json.MarshalIndent(list, "", " "))
} else {
w := common.NewTabWriter("ALIAS", "ID", "OWNER")
w := common.NewTabWriter("ALIAS", "ID", "OWNER", "UPDATED_AT")
for _, item := range list {
fmt.Fprintf(w, "%s\t%s\t%s\t\n", item.Alias, item.ID, item.Owner.Alias)
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", item.Alias, item.ID, item.Owner.Alias, item.Timestamps.UpdatedAt.Time)
}
w.Flush()
}
Expand All @@ -84,16 +83,17 @@ var updateSecretCmd = &cobra.Command{
Short: "Update an OpsLevel secret",
Long: `Update an OpsLevel secret`,
Example: `
cat << EOF | opslevel update secret XXX_secret_id_XXX -f -
owner: "platform"
value: "09sdf09werlkewlkjs0-9sdf
EOF
`,
cat << EOF | opslevel update secret XXX_secret_id_XXX -f -
owner:
alias: "platform"
value: "09sdf09werlkewlkjs0-9sdf
EOF`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID"},
Run: func(cmd *cobra.Command, args []string) {
secretId := args[0]
input, err := readSecretInput()
cobra.CheckErr(err)
secret, err := getClientGQL().UpdateSecret(secretId, *input)
cobra.CheckErr(err)
fmt.Println(secret.ID)
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down

0 comments on commit cbddbba

Please sign in to comment.