From 91d57e0fecffa5af9eb3e421417bae5dfdd5f9dc Mon Sep 17 00:00:00 2001 From: Aron Richter Date: Sat, 19 Dec 2020 18:50:23 -0300 Subject: [PATCH] created rit add workspace command Signed-off-by: Aron Richter --- internal/mocks/mocks.go | 14 +++++ pkg/cmd/add_workspace.go | 103 ++++++++++++++++++++++++++++++++++ pkg/cmd/add_workspace_test.go | 83 +++++++++++++++++++++++++++ pkg/commands/builder.go | 3 +- 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 pkg/cmd/add_workspace.go create mode 100644 pkg/cmd/add_workspace_test.go diff --git a/internal/mocks/mocks.go b/internal/mocks/mocks.go index aff6b9c23..546756e97 100644 --- a/internal/mocks/mocks.go +++ b/internal/mocks/mocks.go @@ -354,3 +354,17 @@ func (tm *TemplateManagerMock) Validate() error { args := tm.Called() return args.Error(0) } + +type WorkspaceMock struct { + mock.Mock +} + +func (w *WorkspaceMock) Add(workspace formula.Workspace) error { + args := w.Called(workspace) + return args.Error(0) +} + +func (w *WorkspaceMock) List() (formula.Workspaces, error) { + args := w.Called() + return args.Get(0).(formula.Workspaces), args.Error(1) +} diff --git a/pkg/cmd/add_workspace.go b/pkg/cmd/add_workspace.go new file mode 100644 index 000000000..e14c90e1b --- /dev/null +++ b/pkg/cmd/add_workspace.go @@ -0,0 +1,103 @@ +package cmd + +import ( + "errors" + "strings" + + "github.com/spf13/cobra" + + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/prompt" +) + +const ( + workspaceNameFlag = "name" + workspaceNameFlagDescription = "Workspace name" + workspacePathFlag = "path" + workspacePathFlagDescription = "Workspace path" +) + +type addWorkspaceCmd struct { + workspace formula.WorkspaceAddLister + inText prompt.InputText +} + +func NewAddWorkspaceCmd( + workspace formula.WorkspaceAddLister, + inText prompt.InputText, +) *cobra.Command { + a := addWorkspaceCmd{ + workspace: workspace, + inText: inText, + } + + cmd := &cobra.Command{ + Use: "workspace", + Short: "Add new workspace", + Example: "rit add workspace", + RunE: a.runFormula(), + ValidArgs: []string{""}, + Args: cobra.OnlyValidArgs, + } + + flags := cmd.Flags() + flags.String(workspaceNameFlag, "", workspaceNameFlagDescription) + flags.String(workspacePathFlag, "", workspacePathFlagDescription) + + return cmd +} + +func (a addWorkspaceCmd) runFormula() CommandRunnerFunc { + return func(cmd *cobra.Command, args []string) error { + if count := cmd.Flags().NFlag(); count == 0 { + wspace, err := addWorkspaceFromPrompt(a) + if err != nil { + return err + } + + return a.workspace.Add(wspace) + } + + wspace, err := addWorkspaceFromFlags(cmd) + if err != nil { + return err + } + + return a.workspace.Add(wspace) + } +} + +func addWorkspaceFromFlags(cmd *cobra.Command) (formula.Workspace, error) { + workspaceName, _ := cmd.Flags().GetString(workspaceNameFlag) + workspacePath, _ := cmd.Flags().GetString(workspacePathFlag) + + if len(workspaceName) == 0 || len(workspacePath) == 0 { + return formula.Workspace{}, errors.New("all flags need to be filled") + } + + wspace := formula.Workspace{ + Name: strings.Title(workspaceName), + Dir: workspacePath, + } + + return wspace, nil +} + +func addWorkspaceFromPrompt(a addWorkspaceCmd) (formula.Workspace, error) { + workspaceName, err := a.inText.Text("Enter the name of workspace", true) + if err != nil { + return formula.Workspace{}, err + } + + workspacePath, err := a.inText.Text("Enter the path of workspace (e.g.: /home/user/github)", true) + if err != nil { + return formula.Workspace{}, err + } + + wspace := formula.Workspace{ + Name: strings.Title(workspaceName), + Dir: workspacePath, + } + + return wspace, nil +} diff --git a/pkg/cmd/add_workspace_test.go b/pkg/cmd/add_workspace_test.go new file mode 100644 index 000000000..f1b3b98c4 --- /dev/null +++ b/pkg/cmd/add_workspace_test.go @@ -0,0 +1,83 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + + "github.com/ZupIT/ritchie-cli/internal/mocks" + "github.com/ZupIT/ritchie-cli/pkg/formula" +) + +func TestNewAddWorkspaceCmd(t *testing.T) { + var tests = []struct { + name string + addWorkspaceWithError error + workspaceName string + workspacePath string + argsName string + argsPath string + wantErr string + }{ + { + name: "add new workspace by prompt", + addWorkspaceWithError: nil, + workspaceName: "Teste", + workspacePath: "/home/user/dir", + wantErr: "", + }, + { + name: "add new workspace by flags", + addWorkspaceWithError: nil, + workspaceName: "Teste", + workspacePath: "/home/user/dir", + argsName: "--name=Teste", + argsPath: "--path=/home/user/dir", + wantErr: "", + }, + { + name: "error when one flags is no filled", + addWorkspaceWithError: nil, + workspaceName: "Teste", + workspacePath: "", + argsName: "--name=Teste", + argsPath: "", + wantErr: "all flags need to be filled", + }, + { + name: "error when workspace does not exists", + addWorkspaceWithError: errors.New("workspace does not exists"), + workspaceName: "Teste", + workspacePath: "/home/user/dir", + wantErr: "workspace does not exists", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + textMock := &mocks.InputTextMock{} + textMock.On("Text", "Enter the name of workspace", true, mock.Anything).Return(tt.workspaceName, nil) + textMock.On("Text", "Enter the path of workspace (e.g.: /home/user/github)", true, mock.Anything).Return(tt.workspacePath, nil) + + wspace := formula.Workspace{ + Name: tt.workspaceName, + Dir: tt.workspacePath, + } + + workspaceMock := &mocks.WorkspaceMock{} + workspaceMock.On("Add", wspace).Return(tt.addWorkspaceWithError) + + addNewWorkspace := NewAddWorkspaceCmd(workspaceMock, textMock) + addNewWorkspace.SetArgs([]string{tt.argsName, tt.argsPath}) + + err := addNewWorkspace.Execute() + if err != nil { + assert.Equal(t, err.Error(), tt.wantErr) + } else { + assert.Empty(t, tt.wantErr) + } + }) + } +} diff --git a/pkg/commands/builder.go b/pkg/commands/builder.go index d2cfb0c6d..43a373926 100644 --- a/pkg/commands/builder.go +++ b/pkg/commands/builder.go @@ -237,6 +237,7 @@ func Build() *cobra.Command { autocompletePowerShell := cmd.NewAutocompletePowerShell(autocompleteGen) deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, dirManager, inputList, inputBool) deleteFormulaCmd := cmd.NewDeleteFormulaCmd(userHomeDir, ritchieHomeDir, formulaWorkspace, dirManager, inputBool, inputText, inputList, treeGen, fileManager) + addWorkspaceCmd := cmd.NewAddWorkspaceCmd(formulaWorkspace, inputText) createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, tplManager, formulaWorkspace, inputText, inputTextValidator, inputList, tutorialFinder, treeChecker) buildFormulaCmd := cmd.NewBuildFormulaCmd() @@ -244,7 +245,7 @@ func Build() *cobra.Command { setFormulaRunnerCmd := cmd.NewSetFormulaRunnerCmd(configManager, inputList) autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell) - addCmd.AddCommand(addRepoCmd) + addCmd.AddCommand(addRepoCmd, addWorkspaceCmd) updateCmd.AddCommand(updateRepoCmd) createCmd.AddCommand(createFormulaCmd) deleteCmd.AddCommand(deleteEnvCmd, deleteRepoCmd, deleteFormulaCmd, deleteWorkspaceCmd, deleteCredentialCmd)