Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Delete credential flags #774

Merged
merged 16 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ func (d *DetailManagerMock) LatestTag(repo formula.Repo) string {

return args.String(0)
}

type InputListMock struct {
mock.Mock
}

func (m *InputListMock) List(string, []string, ...string) (string, error) {
args := m.Called()
return args.String(0), args.Error(1)
}

type InputBoolMock struct {
mock.Mock
}

func (m *InputBoolMock) Bool(string, []string, ...string) (bool, error) {
args := m.Called()
return args.Bool(0), nil
}
4 changes: 4 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func RunFuncE(stdinFunc, promptFunc CommandRunnerFunc) CommandRunnerFunc {
}
}

func IsFlagInput(cmd *cobra.Command) bool {
return cmd.Flags().NFlag() > 0
}

func DeprecateCmd(parentCmd *cobra.Command, deprecatedCmd, deprecatedMsg string) {
command := &cobra.Command{
Use: deprecatedCmd,
Expand Down
96 changes: 67 additions & 29 deletions pkg/cmd/delete_credential.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"io"

Expand All @@ -12,6 +13,15 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

const (
providerFlagName = "provider"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you extract the flag struct from the formula.go and use addReservedFlags function to generate all flags to any commands?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am a bit lost, are core commands supposed to have reserved flags?

providerFlagDescription = "Provider name to delete"
)

type inputConfig struct {
provider string
}

// deleteCredentialCmd type for set credential command
type deleteCredentialCmd struct {
credential.CredDelete
Expand Down Expand Up @@ -46,49 +56,32 @@ func NewDeleteCredentialCmd(
Use: "credential",
Short: "Delete credential",
Long: `Delete credential from current env`,
RunE: RunFuncE(s.runStdin(), s.runPrompt()),
RunE: RunFuncE(s.runStdin(), s.runFormula()),
ValidArgs: []string{""},
Args: cobra.OnlyValidArgs,
}
cmd.LocalFlags()
flags := cmd.Flags()
flags.String(providerFlagName, "", providerFlagDescription)

return cmd
}

func (d deleteCredentialCmd) runPrompt() CommandRunnerFunc {
func (d deleteCredentialCmd) runFormula() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
env, err := d.currentEnv()
curEnv, err := d.currentEnv()
if err != nil {
return err
}
prompt.Info(fmt.Sprintf("Current env: %s", env))
prompt.Info(fmt.Sprintf("Current env: %s", curEnv))

data, err := d.ReadCredentialsValueInEnv(d.CredentialsPath(), env)
inputParams, err := d.resolveInput(cmd, curEnv)
if err != nil {
return err
}

if len(data) <= 0 {
prompt.Error("You have no defined credentials in this env")
} else if inputParams.provider == "" {
return nil
}

var providers []string
for _, c := range data {
providers = append(providers, c.Provider)
}

cred, err := d.List("Credentials: ", providers)
if err != nil {
return err
}

if b, err := d.Bool("Are you sure want to delete this credential?", []string{"yes", "no"}); err != nil {
return err
} else if !b {
return nil
}

if err := d.Delete(cred); err != nil {
if err := d.Delete(inputParams.provider); err != nil {
return err
}

Expand All @@ -97,19 +90,64 @@ func (d deleteCredentialCmd) runPrompt() CommandRunnerFunc {
}
}

func (d *deleteCredentialCmd) resolveInput(cmd *cobra.Command, context string) (inputConfig, error) {
if IsFlagInput(cmd) {
return d.resolveFlags(cmd)
} else {
return d.resolvePrompt(context)
}
}

func (d *deleteCredentialCmd) resolvePrompt(context string) (inputConfig, error) {
data, err := d.ReadCredentialsValueInEnv(d.CredentialsPath(), context)
if err != nil {
return inputConfig{}, err
}

if len(data) == 0 {
return inputConfig{}, errors.New("you have no defined credentials in this env")
}

providers := make([]string, len(data))
Copy link
Contributor

@kaduartur kaduartur Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you declare an array like this, you are saying that its capacity is the data size and you are giving each position of the array an empty value, by appending it will add to the next position displaying to the user a select with empty fields.

image

For example, if the data has a size of 3 you will have an array with size 3, and when appending it will add one more position and the size of your array will be 6 with the first 3 empty positions.

we have two ways to fix this:

var providers []string // Simpler

providers := make([]string, 0, len(data)) // More complex

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first option does not pass the linter since we need to preallocate it, going for the second

for _, c := range data {
providers = append(providers, c.Provider)
}

cred, err := d.List("Credentials: ", providers)
if err != nil {
return inputConfig{}, err
}

if b, err := d.Bool("Are you sure want to delete this credential?", []string{"yes", "no"}); err != nil {
return inputConfig{}, err
} else if !b {
return inputConfig{}, nil
}
return inputConfig{cred}, nil
}

func (d *deleteCredentialCmd) resolveFlags(cmd *cobra.Command) (inputConfig, error) {
provider, err := cmd.Flags().GetString(providerFlagName)
if err != nil {
return inputConfig{}, err
}
return inputConfig{provider}, nil
}

// TODO: remove upon stdin deprecation
func (d deleteCredentialCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
dc, err := d.stdinResolver(cmd.InOrStdin())
if err != nil {
return err
}

env, err := d.currentEnv()
curEnv, err := d.currentEnv()
if err != nil {
return err
}

data, err := d.ReadCredentialsValueInEnv(d.CredentialsPath(), env)
data, err := d.ReadCredentialsValueInEnv(d.CredentialsPath(), curEnv)
if err != nil {
return err
}
Expand Down
Loading