This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aron Richter <aronrichter@gmail.com>
- Loading branch information
1 parent
84b0008
commit 91d57e0
Showing
4 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters