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

Commit

Permalink
Merge branch 'master' into feature/input-autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoDanielRufino committed Jan 20, 2021
2 parents 2ce313c + 37ab4d1 commit c6a70ad
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
name: Horus Security Analysis
command: |
curl -fsSL https://horusec.io/bin/install.sh | bash -s v1-5-0
horusec start -p ./ -a "$HORUSEC_CLI_REPOSITORY_AUTHORIZATION" -G "true" -u "https://api-horus.zup.com.br" -n "ritchie-cli"
horusec start -p ./ -a "$HORUSEC_CLI_REPOSITORY_AUTHORIZATION" -G "true" -u "https://api-horusec.zup.com.br" -n "ritchie-cli"
unit_test:
executor: ritchie-tests-and-static-analisys-executor
Expand Down
1 change: 0 additions & 1 deletion internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ type InputURLMock struct {

func (i *InputURLMock) URL(name, defaultValue string) (string, error) {
args := i.Called(name, defaultValue)

return args.String(0), args.Error(1)
}

Expand Down
36 changes: 35 additions & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,48 @@
package cmd

import (
"fmt"
"reflect"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
)

const stdinWarning = "stdin commands are deprecated and will no longer be supported in future versions. Please use" +
"flags for programatic formula execution"
" flags for programatic formula execution"

type flag struct {
name string
shortName string
kind reflect.Kind
defValue interface{}
description string
}

type flags []flag

// CommandRunnerFunc represents that runner func for commands.
type CommandRunnerFunc func(cmd *cobra.Command, args []string) error

func addReservedFlags(flags *pflag.FlagSet, flagsToAdd flags) {
for _, flag := range flagsToAdd {
switch flag.kind { //nolint:exhaustive
case reflect.String:
flags.StringP(flag.name, flag.shortName, flag.defValue.(string), flag.description)
case reflect.Bool:
flags.BoolP(flag.name, flag.shortName, flag.defValue.(bool), flag.description)
case reflect.Int:
flags.IntP(flag.name, flag.shortName, flag.defValue.(int), flag.description)
default:
warning := fmt.Sprintf("The %q type is not supported for the %q flag", flag.kind.String(), flag.name)
prompt.Warning(warning)
}
}
}

// RunFuncE delegates to stdinFunc if --stdin flag is passed otherwise delegates to promptFunc.
func RunFuncE(stdinFunc, promptFunc CommandRunnerFunc) CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
Expand All @@ -45,6 +75,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
107 changes: 78 additions & 29 deletions pkg/cmd/delete_credential.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"errors"
"fmt"
"io"
"reflect"

"github.com/spf13/cobra"

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

const (
providerFlagName = "provider"
providerFlagDescription = "Provider name to delete"
)

type inputConfig struct {
provider string
}

// deleteCredentialCmd type for set credential command
type deleteCredentialCmd struct {
credential.CredDelete
Expand All @@ -26,6 +37,15 @@ type deleteCredential struct {
Provider string `json:"provider"`
}

var deleteCredentialFlags = flags{
{
name: providerFlagName,
kind: reflect.String,
defValue: "",
description: providerFlagDescription,
},
}

// NewDeleteCredentialCmd creates a new cmd instance
func NewDeleteCredentialCmd(
credDelete credential.CredDelete,
Expand All @@ -46,49 +66,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()

addReservedFlags(cmd.Flags(), deleteCredentialFlags)

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")
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 {
} else if inputParams.provider == "" {
return nil
}

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

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

func (d *deleteCredentialCmd) resolveInput(cmd *cobra.Command, context string) (inputConfig, error) {
if IsFlagInput(cmd) {
return d.resolveFlags(cmd)
}
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, 0, len(data))
for _, c := range data {
providers = append(providers, c.Provider)
}

provider, 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{provider}, nil
}

func (d *deleteCredentialCmd) resolveFlags(cmd *cobra.Command) (inputConfig, error) {
provider, err := cmd.Flags().GetString(providerFlagName)
if err != nil {
return inputConfig{}, err
} else if provider == "" {
return inputConfig{}, errors.New("please provide a value for 'provider'")
}
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

0 comments on commit c6a70ad

Please sign in to comment.