From 19d8d93c4eccd6ba643bf6900f99f60e86b26053 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Tue, 27 Oct 2020 18:36:35 -0300 Subject: [PATCH 01/26] add reading and filtering --- pkg/formula/tree/checker.go | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 pkg/formula/tree/checker.go diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go new file mode 100644 index 000000000..e012dfc54 --- /dev/null +++ b/pkg/formula/tree/checker.go @@ -0,0 +1,3 @@ + + +package tree From 3f2152189855dfd9670afa391179bf3bb76ad7d2 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Tue, 27 Oct 2020 18:39:12 -0300 Subject: [PATCH 02/26] adding Signed-off-by: victor-schumacher --- cmd/main.go | 3 +- pkg/cmd/create_formula.go | 6 ++- pkg/formula/tree.go | 4 ++ pkg/formula/tree/checker.go | 84 +++++++++++++++++++++++++++++++- pkg/formula/tree/default_tree.go | 3 ++ 5 files changed, 97 insertions(+), 3 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 96615c178..2303bf51f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -140,6 +140,7 @@ func buildCommands() *cobra.Command { formBuildBat := builder.NewBuildBat(fileManager) formBuildDocker := builder.NewBuildDocker(fileManager) formulaLocalBuilder := builder.NewBuildLocal(ritchieHomeDir, dirManager, fileManager, treeGen) + treeJsonChecker := tree.NewChecker(dirManager, fileManager) postRunner := runner.NewPostRunner(fileManager, dirManager) inputManager := runner.NewInput(envResolvers, fileManager, inputList, inputText, inputTextValidator, inputBool, inputPassword) @@ -215,7 +216,7 @@ func buildCommands() *cobra.Command { deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, dirManager, inputList, inputBool) deleteFormulaCmd := cmd.NewDeleteFormulaCmd(userHomeDir, ritchieHomeDir, formulaWorkspace, dirManager, inputBool, inputText, inputList, treeGen, fileManager) - createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, tplManager, formulaWorkspace, inputText, inputTextValidator, inputList, tutorialFinder) + createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, tplManager, formulaWorkspace, inputText, inputTextValidator, inputList, tutorialFinder, treeJsonChecker) buildFormulaCmd := cmd.NewBuildFormulaCmd(userHomeDir, formulaLocalBuilder, formulaWorkspace, watchManager, dirManager, inputText, inputList, tutorialFinder) showFormulaRunnerCmd := cmd.NewShowFormulaRunnerCmd(configManager) setFormulaRunnerCmd := cmd.NewSetFormulaRunnerCmd(configManager, inputList) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 5e5391433..915a2f48d 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -30,6 +30,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/formula" "github.com/ZupIT/ritchie-cli/pkg/formula/creator/template" + "github.com/ZupIT/ritchie-cli/pkg/formula/tree" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" "github.com/ZupIT/ritchie-cli/pkg/stdin" @@ -45,6 +46,7 @@ type createFormulaCmd struct { inList prompt.InputList tplM template.Manager rt rtutorial.Finder + treeChecker tree.CheckerManager } // CreateFormulaCmd creates a new cmd instance @@ -57,6 +59,7 @@ func NewCreateFormulaCmd( inTextValidator prompt.InputTextValidator, inList prompt.InputList, rtf rtutorial.Finder, + treeChecker tree.CheckerManager, ) *cobra.Command { c := createFormulaCmd{ homeDir: homeDir, @@ -67,6 +70,7 @@ func NewCreateFormulaCmd( inList: inList, tplM: tplM, rt: rtf, + treeChecker: treeChecker, } cmd := &cobra.Command{ @@ -130,7 +134,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { WorkspacePath: wspace.Dir, FormulaPath: formulaPath, } - + c.treeChecker.CheckCommands() c.create(cf, wspace.Dir, formulaPath) return nil } diff --git a/pkg/formula/tree.go b/pkg/formula/tree.go index cb4185cdb..00cc756a9 100644 --- a/pkg/formula/tree.go +++ b/pkg/formula/tree.go @@ -30,3 +30,7 @@ type TreeManager interface { type TreeGenerator interface { Generate(repoPath string) (Tree, error) } + +type TreeChecker interface { + CheckCommands() +} \ No newline at end of file diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index e012dfc54..60f5ad4c2 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -1,3 +1,85 @@ +package tree +import ( + "encoding/json" + "fmt" + "path/filepath" -package tree + "github.com/ZupIT/ritchie-cli/pkg/api" + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/prompt" + "github.com/ZupIT/ritchie-cli/pkg/stream" +) + +// TODO rit home dir must be a parameter +type CheckerManager struct { + dir stream.DirLister + file stream.FileReader +} + +func NewChecker(dir stream.DirLister, file stream.FileReader) CheckerManager { + return CheckerManager{dir: dir, file: file} +} + +// CheckCommands is used to warn the user about conflicting +// formula commands on different repos. This function don't +// return err because print an error because of a unsuccessful +// warning attempt can be confusing to the user. +func (cm CheckerManager) CheckCommands() { + trees := cm.readCommands() + commands := filterCommands(trees) + conflictingCommands := conflictingCommands(commands) + printConflictingCommandsWarning(conflictingCommands) + +} + +func (cm CheckerManager) readCommands() []formula.Tree { + repoDir := filepath.Join(api.RitchieHomeDir(), "repos") + repos, _ := cm.dir.List(repoDir, false) + + tree := formula.Tree{} + treeArr := make([]formula.Tree, len(repos)) + for _, r := range repos { + path := fmt.Sprintf(treeRepoCmdPattern, api.RitchieHomeDir(), r) + bytes, _ := cm.file.Read(path) + _ = json.Unmarshal(bytes, &tree) + treeArr = append(treeArr, tree) + } + return treeArr +} + +func conflictingCommands(cmds []string) []string { + moreThanOne := 0 + duplicatedCommands := []string{""} + for _, c := range cmds { + for _, c2 := range cmds { + if c == c2 { + moreThanOne++ + if moreThanOne == 2 { + duplicatedCommands = append(duplicatedCommands, c) + } + } + } + moreThanOne = 0 + } + + return duplicatedCommands +} + +func filterCommands(tree []formula.Tree) []string { + allCommands := []string{""} + for _, t := range tree { + for _, c := range t.Commands { + allCommands = append(allCommands, c.Id) + } + } + return allCommands +} + +func printConflictingCommandsWarning(conflictingCommands []string) { + // TODO improve the warn information + prompt.Warning("There are some conflicting commands on formulas" + + "it can cause unexpected behaviors") + + fmt.Println(conflictingCommands) +} diff --git a/pkg/formula/tree/default_tree.go b/pkg/formula/tree/default_tree.go index 0b6a77e84..403dc95ac 100644 --- a/pkg/formula/tree/default_tree.go +++ b/pkg/formula/tree/default_tree.go @@ -26,6 +26,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/stream" ) +// FIXME there are some slashes here, it works with windows? const ( treeLocalCmdPattern = "%s/repos/local/tree.json" treeRepoCmdPattern = "%s/repos/%s/tree.json" @@ -167,3 +168,5 @@ func (d Manager) loadTree(treeCmdFile string) (formula.Tree, error) { return tree, nil } + + From 7fde33ea8aa674842ea0c893980abe6d2ace95a9 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Thu, 29 Oct 2020 11:45:55 -0300 Subject: [PATCH 03/26] finding and printing conflicts Signed-off-by: victor-schumacher --- pkg/cmd/create_formula_test.go | 3 ++ pkg/formula/tree/checker.go | 55 +++++++++++++++++++------------- pkg/formula/tree/checker_test.go | 1 + 3 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 pkg/formula/tree/checker_test.go diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index 645c459ff..e9cd8f0e9 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/ZupIT/ritchie-cli/pkg/formula/creator/template" + "github.com/ZupIT/ritchie-cli/pkg/formula/tree" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/stream" ) @@ -39,6 +40,7 @@ func TestNewCreateFormulaCmd(t *testing.T) { inputTextValidatorMock{}, inputListMock{}, TutorialFinderMock{}, + tree.CheckerManager{}, ) cmd.PersistentFlags().Bool("stdin", false, "input by stdin") if cmd == nil { @@ -125,6 +127,7 @@ func TestCreateFormulaCmd(t *testing.T) { tt.in.inTextValidator, tt.in.inList, TutorialFinderMock{}, + tree.CheckerManager{}, ) createFormulaCmd.PersistentFlags().Bool("stdin", false, "input by stdin") if err := createFormulaCmd.Execute(); (err != nil) != tt.wantErr { diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 60f5ad4c2..b840db41e 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "path/filepath" + "strings" "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/formula" @@ -38,32 +39,16 @@ func (cm CheckerManager) readCommands() []formula.Tree { repos, _ := cm.dir.List(repoDir, false) tree := formula.Tree{} - treeArr := make([]formula.Tree, len(repos)) + var treeArr []formula.Tree for _, r := range repos { path := fmt.Sprintf(treeRepoCmdPattern, api.RitchieHomeDir(), r) bytes, _ := cm.file.Read(path) _ = json.Unmarshal(bytes, &tree) treeArr = append(treeArr, tree) - } - return treeArr -} - -func conflictingCommands(cmds []string) []string { - moreThanOne := 0 - duplicatedCommands := []string{""} - for _, c := range cmds { - for _, c2 := range cmds { - if c == c2 { - moreThanOne++ - if moreThanOne == 2 { - duplicatedCommands = append(duplicatedCommands, c) - } - } - } - moreThanOne = 0 + tree = formula.Tree{} } - return duplicatedCommands + return treeArr } func filterCommands(tree []formula.Tree) []string { @@ -76,10 +61,36 @@ func filterCommands(tree []formula.Tree) []string { return allCommands } +func conflictingCommands(commands []string) []string { + duplicateFrequency := make(map[string]int) + duplicatedCommands := []string{""} + for _, item := range commands { + _, exist := duplicateFrequency[item] + if exist { + duplicateFrequency[item] += 1 // increase counter by 1 if already in the map + duplicatedCommands = append(duplicatedCommands, item) + } else { + duplicateFrequency[item] = 1 // else start counting from 1 + } + } + return duplicatedCommands + +} + + func printConflictingCommandsWarning(conflictingCommands []string) { // TODO improve the warn information - prompt.Warning("There are some conflicting commands on formulas" + - "it can cause unexpected behaviors") + fmt.Print(prompt.Yellow("The following formula commands are conflicting:")) + fc := formatCommands(conflictingCommands) + for _, c := range fc { + fmt.Println(c) + } +} - fmt.Println(conflictingCommands) +func formatCommands(commands []string) []string { + for i, _ := range commands { + commands[i] = strings.Replace(commands[i], "root", "rit", 1) + commands[i] = strings.ReplaceAll(commands[i], "_", " ") + } + return commands } diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go new file mode 100644 index 000000000..c6545c18b --- /dev/null +++ b/pkg/formula/tree/checker_test.go @@ -0,0 +1 @@ +package tree From d507737b03f6efb73ab9d208f881c990cc591d80 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Thu, 29 Oct 2020 16:40:27 -0300 Subject: [PATCH 04/26] add basic test Signed-off-by: victor-schumacher --- pkg/cmd/create_formula_test.go | 5 +++-- pkg/formula/tree/checker.go | 1 - pkg/formula/tree/checker_test.go | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index e9cd8f0e9..55d314a8f 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -31,6 +31,7 @@ func TestNewCreateFormulaCmd(t *testing.T) { fileManager := stream.NewFileManager() dirManager := stream.NewDirManager(fileManager) tplM := template.NewManager("../../testdata", dirManager) + tChecker := tree.NewChecker(dirManager, fileManager) cmd := NewCreateFormulaCmd( os.TempDir(), formCreator{}, @@ -40,7 +41,7 @@ func TestNewCreateFormulaCmd(t *testing.T) { inputTextValidatorMock{}, inputListMock{}, TutorialFinderMock{}, - tree.CheckerManager{}, + tChecker, ) cmd.PersistentFlags().Bool("stdin", false, "input by stdin") if cmd == nil { @@ -157,4 +158,4 @@ func (tm TemplateManagerCustomMock) ResolverNewPath(oldPath, newDir, lang, works } func (tm TemplateManagerCustomMock) Validate() error { return tm.ValidateMock() -} +} \ No newline at end of file diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index b840db41e..dad4bcc3b 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -79,7 +79,6 @@ func conflictingCommands(commands []string) []string { func printConflictingCommandsWarning(conflictingCommands []string) { - // TODO improve the warn information fmt.Print(prompt.Yellow("The following formula commands are conflicting:")) fc := formatCommands(conflictingCommands) for _, c := range fc { diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index c6545c18b..b4137d9c5 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -1 +1,15 @@ package tree + +import ( + "testing" + + "github.com/ZupIT/ritchie-cli/pkg/stream" +) + +func TestChecker(t *testing.T) { + t.Run("Running checker test", func(t *testing.T) { + treeChecker := NewChecker(stream.DirManager{}, stream.FileManager{}) + treeChecker.CheckCommands() + }) + +} From 4f5b39dfaa3ded40e2c43eefedfe6d2f07b71301 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Thu, 29 Oct 2020 16:42:00 -0300 Subject: [PATCH 05/26] fix lint Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index dad4bcc3b..afad4c46b 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -87,7 +87,7 @@ func printConflictingCommandsWarning(conflictingCommands []string) { } func formatCommands(commands []string) []string { - for i, _ := range commands { + for i := range commands { commands[i] = strings.Replace(commands[i], "root", "rit", 1) commands[i] = strings.ReplaceAll(commands[i], "_", " ") } From b2011fd6b4bcb4f5b5dadeecfc8f83b0a7e00f2a Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Thu, 29 Oct 2020 16:58:47 -0300 Subject: [PATCH 06/26] remove spaces Signed-off-by: victor-schumacher --- pkg/formula/tree/default_tree.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/formula/tree/default_tree.go b/pkg/formula/tree/default_tree.go index 403dc95ac..c3ec2dfbf 100644 --- a/pkg/formula/tree/default_tree.go +++ b/pkg/formula/tree/default_tree.go @@ -167,6 +167,4 @@ func (d Manager) loadTree(treeCmdFile string) (formula.Tree, error) { } return tree, nil -} - - +} \ No newline at end of file From 19768d879f6e218f92ece4860bb2053f716669f8 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 09:16:19 -0300 Subject: [PATCH 07/26] fix main Signed-off-by: victor-schumacher --- pkg/commands/builder.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/commands/builder.go b/pkg/commands/builder.go index f0de44a01..a4dd4c4cc 100644 --- a/pkg/commands/builder.go +++ b/pkg/commands/builder.go @@ -121,6 +121,7 @@ func Build() *cobra.Command { credSetter := credential.NewSetter(ritchieHomeDir, ctxFinder) credFinder := credential.NewFinder(ritchieHomeDir, ctxFinder, fileManager) treeManager := tree.NewTreeManager(ritchieHomeDir, repoLister, api.CoreCmds, fileManager, repoProviders, isRootCommand) + treeChecker := tree.NewChecker(dirManager, fileManager) credSettings := credential.NewSettings(fileManager, dirManager, userHomeDir) autocompleteGen := autocomplete.NewGenerator(treeManager) credResolver := envcredential.NewResolver(credFinder, credSetter, inputPassword) @@ -220,7 +221,7 @@ func Build() *cobra.Command { deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, dirManager, inputList, inputBool) deleteFormulaCmd := cmd.NewDeleteFormulaCmd(userHomeDir, ritchieHomeDir, formulaWorkspace, dirManager, inputBool, inputText, inputList, treeGen, fileManager) - createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, tplManager, formulaWorkspace, inputText, inputTextValidator, inputList, tutorialFinder) + createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, tplManager, formulaWorkspace, inputText, inputTextValidator, inputList, tutorialFinder, treeChecker) buildFormulaCmd := cmd.NewBuildFormulaCmd(userHomeDir, formulaLocalBuilder, formulaWorkspace, watchManager, dirManager, inputText, inputList, tutorialFinder) showFormulaRunnerCmd := cmd.NewShowFormulaRunnerCmd(configManager) setFormulaRunnerCmd := cmd.NewSetFormulaRunnerCmd(configManager, inputList) From 7da4ee9f0f625e4a86c9999c06fa394eadd632fd Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 09:46:56 -0300 Subject: [PATCH 08/26] little refactor Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 23 +++++++---------------- pkg/formula/tree/default_tree.go | 1 - 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index afad4c46b..d2eb06808 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -12,7 +12,6 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/stream" ) -// TODO rit home dir must be a parameter type CheckerManager struct { dir stream.DirLister file stream.FileReader @@ -37,7 +36,6 @@ func (cm CheckerManager) CheckCommands() { func (cm CheckerManager) readCommands() []formula.Tree { repoDir := filepath.Join(api.RitchieHomeDir(), "repos") repos, _ := cm.dir.List(repoDir, false) - tree := formula.Tree{} var treeArr []formula.Tree for _, r := range repos { @@ -74,22 +72,15 @@ func conflictingCommands(commands []string) []string { } } return duplicatedCommands - } func printConflictingCommandsWarning(conflictingCommands []string) { - fmt.Print(prompt.Yellow("The following formula commands are conflicting:")) - fc := formatCommands(conflictingCommands) - for _, c := range fc { - fmt.Println(c) - } -} - -func formatCommands(commands []string) []string { - for i := range commands { - commands[i] = strings.Replace(commands[i], "root", "rit", 1) - commands[i] = strings.ReplaceAll(commands[i], "_", " ") - } - return commands + lastCommandIndex := len(conflictingCommands) - 1 + lastCommand := conflictingCommands[lastCommandIndex] + lastCommand = strings.Replace(lastCommand, "root", "rit", 1) + lastCommand = strings.ReplaceAll(lastCommand, "_", " ") + msg := fmt.Sprintf("The following formula command are conflicting: %s", lastCommand) + msg = prompt.Yellow(msg) + fmt.Print(msg) } diff --git a/pkg/formula/tree/default_tree.go b/pkg/formula/tree/default_tree.go index c3ec2dfbf..373411102 100644 --- a/pkg/formula/tree/default_tree.go +++ b/pkg/formula/tree/default_tree.go @@ -26,7 +26,6 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/stream" ) -// FIXME there are some slashes here, it works with windows? const ( treeLocalCmdPattern = "%s/repos/local/tree.json" treeRepoCmdPattern = "%s/repos/%s/tree.json" From a5e35d3defed0909b00308c539f806a22c234f26 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 09:59:01 -0300 Subject: [PATCH 09/26] little refactor Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 16 +++++++--------- pkg/formula/tree/checker_test.go | 1 - 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index d2eb06808..59e62576a 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -27,10 +27,9 @@ func NewChecker(dir stream.DirLister, file stream.FileReader) CheckerManager { // warning attempt can be confusing to the user. func (cm CheckerManager) CheckCommands() { trees := cm.readCommands() - commands := filterCommands(trees) - conflictingCommands := conflictingCommands(commands) - printConflictingCommandsWarning(conflictingCommands) - + commands := cm.filterCommands(trees) + conflictingCommands := cm.conflictingCommands(commands) + cm.printConflictingCommandsWarning(conflictingCommands) } func (cm CheckerManager) readCommands() []formula.Tree { @@ -49,7 +48,7 @@ func (cm CheckerManager) readCommands() []formula.Tree { return treeArr } -func filterCommands(tree []formula.Tree) []string { +func (cm CheckerManager) filterCommands(tree []formula.Tree) []string { allCommands := []string{""} for _, t := range tree { for _, c := range t.Commands { @@ -59,7 +58,7 @@ func filterCommands(tree []formula.Tree) []string { return allCommands } -func conflictingCommands(commands []string) []string { +func (cm CheckerManager) conflictingCommands(commands []string) []string { duplicateFrequency := make(map[string]int) duplicatedCommands := []string{""} for _, item := range commands { @@ -74,13 +73,12 @@ func conflictingCommands(commands []string) []string { return duplicatedCommands } - -func printConflictingCommandsWarning(conflictingCommands []string) { +func (cm CheckerManager) printConflictingCommandsWarning(conflictingCommands []string) { lastCommandIndex := len(conflictingCommands) - 1 lastCommand := conflictingCommands[lastCommandIndex] lastCommand = strings.Replace(lastCommand, "root", "rit", 1) lastCommand = strings.ReplaceAll(lastCommand, "_", " ") msg := fmt.Sprintf("The following formula command are conflicting: %s", lastCommand) msg = prompt.Yellow(msg) - fmt.Print(msg) + fmt.Println(msg) } diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index b4137d9c5..588a7d5b1 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -11,5 +11,4 @@ func TestChecker(t *testing.T) { treeChecker := NewChecker(stream.DirManager{}, stream.FileManager{}) treeChecker.CheckCommands() }) - } From 6265bff4b1f00069d29be7c34fa132baed2f2725 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 11:41:52 -0300 Subject: [PATCH 10/26] added a better test Signed-off-by: victor-schumacher --- pkg/formula/tree/checker_test.go | 39 ++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index 588a7d5b1..e20add73a 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -4,11 +4,42 @@ import ( "testing" "github.com/ZupIT/ritchie-cli/pkg/stream" + sMocks "github.com/ZupIT/ritchie-cli/pkg/stream/mocks" ) func TestChecker(t *testing.T) { - t.Run("Running checker test", func(t *testing.T) { - treeChecker := NewChecker(stream.DirManager{}, stream.FileManager{}) - treeChecker.CheckCommands() - }) + treeJson := `{ + "commands": [ + { + "id": "root_aws_create_bucket", + "parent": "root_aws_create", + "usage": "bucket", + "help": "short help placeholder for bucket", + "longHelp": "long help placeholder for bucket used by index page and -h", + "formula": true + } + ] + } +` + + tests := []struct{ + name string + file stream.FileReader + }{ + { + name: "Should success run", + file : sMocks.FileReaderCustomMock{ + ReadMock: func(path string) ([]byte, error) { + return []byte(treeJson), nil + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + treeChecker := NewChecker(stream.DirManager{}, tt.file) + treeChecker.CheckCommands() + }) + } + } From 5c3ce8f955ae2aac49a067f11492ab6b186b082a Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 11:45:50 -0300 Subject: [PATCH 11/26] added a repeated command Signed-off-by: victor-schumacher --- pkg/formula/tree/checker_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index e20add73a..efc07eb5f 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -10,6 +10,14 @@ import ( func TestChecker(t *testing.T) { treeJson := `{ "commands": [ + { + "id": "root_aws_create_bucket", + "parent": "root_aws_create", + "usage": "bucket", + "help": "short help placeholder for bucket", + "longHelp": "long help placeholder for bucket used by index page and -h", + "formula": true + }, { "id": "root_aws_create_bucket", "parent": "root_aws_create", From 1de7820f684ec22c5c00c17e24f29725c339ba9b Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 12:04:33 -0300 Subject: [PATCH 12/26] added dir Signed-off-by: victor-schumacher --- pkg/formula/tree/checker_test.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index efc07eb5f..4b6d2e693 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -33,6 +33,7 @@ func TestChecker(t *testing.T) { tests := []struct{ name string file stream.FileReader + dir DirManagerCustomMock }{ { name: "Should success run", @@ -41,13 +42,40 @@ func TestChecker(t *testing.T) { return []byte(treeJson), nil }, }, + dir: DirManagerCustomMock{ + list: func(dir string, hiddenDir bool) ([]string, error) { + return []string{"commons"}, nil + }, + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - treeChecker := NewChecker(stream.DirManager{}, tt.file) + treeChecker := NewChecker(tt.dir, tt.file) treeChecker.CheckCommands() }) } } +type DirManagerCustomMock struct { + exists func(dir string) bool + list func(dir string, hiddenDir bool) ([]string, error) + isDir func(dir string) bool + create func(dir string) error +} + +func (d DirManagerCustomMock) Exists(dir string) bool { + return d.exists(dir) +} + +func (d DirManagerCustomMock) List(dir string, hiddenDir bool) ([]string, error) { + return d.list(dir, hiddenDir) +} + +func (d DirManagerCustomMock) IsDir(dir string) bool { + return d.isDir(dir) +} + +func (d DirManagerCustomMock) Create(dir string) error { + return d.create(dir) +} \ No newline at end of file From b3fbcc0e8cef9476bbcc04c488c56accc47226f4 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 30 Oct 2020 15:48:46 -0300 Subject: [PATCH 13/26] add check for no conflicting commands Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 59e62576a..f4fbf06f6 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -29,7 +29,9 @@ func (cm CheckerManager) CheckCommands() { trees := cm.readCommands() commands := cm.filterCommands(trees) conflictingCommands := cm.conflictingCommands(commands) - cm.printConflictingCommandsWarning(conflictingCommands) + if len(commands) > 0 { + cm.printConflictingCommandsWarning(conflictingCommands) + } } func (cm CheckerManager) readCommands() []formula.Tree { From 31fafd87d9e178370693152c556adce600240e63 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Tue, 3 Nov 2020 09:41:58 -0300 Subject: [PATCH 14/26] added tree check to add repo Signed-off-by: victor-schumacher --- pkg/cmd/add_repo.go | 6 +++++- pkg/cmd/add_repo_test.go | 2 ++ pkg/commands/builder.go | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/add_repo.go b/pkg/cmd/add_repo.go index 956b37bd4..6c8a1c174 100644 --- a/pkg/cmd/add_repo.go +++ b/pkg/cmd/add_repo.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/cobra" "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/formula/tree" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" "github.com/ZupIT/ritchie-cli/pkg/stdin" @@ -42,7 +43,8 @@ type addRepoCmd struct { prompt.InputList prompt.InputBool prompt.InputInt - rt rtutorial.Finder + rt rtutorial.Finder + treeChecker tree.CheckerManager } func NewAddRepoCmd( @@ -55,6 +57,7 @@ func NewAddRepoCmd( inBool prompt.InputBool, inInt prompt.InputInt, rtf rtutorial.Finder, + treeChecker tree.CheckerManager, ) *cobra.Command { addRepo := addRepoCmd{ repo: repo, @@ -66,6 +69,7 @@ func NewAddRepoCmd( InputInt: inInt, InputPassword: inPass, rt: rtf, + treeChecker: treeChecker, } cmd := &cobra.Command{ Use: "repo", diff --git a/pkg/cmd/add_repo_test.go b/pkg/cmd/add_repo_test.go index ff17c03af..2a345629f 100644 --- a/pkg/cmd/add_repo_test.go +++ b/pkg/cmd/add_repo_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/formula/tree" "github.com/ZupIT/ritchie-cli/pkg/git/github" "github.com/ZupIT/ritchie-cli/pkg/prompt" ) @@ -175,6 +176,7 @@ func Test_addRepoCmd_runPrompt(t *testing.T) { tt.fields.InputBool, tt.fields.InputInt, TutorialFinderMock{}, + tree.CheckerManager{}, ) o.PersistentFlags().Bool("stdin", false, "input by stdin") if err := o.Execute(); (err != nil) != tt.wantErr { diff --git a/pkg/commands/builder.go b/pkg/commands/builder.go index a4dd4c4cc..28205d85a 100644 --- a/pkg/commands/builder.go +++ b/pkg/commands/builder.go @@ -208,7 +208,7 @@ func Build() *cobra.Command { deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList) setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList) showCtxCmd := cmd.NewShowContextCmd(ctxFinder) - addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, repoProviders, inputTextValidator, inputPassword, inputURL, inputList, inputBool, inputInt, tutorialFinder) + addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, repoProviders, inputTextValidator, inputPassword, inputURL, inputList, inputBool, inputInt, tutorialFinder, treeChecker) updateRepoCmd := cmd.NewUpdateRepoCmd(http.DefaultClient, repoListUpdater, repoProviders, inputText, inputPassword, inputURL, inputList, inputBool, inputInt) listRepoCmd := cmd.NewListRepoCmd(repoLister, repoProviders, tutorialFinder) deleteRepoCmd := cmd.NewDeleteRepoCmd(repoLister, inputList, repoDeleter) From 0aede6f6bec5f4b869546807aa95039eb93de247 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Tue, 3 Nov 2020 09:57:20 -0300 Subject: [PATCH 15/26] fix empty warning Signed-off-by: victor-schumacher --- pkg/cmd/add_repo.go | 1 + pkg/formula/tree/checker.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/add_repo.go b/pkg/cmd/add_repo.go index 6c8a1c174..90b41b923 100644 --- a/pkg/cmd/add_repo.go +++ b/pkg/cmd/add_repo.go @@ -189,6 +189,7 @@ func (ad addRepoCmd) runPrompt() CommandRunnerFunc { return err } tutorialAddRepo(tutorialHolder.Current) + ad.treeChecker.CheckCommands() return nil } } diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index f4fbf06f6..d2bccf82d 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -29,7 +29,7 @@ func (cm CheckerManager) CheckCommands() { trees := cm.readCommands() commands := cm.filterCommands(trees) conflictingCommands := cm.conflictingCommands(commands) - if len(commands) > 0 { + if len(conflictingCommands) > 1 { cm.printConflictingCommandsWarning(conflictingCommands) } } From b5b10db0fa285b868d89b7e9db006e75cbfe0684 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Tue, 3 Nov 2020 11:02:59 -0300 Subject: [PATCH 16/26] fixing tests Signed-off-by: victor-schumacher --- pkg/cmd/add_repo_test.go | 11 ++++++++++- pkg/cmd/mocks_test.go | 6 +++--- pkg/formula/tree/checker.go | 6 +++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/add_repo_test.go b/pkg/cmd/add_repo_test.go index 2a345629f..ff9ee16d1 100644 --- a/pkg/cmd/add_repo_test.go +++ b/pkg/cmd/add_repo_test.go @@ -164,6 +164,15 @@ func Test_addRepoCmd_runPrompt(t *testing.T) { wantErr: true, }, } + checkerManager := tree.NewChecker( + DirManagerCustomMock{ + list: func(dir string, hiddenDir bool) ([]string, error) { + return []string{""}, nil + }, + }, + fileReaderMock{}, + ) + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { o := NewAddRepoCmd( @@ -176,7 +185,7 @@ func Test_addRepoCmd_runPrompt(t *testing.T) { tt.fields.InputBool, tt.fields.InputInt, TutorialFinderMock{}, - tree.CheckerManager{}, + checkerManager, ) o.PersistentFlags().Bool("stdin", false, "input by stdin") if err := o.Execute(); (err != nil) != tt.wantErr { diff --git a/pkg/cmd/mocks_test.go b/pkg/cmd/mocks_test.go index 7725760ba..5ad5ded93 100644 --- a/pkg/cmd/mocks_test.go +++ b/pkg/cmd/mocks_test.go @@ -20,11 +20,11 @@ import ( "errors" "io" + "github.com/spf13/cobra" + "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/git" - "github.com/spf13/cobra" - "github.com/ZupIT/ritchie-cli/pkg/autocomplete" "github.com/ZupIT/ritchie-cli/pkg/credential" "github.com/ZupIT/ritchie-cli/pkg/formula" @@ -580,4 +580,4 @@ func (m RepositoryListUpdaterCustomMock) List() (formula.Repos, error) { func (m RepositoryListUpdaterCustomMock) Update(name formula.RepoName, version formula.RepoVersion) error { return m.update(name, version) -} +} \ No newline at end of file diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index d2bccf82d..d33dd73b7 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -13,12 +13,12 @@ import ( ) type CheckerManager struct { - dir stream.DirLister + Dir stream.DirLister file stream.FileReader } func NewChecker(dir stream.DirLister, file stream.FileReader) CheckerManager { - return CheckerManager{dir: dir, file: file} + return CheckerManager{Dir: dir, file: file} } // CheckCommands is used to warn the user about conflicting @@ -36,7 +36,7 @@ func (cm CheckerManager) CheckCommands() { func (cm CheckerManager) readCommands() []formula.Tree { repoDir := filepath.Join(api.RitchieHomeDir(), "repos") - repos, _ := cm.dir.List(repoDir, false) + repos, _ := cm.Dir.List(repoDir, false) tree := formula.Tree{} var treeArr []formula.Tree for _, r := range repos { From 53eac84f33a0a95f585c74fa523f10aee99ad0db Mon Sep 17 00:00:00 2001 From: Victor Schumacher <51962831+victorschumacherzup@users.noreply.github.com> Date: Tue, 3 Nov 2020 14:51:53 -0300 Subject: [PATCH 17/26] Apply suggestions from code review Co-authored-by: henriquemoraeszup <68074718+henriquemoraeszup@users.noreply.github.com> --- pkg/formula/tree/checker.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index d33dd73b7..5cae8864c 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -22,8 +22,8 @@ func NewChecker(dir stream.DirLister, file stream.FileReader) CheckerManager { } // CheckCommands is used to warn the user about conflicting -// formula commands on different repos. This function don't -// return err because print an error because of a unsuccessful +// formula commands on different repos. This function doesn't +// return an error because printing an error from a unsuccessful // warning attempt can be confusing to the user. func (cm CheckerManager) CheckCommands() { trees := cm.readCommands() @@ -80,7 +80,7 @@ func (cm CheckerManager) printConflictingCommandsWarning(conflictingCommands []s lastCommand := conflictingCommands[lastCommandIndex] lastCommand = strings.Replace(lastCommand, "root", "rit", 1) lastCommand = strings.ReplaceAll(lastCommand, "_", " ") - msg := fmt.Sprintf("The following formula command are conflicting: %s", lastCommand) + msg := fmt.Sprintf("The following formula commands are conflicting: %s", lastCommand) msg = prompt.Yellow(msg) fmt.Println(msg) } From 165f5d2918b6ea77f3231136a0311b1bc652bb96 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Wed, 4 Nov 2020 16:44:24 -0300 Subject: [PATCH 18/26] using treeManager now Signed-off-by: victor-schumacher --- pkg/cmd/add_repo.go | 2 +- pkg/cmd/add_repo_test.go | 9 +-- pkg/cmd/create_formula.go | 2 +- pkg/cmd/create_formula_test.go | 2 +- pkg/commands/builder.go | 2 +- pkg/formula/formula.go | 1 + pkg/formula/input/prompt/prompt.go | 10 ++-- pkg/formula/tree.go | 2 +- pkg/formula/tree/checker.go | 42 +++++--------- pkg/formula/tree/checker_test.go | 91 +++++++++++++----------------- 10 files changed, 64 insertions(+), 99 deletions(-) diff --git a/pkg/cmd/add_repo.go b/pkg/cmd/add_repo.go index 90b41b923..6af6b3ddf 100644 --- a/pkg/cmd/add_repo.go +++ b/pkg/cmd/add_repo.go @@ -189,7 +189,7 @@ func (ad addRepoCmd) runPrompt() CommandRunnerFunc { return err } tutorialAddRepo(tutorialHolder.Current) - ad.treeChecker.CheckCommands() + ad.treeChecker.Check() return nil } } diff --git a/pkg/cmd/add_repo_test.go b/pkg/cmd/add_repo_test.go index ff9ee16d1..890d0f2bb 100644 --- a/pkg/cmd/add_repo_test.go +++ b/pkg/cmd/add_repo_test.go @@ -164,14 +164,7 @@ func Test_addRepoCmd_runPrompt(t *testing.T) { wantErr: true, }, } - checkerManager := tree.NewChecker( - DirManagerCustomMock{ - list: func(dir string, hiddenDir bool) ([]string, error) { - return []string{""}, nil - }, - }, - fileReaderMock{}, - ) + checkerManager := tree.NewChecker(treeMock{}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 915a2f48d..8483abb25 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -134,7 +134,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { WorkspacePath: wspace.Dir, FormulaPath: formulaPath, } - c.treeChecker.CheckCommands() + c.treeChecker.Check() c.create(cf, wspace.Dir, formulaPath) return nil } diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index 55d314a8f..31a47c670 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -31,7 +31,7 @@ func TestNewCreateFormulaCmd(t *testing.T) { fileManager := stream.NewFileManager() dirManager := stream.NewDirManager(fileManager) tplM := template.NewManager("../../testdata", dirManager) - tChecker := tree.NewChecker(dirManager, fileManager) + tChecker := tree.NewChecker(treeMock{}) cmd := NewCreateFormulaCmd( os.TempDir(), formCreator{}, diff --git a/pkg/commands/builder.go b/pkg/commands/builder.go index 28205d85a..ee12fea09 100644 --- a/pkg/commands/builder.go +++ b/pkg/commands/builder.go @@ -121,7 +121,7 @@ func Build() *cobra.Command { credSetter := credential.NewSetter(ritchieHomeDir, ctxFinder) credFinder := credential.NewFinder(ritchieHomeDir, ctxFinder, fileManager) treeManager := tree.NewTreeManager(ritchieHomeDir, repoLister, api.CoreCmds, fileManager, repoProviders, isRootCommand) - treeChecker := tree.NewChecker(dirManager, fileManager) + treeChecker := tree.NewChecker(treeManager) credSettings := credential.NewSettings(fileManager, dirManager, userHomeDir) autocompleteGen := autocomplete.NewGenerator(treeManager) credResolver := envcredential.NewResolver(credFinder, credSetter, inputPassword) diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 77c8f0b8d..8f84e6f87 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -118,6 +118,7 @@ type ( InType api.TermInputType RunType RunnerType Verbose bool + Default bool Flags *pflag.FlagSet } ) diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index 296087303..f3d770252 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -76,7 +76,7 @@ func NewInputManager( } } -func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, _ *pflag.FlagSet) error { +func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, e *pflag.FlagSet) error { config := setup.Config for _, i := range config.Inputs { var inputVal string @@ -93,6 +93,8 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, _ *pflag.FlagS continue } + // fmt.Println(e.Lookup("--default")) + switch iType := i.Type; iType { case input.TextType: if items != nil { @@ -125,18 +127,18 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, _ *pflag.FlagS if len(inputVal) != 0 { in.persistCache(setup.FormulaPath, inputVal, i, items) - checkForSameEnv(i.Name) + in.checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) } } return nil } -func checkForSameEnv(envKey string){ +func (in InputManager) checkForSameEnv(envKey string) { envKey = strings.ToUpper(envKey) if _, exist := os.LookupEnv(envKey); exist { warnMsg := fmt.Sprintf( - "The input param %s has the same name of a machine variable." + + "The input param %s has the same name of a machine variable."+ " It will probably result on unexpect behavior", envKey) prompt.Warning(warnMsg) } diff --git a/pkg/formula/tree.go b/pkg/formula/tree.go index 00cc756a9..4f7edd572 100644 --- a/pkg/formula/tree.go +++ b/pkg/formula/tree.go @@ -32,5 +32,5 @@ type TreeGenerator interface { } type TreeChecker interface { - CheckCommands() + Check() } \ No newline at end of file diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 5cae8864c..4595e4a19 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -1,57 +1,41 @@ package tree import ( - "encoding/json" "fmt" - "path/filepath" "strings" - "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/formula" "github.com/ZupIT/ritchie-cli/pkg/prompt" - "github.com/ZupIT/ritchie-cli/pkg/stream" ) type CheckerManager struct { - Dir stream.DirLister - file stream.FileReader + tree formula.TreeManager } -func NewChecker(dir stream.DirLister, file stream.FileReader) CheckerManager { - return CheckerManager{Dir: dir, file: file} +func NewChecker( + tree formula.TreeManager, +) CheckerManager { + return CheckerManager{ + tree: tree, + } } // CheckCommands is used to warn the user about conflicting // formula commands on different repos. This function doesn't // return an error because printing an error from a unsuccessful // warning attempt can be confusing to the user. -func (cm CheckerManager) CheckCommands() { - trees := cm.readCommands() - commands := cm.filterCommands(trees) +func (cm CheckerManager) Check() { + commands := cm.filterCommands() conflictingCommands := cm.conflictingCommands(commands) if len(conflictingCommands) > 1 { cm.printConflictingCommandsWarning(conflictingCommands) } } -func (cm CheckerManager) readCommands() []formula.Tree { - repoDir := filepath.Join(api.RitchieHomeDir(), "repos") - repos, _ := cm.Dir.List(repoDir, false) - tree := formula.Tree{} - var treeArr []formula.Tree - for _, r := range repos { - path := fmt.Sprintf(treeRepoCmdPattern, api.RitchieHomeDir(), r) - bytes, _ := cm.file.Read(path) - _ = json.Unmarshal(bytes, &tree) - treeArr = append(treeArr, tree) - tree = formula.Tree{} - } - - return treeArr -} - -func (cm CheckerManager) filterCommands(tree []formula.Tree) []string { +func (cm CheckerManager) filterCommands() []string { allCommands := []string{""} + tree, _ := cm.tree.Tree() + for _, t := range tree { for _, c := range t.Commands { allCommands = append(allCommands, c.Id) @@ -80,7 +64,7 @@ func (cm CheckerManager) printConflictingCommandsWarning(conflictingCommands []s lastCommand := conflictingCommands[lastCommandIndex] lastCommand = strings.Replace(lastCommand, "root", "rit", 1) lastCommand = strings.ReplaceAll(lastCommand, "_", " ") - msg := fmt.Sprintf("The following formula commands are conflicting: %s", lastCommand) + msg := fmt.Sprintf("The following formula command are conflicting: %s", lastCommand) msg = prompt.Yellow(msg) fmt.Println(msg) } diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index 4b6d2e693..a1dfd1854 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -3,79 +3,64 @@ package tree import ( "testing" - "github.com/ZupIT/ritchie-cli/pkg/stream" - sMocks "github.com/ZupIT/ritchie-cli/pkg/stream/mocks" + "github.com/ZupIT/ritchie-cli/pkg/api" + "github.com/ZupIT/ritchie-cli/pkg/formula" ) + + func TestChecker(t *testing.T) { - treeJson := `{ - "commands": [ - { - "id": "root_aws_create_bucket", - "parent": "root_aws_create", - "usage": "bucket", - "help": "short help placeholder for bucket", - "longHelp": "long help placeholder for bucket used by index page and -h", - "formula": true - }, - { - "id": "root_aws_create_bucket", - "parent": "root_aws_create", - "usage": "bucket", - "help": "short help placeholder for bucket", - "longHelp": "long help placeholder for bucket used by index page and -h", - "formula": true - } - ] - } -` + treeMock := treeMock{ + tree: formula.Tree{ + Commands: api.Commands{ + { + Id: "root_mock", + Parent: "root", + Usage: "mock", + Help: "mock for add", + }, + { + Id: "root_mock", + Parent: "root_mock", + Usage: "test", + Help: "test for add", + Formula: true, + }, + }, + }, + } tests := []struct{ name string - file stream.FileReader - dir DirManagerCustomMock }{ { name: "Should success run", - file : sMocks.FileReaderCustomMock{ - ReadMock: func(path string) ([]byte, error) { - return []byte(treeJson), nil - }, - }, - dir: DirManagerCustomMock{ - list: func(dir string, hiddenDir bool) ([]string, error) { - return []string{"commons"}, nil - }, - }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - treeChecker := NewChecker(tt.dir, tt.file) - treeChecker.CheckCommands() + + treeChecker := NewChecker(treeMock) + treeChecker.Check() }) } } -type DirManagerCustomMock struct { - exists func(dir string) bool - list func(dir string, hiddenDir bool) ([]string, error) - isDir func(dir string) bool - create func(dir string) error -} - -func (d DirManagerCustomMock) Exists(dir string) bool { - return d.exists(dir) -} -func (d DirManagerCustomMock) List(dir string, hiddenDir bool) ([]string, error) { - return d.list(dir, hiddenDir) +type treeMock struct { + tree formula.Tree + error error + value string } -func (d DirManagerCustomMock) IsDir(dir string) bool { - return d.isDir(dir) +func (t treeMock) Tree() (map[string]formula.Tree, error) { + if t.value != "" { + return map[string]formula.Tree{t.value: t.tree}, t.error + } + return map[string]formula.Tree{"test": t.tree}, t.error } -func (d DirManagerCustomMock) Create(dir string) error { - return d.create(dir) +func (t treeMock) MergedTree(bool) formula.Tree { + return t.tree } \ No newline at end of file From ec5dc1eede005db4465876d1246bc22adee685b4 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 6 Nov 2020 10:15:43 -0300 Subject: [PATCH 19/26] removed miss code Signed-off-by: victor-schumacher --- metrics.json | 7 +++++++ pkg/formula/formula.go | 1 - pkg/formula/input/prompt/prompt.go | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 metrics.json diff --git a/metrics.json b/metrics.json new file mode 100644 index 000000000..518ec36af --- /dev/null +++ b/metrics.json @@ -0,0 +1,7 @@ +{ + + + + + +} \ No newline at end of file diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 8f84e6f87..77c8f0b8d 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -118,7 +118,6 @@ type ( InType api.TermInputType RunType RunnerType Verbose bool - Default bool Flags *pflag.FlagSet } ) diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index f3d770252..ab75ce970 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -76,7 +76,7 @@ func NewInputManager( } } -func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, e *pflag.FlagSet) error { +func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, _ *pflag.FlagSet) error { config := setup.Config for _, i := range config.Inputs { var inputVal string From 8177672865db1bb0231203e4706c1ad114164912 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 6 Nov 2020 10:18:57 -0300 Subject: [PATCH 20/26] addde total commands Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 4595e4a19..321b30979 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -64,7 +64,7 @@ func (cm CheckerManager) printConflictingCommandsWarning(conflictingCommands []s lastCommand := conflictingCommands[lastCommandIndex] lastCommand = strings.Replace(lastCommand, "root", "rit", 1) lastCommand = strings.ReplaceAll(lastCommand, "_", " ") - msg := fmt.Sprintf("The following formula command are conflicting: %s", lastCommand) + msg := fmt.Sprintf("There's a total of %d formula conflicting commands, like:\n %s", len(conflictingCommands), lastCommand) msg = prompt.Yellow(msg) fmt.Println(msg) } From ff1b177a42f1ae87b4b362b0f25c48a7423a5138 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 6 Nov 2020 12:20:04 -0300 Subject: [PATCH 21/26] remove changes Signed-off-by: victor-schumacher --- metrics.json | 7 ------- pkg/formula/input/prompt/prompt.go | 8 +++----- 2 files changed, 3 insertions(+), 12 deletions(-) delete mode 100644 metrics.json diff --git a/metrics.json b/metrics.json deleted file mode 100644 index 518ec36af..000000000 --- a/metrics.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - - - - - -} \ No newline at end of file diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index ab75ce970..296087303 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -93,8 +93,6 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, _ *pflag.FlagS continue } - // fmt.Println(e.Lookup("--default")) - switch iType := i.Type; iType { case input.TextType: if items != nil { @@ -127,18 +125,18 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, _ *pflag.FlagS if len(inputVal) != 0 { in.persistCache(setup.FormulaPath, inputVal, i, items) - in.checkForSameEnv(i.Name) + checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) } } return nil } -func (in InputManager) checkForSameEnv(envKey string) { +func checkForSameEnv(envKey string){ envKey = strings.ToUpper(envKey) if _, exist := os.LookupEnv(envKey); exist { warnMsg := fmt.Sprintf( - "The input param %s has the same name of a machine variable."+ + "The input param %s has the same name of a machine variable." + " It will probably result on unexpect behavior", envKey) prompt.Warning(warnMsg) } From 172adb3c23dd1aef6dc21267a81fc537aa965a55 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 6 Nov 2020 12:21:32 -0300 Subject: [PATCH 22/26] remove comments Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 321b30979..52f1dbcd3 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -50,10 +50,10 @@ func (cm CheckerManager) conflictingCommands(commands []string) []string { for _, item := range commands { _, exist := duplicateFrequency[item] if exist { - duplicateFrequency[item] += 1 // increase counter by 1 if already in the map + duplicateFrequency[item] += 1 duplicatedCommands = append(duplicatedCommands, item) } else { - duplicateFrequency[item] = 1 // else start counting from 1 + duplicateFrequency[item] = 1 } } return duplicatedCommands From 6a9be03faebf5ea2a5c4b14925f7aaf667c0c50d Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 6 Nov 2020 13:05:13 -0300 Subject: [PATCH 23/26] added a check for output Signed-off-by: victor-schumacher --- pkg/formula/tree/checker_test.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index a1dfd1854..ba6434d07 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -1,14 +1,15 @@ package tree import ( + "io/ioutil" + "os" + "strings" "testing" "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/formula" ) - - func TestChecker(t *testing.T) { treeMock := treeMock{ tree: formula.Tree{ @@ -30,22 +31,37 @@ func TestChecker(t *testing.T) { }, } - tests := []struct{ + tests := []struct { name string }{ { - name: "Should success run", - + name: "Should warn conflicting commands", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + out := captureCheckerStdout(treeMock) + if !strings.Contains(out, "rit mock") { + t.Error("Wrong output on tree checker function") + } - treeChecker := NewChecker(treeMock) - treeChecker.Check() }) } +} +func captureCheckerStdout(tree formula.TreeManager) string { + treeChecker := NewChecker(tree) + + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + treeChecker.Check() + + _ = w.Close() + out, _ := ioutil.ReadAll(r) + os.Stdout = rescueStdout + return string(out) } type treeMock struct { @@ -63,4 +79,4 @@ func (t treeMock) Tree() (map[string]formula.Tree, error) { func (t treeMock) MergedTree(bool) formula.Tree { return t.tree -} \ No newline at end of file +} From 56e1fb4180f8bd17323bb22163d8e95a12f7fd11 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 6 Nov 2020 17:08:46 -0300 Subject: [PATCH 24/26] syntax and formula check Signed-off-by: victor-schumacher --- pkg/cmd/add_repo.go | 14 +++++++------- pkg/cmd/create_formula.go | 12 ++++++------ pkg/formula/tree/checker.go | 18 ++++++++++-------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/pkg/cmd/add_repo.go b/pkg/cmd/add_repo.go index 6af6b3ddf..b709b44c8 100644 --- a/pkg/cmd/add_repo.go +++ b/pkg/cmd/add_repo.go @@ -43,8 +43,8 @@ type addRepoCmd struct { prompt.InputList prompt.InputBool prompt.InputInt - rt rtutorial.Finder - treeChecker tree.CheckerManager + tutorial rtutorial.Finder + tree tree.CheckerManager } func NewAddRepoCmd( @@ -68,8 +68,8 @@ func NewAddRepoCmd( InputBool: inBool, InputInt: inInt, InputPassword: inPass, - rt: rtf, - treeChecker: treeChecker, + tutorial: rtf, + tree: treeChecker, } cmd := &cobra.Command{ Use: "repo", @@ -184,12 +184,12 @@ func (ad addRepoCmd) runPrompt() CommandRunnerFunc { successMsg := fmt.Sprintf("The %q repository was added with success, now you can use your formulas with the Ritchie!", repository.Name) prompt.Success(successMsg) - tutorialHolder, err := ad.rt.Find() + tutorialHolder, err := ad.tutorial.Find() if err != nil { return err } tutorialAddRepo(tutorialHolder.Current) - ad.treeChecker.Check() + ad.tree.Check() return nil } } @@ -211,7 +211,7 @@ func (ad addRepoCmd) runStdin() CommandRunnerFunc { successMsg := fmt.Sprintf("The %q repository was added with success, now you can use your formulas with the Ritchie!", r.Name) prompt.Success(successMsg) - tutorialHolder, err := ad.rt.Find() + tutorialHolder, err := ad.tutorial.Find() if err != nil { return err } diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 8483abb25..01c0d5df9 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -45,8 +45,8 @@ type createFormulaCmd struct { inTextValidator prompt.InputTextValidator inList prompt.InputList tplM template.Manager - rt rtutorial.Finder - treeChecker tree.CheckerManager + tutorial rtutorial.Finder + tree tree.CheckerManager } // CreateFormulaCmd creates a new cmd instance @@ -69,8 +69,8 @@ func NewCreateFormulaCmd( inTextValidator: inTextValidator, inList: inList, tplM: tplM, - rt: rtf, - treeChecker: treeChecker, + tutorial: rtf, + tree: treeChecker, } cmd := &cobra.Command{ @@ -134,7 +134,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { WorkspacePath: wspace.Dir, FormulaPath: formulaPath, } - c.treeChecker.Check() + c.tree.Check() c.create(cf, wspace.Dir, formulaPath) return nil } @@ -175,7 +175,7 @@ func (c createFormulaCmd) create(cf formula.Create, workspacePath, formulaPath s return } - tutorialHolder, err := c.rt.Find() + tutorialHolder, err := c.tutorial.Find() if err != nil { s.Error(err) return diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 52f1dbcd3..447ba2cd1 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -33,12 +33,14 @@ func (cm CheckerManager) Check() { } func (cm CheckerManager) filterCommands() []string { - allCommands := []string{""} + var allCommands []string tree, _ := cm.tree.Tree() for _, t := range tree { for _, c := range t.Commands { - allCommands = append(allCommands, c.Id) + if c.Formula { + allCommands = append(allCommands, c.Id) + } } } return allCommands @@ -46,14 +48,14 @@ func (cm CheckerManager) filterCommands() []string { func (cm CheckerManager) conflictingCommands(commands []string) []string { duplicateFrequency := make(map[string]int) - duplicatedCommands := []string{""} - for _, item := range commands { - _, exist := duplicateFrequency[item] + var duplicatedCommands []string + for _, command := range commands { + _, exist := duplicateFrequency[command] if exist { - duplicateFrequency[item] += 1 - duplicatedCommands = append(duplicatedCommands, item) + duplicateFrequency[command] += 1 + duplicatedCommands = append(duplicatedCommands, command) } else { - duplicateFrequency[item] = 1 + duplicateFrequency[command] = 1 } } return duplicatedCommands From a982635c456a7b1901ca7f0a136b5512e7d8610d Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Mon, 9 Nov 2020 09:18:22 -0300 Subject: [PATCH 25/26] fix tests Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 4 ++-- pkg/formula/tree/checker_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index 447ba2cd1..c69a51419 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -27,7 +27,7 @@ func NewChecker( func (cm CheckerManager) Check() { commands := cm.filterCommands() conflictingCommands := cm.conflictingCommands(commands) - if len(conflictingCommands) > 1 { + if len(conflictingCommands) >= 1 { cm.printConflictingCommandsWarning(conflictingCommands) } } @@ -35,7 +35,6 @@ func (cm CheckerManager) Check() { func (cm CheckerManager) filterCommands() []string { var allCommands []string tree, _ := cm.tree.Tree() - for _, t := range tree { for _, c := range t.Commands { if c.Formula { @@ -58,6 +57,7 @@ func (cm CheckerManager) conflictingCommands(commands []string) []string { duplicateFrequency[command] = 1 } } + fmt.Println(duplicatedCommands) return duplicatedCommands } diff --git a/pkg/formula/tree/checker_test.go b/pkg/formula/tree/checker_test.go index ba6434d07..44ca27ac7 100644 --- a/pkg/formula/tree/checker_test.go +++ b/pkg/formula/tree/checker_test.go @@ -19,6 +19,7 @@ func TestChecker(t *testing.T) { Parent: "root", Usage: "mock", Help: "mock for add", + Formula: true, }, { Id: "root_mock", @@ -44,7 +45,6 @@ func TestChecker(t *testing.T) { if !strings.Contains(out, "rit mock") { t.Error("Wrong output on tree checker function") } - }) } } From 7feac77341a6a92e4ed5fbaefd76b4a08633278f Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Mon, 9 Nov 2020 09:52:23 -0300 Subject: [PATCH 26/26] remove println Signed-off-by: victor-schumacher --- pkg/formula/tree/checker.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/formula/tree/checker.go b/pkg/formula/tree/checker.go index c69a51419..27e72a844 100644 --- a/pkg/formula/tree/checker.go +++ b/pkg/formula/tree/checker.go @@ -57,7 +57,6 @@ func (cm CheckerManager) conflictingCommands(commands []string) []string { duplicateFrequency[command] = 1 } } - fmt.Println(duplicatedCommands) return duplicatedCommands }