Skip to content

Commit

Permalink
secret: add secret get command
Browse files Browse the repository at this point in the history
  • Loading branch information
Mate Ory authored and orymate committed Jan 8, 2019
1 parent 94e08cb commit 5a06651
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/cli/command/organization/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewSelectCommand(banzaiCli cli.Cli) *cobra.Command {

cmd := &cobra.Command{
Use: "select [ORG NAME]",
Short: "Select and organization",
Short: "Select an organization as the default",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
Expand Down
6 changes: 1 addition & 5 deletions internal/cli/command/secret/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ func NewSecretCommand(banzaiCli cli.Cli) *cobra.Command {

cmd.AddCommand(
NewListCommand(banzaiCli),
NewGetCommand(banzaiCli),
)

// Temporarily, until we have only one command here
cmd = NewListCommand(banzaiCli)
cmd.Use = "secret"
cmd.Aliases = []string{"secrets", "s"}

return cmd
}
99 changes: 99 additions & 0 deletions internal/cli/command/secret/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright © 2018 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package secret

import (
"context"
"errors"
"fmt"

"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/banzaicloud/banzai-cli/internal/cli/format"
"github.com/banzaicloud/banzai-cli/internal/cli/input"
"github.com/banzaicloud/pipeline/client"
"github.com/goph/emperror"
"github.com/spf13/cobra"
)

type getOptions struct {
format string
name string
id string
hide bool
}

// NewGetCommand creates a new cobra.Command for `banzai secret get`.
func NewGetCommand(banzaiCli cli.Cli) *cobra.Command {
options := getOptions{}

cmd := &cobra.Command{
Use: "get ([--name=]NAME | --id=ID)",
Short: "Get a secret",
Args: cobra.MaximumNArgs(1),
Aliases: []string{"g", "show", "sh"},
RunE: func(cmd *cobra.Command, args []string) error {
options.format, _ = cmd.Flags().GetString("output")
if len(args) == 1 {
options.name = args[0]
}
return runGet(banzaiCli, options)
},
}

flags := cmd.Flags()

flags.StringVarP(&options.name, "name", "n", "", "Name of secret to get")
flags.StringVarP(&options.id, "id", "i", "", "ID of secret to get")
flags.BoolVarP(&options.hide, "hide", "H", false, "Hide secret contents in the output")

return cmd
}

func runGet(banzaiCli cli.Cli, options getOptions) error {
if options.id == "" && options.name == "" {
return errors.New("specify either the name or the ID of the secret")
}
orgID := input.GetOrganization(banzaiCli)
id := options.id
if id == "" {
secrets, _, err := banzaiCli.Client().SecretsApi.GetSecrets(context.Background(), orgID, &client.GetSecretsOpts{})
if err != nil {
return emperror.Wrap(err, "could not list secrets")
}
for _, secret := range secrets {
if secret.Name == options.name {
id = secret.Id
break
}
}
if id == "" {
return fmt.Errorf("can't find secret named %q", options.name)
}
}

secret, _, err := banzaiCli.Client().SecretsApi.GetSecret(context.Background(), orgID, id)
if err != nil {
return emperror.Wrap(err, "could not get secret")
}

if options.hide {
for i := range secret.Values {
secret.Values[i] = "<hidden>"
}
}

format.SecretWrite(banzaiCli.Out(), options.format, banzaiCli.Color(), secret)
return nil
}
11 changes: 6 additions & 5 deletions internal/cli/command/secret/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ type listOptions struct {
secretType string
}

// NewListCommand creates a new cobra.Command for `banzai organization list`.
// NewListCommand creates a new cobra.Command for `banzai secret list`.
func NewListCommand(banzaiCli cli.Cli) *cobra.Command {
options := listOptions{}

cmd := &cobra.Command{
Use: "list",
Short: "List secrets",
Args: cobra.NoArgs,
Use: "list",
Short: "List secrets",
Args: cobra.NoArgs,
Aliases: []string{"l", "ls"},
Run: func(cmd *cobra.Command, args []string) {
options.format, _ = cmd.Flags().GetString("output")
runList(banzaiCli, options)
Expand All @@ -64,5 +65,5 @@ func runList(banzaiCli cli.Cli, options listOptions) {
log.Fatalf("could not list secrets: %v", err)
}

format.SecretWrite(banzaiCli.Out(), options.format, banzaiCli.Color(), secrets)
format.SecretsWrite(banzaiCli.Out(), options.format, banzaiCli.Color(), secrets)
}
7 changes: 6 additions & 1 deletion internal/cli/format/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ import (
log "github.com/sirupsen/logrus"
)

// SecretWrite writes a secret list to the output.
// SecretWrite writes a secret to the output.
func SecretWrite(out io.Writer, format string, color bool, data interface{}) {
SecretsWrite(out, format, color, []interface{}{data})
}

// SecretsWrite writes a secret list to the output.
func SecretsWrite(out io.Writer, format string, color bool, data interface{}) {
ctx := &output.Context{
Out: out,
Color: color,
Expand Down
13 changes: 10 additions & 3 deletions internal/cli/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/banzaicloud/banzai-cli/pkg/formatting"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

// Context contains parameters for formatting data.
Expand All @@ -32,7 +32,12 @@ type Context struct {
Fields []string
}

// Output writes data in a specific format.
// SingleOutput writes single record in a specific format.
func SingleOutput(ctx *Context, data interface{}) error {
return Output(ctx, []interface{}{data})
}

// Output writes a data slice in a specific format.
func Output(ctx *Context, data interface{}) error {
switch ctx.Format {
case "json":
Expand All @@ -55,12 +60,14 @@ func Output(ctx *Context, data interface{}) error {

return errors.Wrap(err, "cannot write output")

default:
case "default":
table := formatting.NewTable(data, ctx.Fields)
formatted := table.Format(ctx.Color)

_, err := fmt.Fprintln(ctx.Out, formatted)

return errors.Wrap(err, "cannot write output")
default:
return fmt.Errorf("no output format named %q", ctx.Format)
}
}

0 comments on commit 5a06651

Please sign in to comment.