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

Commit

Permalink
created rit add workspace command
Browse files Browse the repository at this point in the history
Signed-off-by: Aron Richter <aronrichter@gmail.com>
  • Loading branch information
aronrichter committed Dec 21, 2020
1 parent 84b0008 commit 91d57e0
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 1 deletion.
14 changes: 14 additions & 0 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
103 changes: 103 additions & 0 deletions pkg/cmd/add_workspace.go
Original file line number Diff line number Diff line change
@@ -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
}
83 changes: 83 additions & 0 deletions pkg/cmd/add_workspace_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ 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()
showFormulaRunnerCmd := cmd.NewShowFormulaRunnerCmd(configManager)
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)
Expand Down

0 comments on commit 91d57e0

Please sign in to comment.