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

List credentials command #295

Merged
merged 34 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bbda4de
added command
victor-schumacher Jul 15, 2020
1450cb6
add hide credential
victor-schumacher Jul 15, 2020
995cb31
change credential structure
victor-schumacher Jul 15, 2020
25f2325
list working
victor-schumacher Jul 17, 2020
03b7fb6
refactor table
victor-schumacher Jul 17, 2020
c7fdad9
remove comments
victor-schumacher Jul 17, 2020
6f0c003
change folder structure
victor-schumacher Jul 21, 2020
4a9b01c
changing tests
victor-schumacher Jul 21, 2020
f18fc76
merge 2.0.0
victor-schumacher Jul 21, 2020
4bed601
tests ok
victor-schumacher Jul 22, 2020
10ab106
fix lint
victor-schumacher Jul 22, 2020
dbaf074
remove single name on tests,
victor-schumacher Jul 22, 2020
9b04bfc
refactor
victor-schumacher Jul 22, 2020
a64e1b0
Merge branch 'ritchie-2.0.0' into feature/listCredentials
victor-schumacher Jul 23, 2020
fe6d95e
add test
victor-schumacher Jul 23, 2020
ad7ffbf
add more test
victor-schumacher Jul 24, 2020
cff13f1
refactor
victor-schumacher Jul 24, 2020
bfe9b94
path now use filepath.Join
victor-schumacher Jul 24, 2020
53cf17a
changed to dirManager
victor-schumacher Jul 24, 2020
e481ec6
working on changes
victor-schumacher Jul 27, 2020
2ed1716
Merge branch 'feature/listCredentials' into ritchie-2.0.0
victor-schumacher Jul 27, 2020
689d963
whatever
victor-schumacher Jul 27, 2020
1055ac0
whatever
victor-schumacher Jul 27, 2020
cd59c46
mocks
victor-schumacher Jul 27, 2020
0f1197c
separate operations interface
victor-schumacher Jul 27, 2020
dfde4ff
merge
victor-schumacher Jul 27, 2020
a19d1d1
merge rit 2
victor-schumacher Jul 27, 2020
f4fa669
add tutorial finder mock on list repo
victor-schumacher Jul 27, 2020
8140ae5
fix duplicated credentials
victor-schumacher Jul 27, 2020
57b4909
change homeDir
victor-schumacher Jul 27, 2020
00f5738
merge rit2
victor-schumacher Jul 27, 2020
2e017c0
merge rit 2.0.0
victor-schumacher Jul 27, 2020
347c7e4
change credential table
victor-schumacher Jul 28, 2020
96b8550
add format to long credential values, change infos on list help
victor-schumacher Jul 28, 2020
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
19 changes: 10 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

"k8s.io/kubectl/pkg/util/templates"

"github.com/ZupIT/ritchie-cli/pkg/formula/creator/template"
"github.com/ZupIT/ritchie-cli/pkg/credential"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"

"github.com/ZupIT/ritchie-cli/pkg/formula/builder"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator/template"
"github.com/ZupIT/ritchie-cli/pkg/formula/repo"
"github.com/ZupIT/ritchie-cli/pkg/formula/runner"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
Expand All @@ -26,7 +26,6 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/autocomplete"
"github.com/ZupIT/ritchie-cli/pkg/cmd"
"github.com/ZupIT/ritchie-cli/pkg/credential/credsingle"
"github.com/ZupIT/ritchie-cli/pkg/env"
"github.com/ZupIT/ritchie-cli/pkg/env/envcredential"
"github.com/ZupIT/ritchie-cli/pkg/file/fileutil"
Expand Down Expand Up @@ -77,15 +76,15 @@ func buildCommands() *cobra.Command {
repoPrioritySetter := repo.NewPrioritySetter(ritchieHomeDir, fileManager)

tplManager := template.NewManager(api.RitchieHomeDir())
ctxFinder := rcontext.NewFinder(ritchieHomeDir)
ctxFinder := rcontext.NewFinder(ritchieHomeDir, fileManager)
ctxSetter := rcontext.NewSetter(ritchieHomeDir, ctxFinder)
ctxRemover := rcontext.NewRemover(ritchieHomeDir, ctxFinder)
ctxFindSetter := rcontext.NewFindSetter(ritchieHomeDir, ctxFinder, ctxSetter)
ctxFindRemover := rcontext.NewFindRemover(ritchieHomeDir, ctxFinder, ctxRemover)
credSetter := credsingle.NewSetter(ritchieHomeDir, ctxFinder)
credFinder := credsingle.NewFinder(ritchieHomeDir, ctxFinder)
credSetter := credential.NewSetter(ritchieHomeDir, ctxFinder)
credFinder := credential.NewFinder(ritchieHomeDir, ctxFinder)
treeManager := tree.NewTreeManager(ritchieHomeDir, repoLister, api.CoreCmds)
credSettings := credsingle.NewSingleSettings(fileManager)
credSettings := credential.NewSettings(fileManager, dirManager, userHomeDir)
autocompleteGen := autocomplete.NewGenerator(treeManager)
credResolver := envcredential.NewResolver(credFinder)
envResolvers := make(env.Resolvers)
Expand Down Expand Up @@ -141,8 +140,9 @@ func buildCommands() *cobra.Command {
inputText,
inputBool,
inputList,
inputPassword,
)
inputPassword)
listCredentialCmd := cmd.NewListCredentialCmd(credSettings)

deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
Expand All @@ -165,6 +165,7 @@ func buildCommands() *cobra.Command {
createCmd.AddCommand(createFormulaCmd)
deleteCmd.AddCommand(deleteCtxCmd, deleteRepoCmd)
listCmd.AddCommand(listRepoCmd)
listCmd.AddCommand(listCredentialCmd)
setCmd.AddCommand(setCredentialCmd, setCtxCmd, setPriorityCmd)
showCmd.AddCommand(showCtxCmd)
buildCmd.AddCommand(buildFormulaCmd)
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import "github.com/spf13/cobra"
const descListLong = `
This command consists of multiple subcommands to interact with ritchie.

It can be used to list formulas, repositories and other objects.
It can be used to list repositories or credentials.
`

// NewListCmd create a new list instance
func NewListCmd() *cobra.Command {
return &cobra.Command{
Use: "list SUBCOMMAND",
Short: "List repositories",
Long: "List all formula repositories.",
Example: "rit list repo",
Short: "List repositories or credentials",
Long: descListLong,
Example: "rit list repo, rit list credential",
}
}
59 changes: 59 additions & 0 deletions pkg/cmd/list_credential.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"fmt"

"github.com/gosuri/uitable"
"github.com/spf13/cobra"

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

type listCredentialCmd struct {
credential.Settings
}

func NewListCredentialCmd(ss credential.Settings) *cobra.Command {
l := &listCredentialCmd{ss}

cmd := &cobra.Command{
Use: "credential",
Short: "List all credential names and fields.",
Example: "rit list credential",
RunE: l.run(),
}

return cmd
}

func printCredentialsTable(fields credential.ListCredDatas) {
table := uitable.New()
table.Wrap = true
table.AddRow(
prompt.Bold("CREDENTIAL"),
prompt.Bold("PROVIDER"),
prompt.Bold("CONTEXT"),
)

for _, c := range fields {
table.AddRow(c.Credential, c.Provider, c.Context)
}
if len(table.Rows) < 2 {
setCmd := prompt.Cyan("rit set credential")
fmt.Printf("You dont have any credential, use %s\n", setCmd)
} else {
fmt.Println(table)
}
}

func (l listCredentialCmd) run() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
data, err := l.Settings.ReadCredentialsValue(l.CredentialsPath())
if err != nil {
return err
}
printCredentialsTable(data)
return nil
}
}
24 changes: 24 additions & 0 deletions pkg/cmd/list_credential_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"os"
"testing"

"github.com/ZupIT/ritchie-cli/pkg/credential"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

func Test_ListCredentialCmd(t *testing.T) {
fileManager := stream.NewFileManager()
dirManager := stream.NewDirManager(fileManager)
homeDir, _ := os.UserHomeDir()
credSettings := credential.NewSettings(fileManager, dirManager, homeDir)

t.Run("Success case", func(t *testing.T) {
o := NewListCredentialCmd(credSettings)
if err := o.Execute();err !=nil{
t.Errorf("Test_ListCredentialCmd error = %s", err)
}
})

}
32 changes: 26 additions & 6 deletions pkg/cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (inputTextValidatorMock) Text(name string, validate func(interface{}) error
return "mocked text", nil
}

type inputTextErrorMock struct{}

func (inputTextErrorMock) Text(name string, required bool, helper ...string) (string, error) {
return "", errors.New("error on input text")
}

type inputSecretMock struct{}

func (inputSecretMock) Text(name string, required bool, helper ...string) (string, error) {
Expand Down Expand Up @@ -224,20 +230,34 @@ func (credSetterMock) Set(d credential.Detail) error {
return nil
}

type singleCredSettingsMock struct{}

func (s singleCredSettingsMock) WriteDefaultCredentials(path string) error {
return nil
type credSettingsMock struct {
error
}

func (s singleCredSettingsMock) ReadCredentials(path string) (credential.Fields, error) {
func (s credSettingsMock) ReadCredentialsFields(path string) (credential.Fields, error) {
return credential.Fields{}, nil
}

func (s singleCredSettingsMock) WriteCredentials(fields credential.Fields, path string) error {
func (s credSettingsMock) ReadCredentialsValue(path string) ([]credential.ListCredData, error) {
return []credential.ListCredData{}, nil
}

func (s credSettingsMock) WriteDefaultCredentialsFields(path string) error {
return nil
}

func (s credSettingsMock) WriteCredentialsFields(fields credential.Fields, path string) error {
return nil
}

func (s credSettingsMock) ProviderPath() string {
return ""
}

func (s credSettingsMock) CredentialsPath () string {
return ""
}

type runnerMock struct {
error error
}
Expand Down
33 changes: 16 additions & 17 deletions pkg/cmd/set_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/credential"
"github.com/ZupIT/ritchie-cli/pkg/credential/credsingle"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)
Expand All @@ -18,7 +17,7 @@ var inputTypes = []string{"plain text", "secret"}
// setCredentialCmd type for set credential command
type setCredentialCmd struct {
credential.Setter
credential.SingleSettings
credential.ReaderWriterPather
prompt.InputText
prompt.InputBool
prompt.InputList
Expand All @@ -28,19 +27,19 @@ type setCredentialCmd struct {
// NewSetCredentialCmd creates a new cmd instance
func NewSetCredentialCmd(
credSetter credential.Setter,
credSetting credential.SingleSettings,
file credential.ReaderWriterPather,
inText prompt.InputText,
inBool prompt.InputBool,
inList prompt.InputList,
inPass prompt.InputPassword,
) *cobra.Command {
s := &setCredentialCmd{
Setter: credSetter,
SingleSettings: credSetting,
InputText: inText,
InputBool: inBool,
InputList: inList,
InputPassword: inPass,
Setter: credSetter,
ReaderWriterPather: file,
InputText: inText,
InputBool: inBool,
InputList: inList,
InputPassword: inPass,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -71,26 +70,26 @@ func (s setCredentialCmd) runPrompt() CommandRunnerFunc {
}

func (s setCredentialCmd) prompt() (credential.Detail, error) {
err := s.WriteDefaultCredentials(credsingle.ProviderPath())
if err != nil {

if err := s.WriteDefaultCredentialsFields(s.ProviderPath()); err != nil {
return credential.Detail{}, err
}

var credDetail credential.Detail
cred := credential.Credential{}

credentials, err := s.ReadCredentials(credsingle.ProviderPath())
credentials, err := s.ReadCredentialsFields(s.ProviderPath())
if err != nil {
return credential.Detail{}, err
}

providerArr := credsingle.NewProviderArr(credentials)
providerArr := credential.NewProviderArr(credentials)
providerChoose, err := s.List("Select your provider", providerArr)
if err != nil {
return credDetail, err
}

if providerChoose == credsingle.AddNew {
if providerChoose == credential.AddNew {
newProvider, err := s.Text("Define your provider name:", true)
if err != nil {
return credDetail, err
Expand All @@ -117,8 +116,7 @@ func (s setCredentialCmd) prompt() (credential.Detail, error) {
}
}
credentials[newProvider] = newFields
err = s.WriteCredentials(credentials, credsingle.ProviderPath())
if err != nil {
if err = s.WriteCredentialsFields(credentials, s.ProviderPath()); err != nil {
return credDetail, err
}

Expand All @@ -135,7 +133,7 @@ func (s setCredentialCmd) prompt() (credential.Detail, error) {
return credDetail, err
}
} else {
value, err = s.Text(i.Name, true)
value, err = s.Text(i.Name+":", true)
if err != nil {
return credDetail, err
}
Expand All @@ -160,6 +158,7 @@ func (s setCredentialCmd) runStdin() CommandRunnerFunc {
}

prompt.Success(fmt.Sprintf("✔ %s credential saved!", strings.Title(cred.Service)))
prompt.Info("Check your credentials using rit list credential")
return nil
}
}
Expand Down
Loading