Skip to content

Commit

Permalink
Use cobra rangeargs instead of customer err
Browse files Browse the repository at this point in the history
  • Loading branch information
elsesiy committed Aug 7, 2024
1 parent 60d58be commit 496d4e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
validSecretJson = `{
"apiVersion": "v1",
"apiVersion": "v1",
"data": {
"key1": "dmFsdWUxCg==",
"key2": "dmFsdWUyCg=="
Expand All @@ -26,7 +26,7 @@ var (
}`

emptySecretJson = `{
"apiVersion": "v1",
"apiVersion": "v1",
"data": {},
"kind": "Secret",
"metadata": {
Expand Down
21 changes: 8 additions & 13 deletions pkg/cmd/view-secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ const (
)

var (
// ErrInsufficientArgs is thrown if arg len <1 or >2
ErrInsufficientArgs = fmt.Errorf("\nincorrect number or arguments, see --help for usage instructions")

// ErrNoSecretFound is thrown when no secret name was provided but we didn't find any secrets
ErrNoSecretFound = errors.New("no secrets found")

// ErrSecretEmpty is thrown when there's no data in the secret
ErrSecretEmpty = errors.New("secret is empty")

Expand All @@ -79,10 +77,11 @@ func NewCmdViewSecret() *cobra.Command {
res := &CommandOpts{}

cmd := &cobra.Command{
Use: "view-secret [secret-name] [secret-key]",
Short: "Decode a kubernetes secret by name & key in the current context/cluster/namespace",
Args: cobra.RangeArgs(0, 2),
Example: fmt.Sprintf(example, "kubectl"),
Short: "Decode a kubernetes secret by name & key in the current context/cluster/namespace",
SilenceUsage: true,
Use: "view-secret [secret-name] [secret-key]",
RunE: func(c *cobra.Command, args []string) error {
if err := res.Validate(args); err != nil {
return err
Expand Down Expand Up @@ -111,15 +110,11 @@ func NewCmdViewSecret() *cobra.Command {
// Validate ensures proper command usage
func (c *CommandOpts) Validate(args []string) error {
argLen := len(args)
switch argLen {
case 1:
if argLen >= 1 {
c.secretName = args[0]
case 2:
c.secretName = args[0]
c.secretKey = args[1]
default:
if argLen < 0 || argLen > 2 {
return ErrInsufficientArgs

if argLen == 2 {
c.secretKey = args[1]
}
}

Expand Down

0 comments on commit 496d4e0

Please sign in to comment.