Skip to content

Commit

Permalink
Merge pull request #7 from wantedly/bgpat/all-namespaces
Browse files Browse the repository at this point in the history
Add all-namespace option
  • Loading branch information
bgpat authored Dec 6, 2019
2 parents 84431e2 + 958e4f7 commit 2dab901
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"strings"
"text/tabwriter"

"github.com/wantedly/k8sec/k8s"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/wantedly/k8sec/k8s"
)

var listOpts = struct {
base64encode bool
base64encode bool
allnamespaces bool
}{}

// listCmd represents the list command
Expand All @@ -38,10 +39,11 @@ rails Opaque database-url cG9zdGdyZXM6Ly9leGFtcGxlLmNvbTo1NDMyL2RibmFtZQ==
}

type Secret struct {
Name string
Type string
Key string
Value string
Namespace string
Name string
Type string
Key string
Value string
}

func doList(cmd *cobra.Command, args []string) error {
Expand All @@ -58,13 +60,15 @@ func doList(cmd *cobra.Command, args []string) error {

if rootOpts.namespace != "" {
namespace = rootOpts.namespace
} else if listOpts.allnamespaces {
namespace = ""
} else {
namespace = k8sclient.DefaultNamespace()
}

w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintln(w, strings.Join([]string{"NAME", "TYPE", "KEY", "VALUE"}, "\t"))
fmt.Fprintln(w, strings.Join([]string{"NAMESPACE", "NAME", "TYPE", "KEY", "VALUE"}, "\t"))

var v string

Expand All @@ -84,10 +88,11 @@ func doList(cmd *cobra.Command, args []string) error {
}

secrets = append(secrets, Secret{
Name: secret.Name,
Type: string(secret.Type),
Key: key,
Value: v,
Namespace: secret.Namespace,
Name: secret.Name,
Type: string(secret.Type),
Key: key,
Value: v,
})
}

Expand Down Expand Up @@ -128,17 +133,18 @@ func doList(cmd *cobra.Command, args []string) error {

for _, kv := range kvs {
secrets = append(secrets, Secret{
Name: secret.Name,
Type: string(secret.Type),
Key: kv.k,
Value: kv.v,
Namespace: secret.Namespace,
Name: secret.Name,
Type: string(secret.Type),
Key: kv.k,
Value: kv.v,
})
}
}
}

for _, secret := range secrets {
fmt.Fprintln(w, strings.Join([]string{secret.Name, secret.Type, secret.Key, secret.Value}, "\t"))
fmt.Fprintln(w, strings.Join([]string{secret.Namespace, secret.Name, secret.Type, secret.Key, secret.Value}, "\t"))
}

w.Flush()
Expand All @@ -150,4 +156,5 @@ func init() {
RootCmd.AddCommand(listCmd)

listCmd.Flags().BoolVar(&listOpts.base64encode, "base64", false, "Show values as base64-encoded string")
listCmd.Flags().BoolVarP(&listOpts.allnamespaces, "all-namespaces", "A", false, "Show values in All Namespaces")
}

0 comments on commit 2dab901

Please sign in to comment.