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

warning when formulas commands conflicts #638

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
19d8d93
add reading and filtering
victor-schumacher Oct 27, 2020
3f21521
adding
victor-schumacher Oct 27, 2020
7fde33e
finding and printing conflicts
victor-schumacher Oct 29, 2020
d507737
add basic test
victor-schumacher Oct 29, 2020
4f5b39d
fix lint
victor-schumacher Oct 29, 2020
b2011fd
remove spaces
victor-schumacher Oct 29, 2020
a674ca6
merge master
victor-schumacher Oct 30, 2020
19768d8
fix main
victor-schumacher Oct 30, 2020
7da4ee9
little refactor
victor-schumacher Oct 30, 2020
a5e35d3
little refactor
victor-schumacher Oct 30, 2020
6265bff
added a better test
victor-schumacher Oct 30, 2020
5c3ce8f
added a repeated command
victor-schumacher Oct 30, 2020
1de7820
added dir
victor-schumacher Oct 30, 2020
b3fbcc0
add check for no conflicting commands
victor-schumacher Oct 30, 2020
31fafd8
added tree check to add repo
victor-schumacher Nov 3, 2020
0aede6f
fix empty warning
victor-schumacher Nov 3, 2020
b5b10db
fixing tests
victor-schumacher Nov 3, 2020
53eac84
Apply suggestions from code review
victorschumacherzup Nov 3, 2020
165f5d2
using treeManager now
victor-schumacher Nov 4, 2020
ec5dc1e
removed miss code
victor-schumacher Nov 6, 2020
8177672
addde total commands
victor-schumacher Nov 6, 2020
ff1b177
remove changes
victor-schumacher Nov 6, 2020
172adb3
remove comments
victor-schumacher Nov 6, 2020
6a9be03
added a check for output
victor-schumacher Nov 6, 2020
56e1fb4
syntax and formula check
victor-schumacher Nov 6, 2020
a982635
fix tests
victor-schumacher Nov 9, 2020
7feac77
remove println
victor-schumacher Nov 9, 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
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ func main() {
os.Exit(1)
}
commands.SendMetric(commands.ExecutionTime(startTime))
}
}
13 changes: 9 additions & 4 deletions pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,7 +43,8 @@ type addRepoCmd struct {
prompt.InputList
prompt.InputBool
prompt.InputInt
rt rtutorial.Finder
tutorial rtutorial.Finder
tree tree.CheckerManager
}

func NewAddRepoCmd(
Expand All @@ -55,6 +57,7 @@ func NewAddRepoCmd(
inBool prompt.InputBool,
inInt prompt.InputInt,
rtf rtutorial.Finder,
treeChecker tree.CheckerManager,
) *cobra.Command {
addRepo := addRepoCmd{
repo: repo,
Expand All @@ -65,7 +68,8 @@ func NewAddRepoCmd(
InputBool: inBool,
InputInt: inInt,
InputPassword: inPass,
rt: rtf,
tutorial: rtf,
tree: treeChecker,
}
cmd := &cobra.Command{
Use: "repo",
Expand Down Expand Up @@ -180,11 +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.tree.Check()
return nil
}
}
Expand All @@ -206,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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/add_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -163,6 +164,8 @@ func Test_addRepoCmd_runPrompt(t *testing.T) {
wantErr: true,
},
}
checkerManager := tree.NewChecker(treeMock{})

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := NewAddRepoCmd(
Expand All @@ -175,6 +178,7 @@ func Test_addRepoCmd_runPrompt(t *testing.T) {
tt.fields.InputBool,
tt.fields.InputInt,
TutorialFinderMock{},
checkerManager,
)
o.PersistentFlags().Bool("stdin", false, "input by stdin")
if err := o.Execute(); (err != nil) != tt.wantErr {
Expand Down
12 changes: 8 additions & 4 deletions pkg/cmd/create_formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -44,7 +45,8 @@ type createFormulaCmd struct {
inTextValidator prompt.InputTextValidator
inList prompt.InputList
tplM template.Manager
rt rtutorial.Finder
tutorial rtutorial.Finder
tree tree.CheckerManager
}

// CreateFormulaCmd creates a new cmd instance
Expand All @@ -57,6 +59,7 @@ func NewCreateFormulaCmd(
inTextValidator prompt.InputTextValidator,
inList prompt.InputList,
rtf rtutorial.Finder,
treeChecker tree.CheckerManager,
) *cobra.Command {
c := createFormulaCmd{
homeDir: homeDir,
Expand All @@ -66,7 +69,8 @@ func NewCreateFormulaCmd(
inTextValidator: inTextValidator,
inList: inList,
tplM: tplM,
rt: rtf,
tutorial: rtf,
tree: treeChecker,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -130,7 +134,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc {
WorkspacePath: wspace.Dir,
FormulaPath: formulaPath,
}

c.tree.Check()
c.create(cf, wspace.Dir, formulaPath)
return nil
}
Expand Down Expand Up @@ -171,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
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -30,6 +31,7 @@ func TestNewCreateFormulaCmd(t *testing.T) {
fileManager := stream.NewFileManager()
dirManager := stream.NewDirManager(fileManager)
tplM := template.NewManager("../../testdata", dirManager)
tChecker := tree.NewChecker(treeMock{})
cmd := NewCreateFormulaCmd(
os.TempDir(),
formCreator{},
Expand All @@ -39,6 +41,7 @@ func TestNewCreateFormulaCmd(t *testing.T) {
inputTextValidatorMock{},
inputListMock{},
TutorialFinderMock{},
tChecker,
)
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
if cmd == nil {
Expand Down Expand Up @@ -125,6 +128,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 {
Expand Down Expand Up @@ -154,4 +158,4 @@ func (tm TemplateManagerCustomMock) ResolverNewPath(oldPath, newDir, lang, works
}
func (tm TemplateManagerCustomMock) Validate() error {
return tm.ValidateMock()
}
}
6 changes: 3 additions & 3 deletions pkg/cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
5 changes: 3 additions & 2 deletions pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(treeManager)
credSettings := credential.NewSettings(fileManager, dirManager, userHomeDir)
autocompleteGen := autocomplete.NewGenerator(treeManager)
credResolver := envcredential.NewResolver(credFinder, credSetter, inputPassword)
Expand Down Expand Up @@ -207,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)
Expand All @@ -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)
victorschumacherzup marked this conversation as resolved.
Show resolved Hide resolved
buildFormulaCmd := cmd.NewBuildFormulaCmd(userHomeDir, formulaLocalBuilder, formulaWorkspace, watchManager, dirManager, inputText, inputList, tutorialFinder)
showFormulaRunnerCmd := cmd.NewShowFormulaRunnerCmd(configManager)
setFormulaRunnerCmd := cmd.NewSetFormulaRunnerCmd(configManager, inputList)
Expand Down
4 changes: 4 additions & 0 deletions pkg/formula/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ type TreeManager interface {
type TreeGenerator interface {
Generate(repoPath string) (Tree, error)
}

type TreeChecker interface {
Check()
}
71 changes: 71 additions & 0 deletions pkg/formula/tree/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tree

import (
"fmt"
"strings"

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

type CheckerManager struct {
tree formula.TreeManager
}

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) Check() {
commands := cm.filterCommands()
conflictingCommands := cm.conflictingCommands(commands)
if len(conflictingCommands) >= 1 {
cm.printConflictingCommandsWarning(conflictingCommands)
}
}

func (cm CheckerManager) filterCommands() []string {
var allCommands []string
tree, _ := cm.tree.Tree()
for _, t := range tree {
for _, c := range t.Commands {
if c.Formula {
allCommands = append(allCommands, c.Id)
}
}
}
return allCommands
}

func (cm CheckerManager) conflictingCommands(commands []string) []string {
duplicateFrequency := make(map[string]int)
var duplicatedCommands []string
for _, command := range commands {
_, exist := duplicateFrequency[command]
if exist {
duplicateFrequency[command] += 1
duplicatedCommands = append(duplicatedCommands, command)
} else {
duplicateFrequency[command] = 1
}
}
return duplicatedCommands
}

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("There's a total of %d formula conflicting commands, like:\n %s", len(conflictingCommands), lastCommand)
msg = prompt.Yellow(msg)
fmt.Println(msg)
}
82 changes: 82 additions & 0 deletions pkg/formula/tree/checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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{
Commands: api.Commands{
{
Id: "root_mock",
Parent: "root",
Usage: "mock",
Help: "mock for add",
Formula: true,
},
{
Id: "root_mock",
Parent: "root_mock",
Usage: "test",
Help: "test for add",
Formula: true,
},
},
},
}

tests := []struct {
name string
}{
{
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")
}
})
}
}

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 {
tree formula.Tree
error error
value string
}

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 (t treeMock) MergedTree(bool) formula.Tree {
return t.tree
}
Loading