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

Commit

Permalink
Adding flags and tests to delete workspace (#832)
Browse files Browse the repository at this point in the history
* Adding flags and tests to delete workspace

Signed-off-by: Henrique Moraes <[email protected]>
  • Loading branch information
henriquemoraeszup authored Jan 26, 2021
1 parent d365d2e commit 88bc0a3
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 44 deletions.
1 change: 0 additions & 1 deletion pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
const (
defaultRepoURL = "https://github.com/ZupIT/ritchie-formulas"
messageExisting = "This formula repository already exists, check using \"rit list repo\""
nameFlagName = "name"
repoUrlFlagName = "repoUrl"
priorityFlagName = "priority"
tokenFlagName = "token"
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/add_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func TestAddRepoCmd(t *testing.T) {
repoProviders.Add("GitLab", formula.Git{Repos: gitRepositoryWithoutTagsMock, NewRepoInfo: github.NewRepoInfo})
repoProviders.Add("Bitbucket", formula.Git{Repos: gitRepositoryErrorsMock, NewRepoInfo: github.NewRepoInfo})

repoList := repoProviders.List()

repoTest := &formula.Repo{
Provider: "Github",
Name: "someRepo1",
Expand Down Expand Up @@ -247,7 +249,7 @@ func TestAddRepoCmd(t *testing.T) {
name: "fail flags with wrong provider",
args: []string{"--provider=github"},
fields: fields{},
want: errors.New("please select a provider from " + strings.Join(repoProviders.List(), ", ")),
want: errors.New("please select a provider from " + strings.Join(repoList, ", ")),
},
{
name: "fail flags with empty name",
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import (
const stdinWarning = "stdin commands are deprecated and will no longer be supported in future versions. Please use" +
" flags for programatic formula execution"

const (
nameFlagName = "name"
providerFlagName = "provider"
)

type flag struct {
name string
shortName string
Expand Down
7 changes: 1 addition & 6 deletions pkg/cmd/delete_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

const (
providerFlagName = "provider"
providerFlagDescription = "Provider name to delete"
)

type inputDeleteCredential struct {
provider string
}
Expand All @@ -42,7 +37,7 @@ var deleteCredentialFlags = flags{
name: providerFlagName,
kind: reflect.String,
defValue: "",
description: providerFlagDescription,
description: "Provider name to delete",
},
}

Expand Down
108 changes: 75 additions & 33 deletions pkg/cmd/delete_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"errors"
"fmt"
"path/filepath"
"reflect"
"strings"

"github.com/spf13/cobra"

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

var ErrEmptyWorkspaces = errors.New("there are no workspaces to delete")
Expand All @@ -36,24 +36,30 @@ type deleteWorkspaceCmd struct {
userHomeDir string
workspace formula.WorkspaceListDeleter
repo formula.RepositoryDeleter
dir stream.DirChecker
inList prompt.InputList
inBool prompt.InputBool
}

var deleteWorkspaceFlags = flags{
{
name: nameFlagName,
kind: reflect.String,
defValue: "",
description: "workspace name",
},
}

func NewDeleteWorkspaceCmd(
userHomeDir string,
workspace formula.WorkspaceListDeleter,
repo formula.RepositoryDeleter,
dir stream.DirChecker,
inList prompt.InputList,
inBool prompt.InputBool,
) *cobra.Command {
d := deleteWorkspaceCmd{
userHomeDir: userHomeDir,
workspace: workspace,
repo: repo,
dir: dir,
inList: inList,
inBool: inBool,
}
Expand All @@ -62,59 +68,95 @@ func NewDeleteWorkspaceCmd(
Use: "workspace",
Short: "Delete a workspace",
Example: "rit delete workspace",
RunE: d.runPrompt(),
RunE: d.runFormula(),
}

addReservedFlags(cmd.Flags(), deleteWorkspaceFlags)

return cmd
}

func (d deleteWorkspaceCmd) runPrompt() CommandRunnerFunc {
func (d *deleteWorkspaceCmd) runFormula() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
workspaces, err := d.workspace.List()
workspace, err := d.resolveInput(cmd)
if err != nil {
return err
}

defaultWorkspace := filepath.Join(d.userHomeDir, formula.DefaultWorkspaceDir)
if d.dir.Exists(defaultWorkspace) {
workspaces[formula.DefaultWorkspaceName] = defaultWorkspace
}

if len(workspaces) == 0 {
return ErrEmptyWorkspaces
}

wspace, err := WorkspaceListInput(workspaces, d.inList)
if err != nil {
repoLocalName := repoutil.LocalName(workspace.Name)
if err := d.repo.Delete(repoLocalName); err != nil {
return err
}

question := fmt.Sprintf("Are you sure you want to delete the workspace: rit %s", wspace.Dir)
ans, err := d.inBool.Bool(question, []string{"no", "yes"})
if err != nil {
return err
}
if !ans {
return nil
if workspace.Name == formula.DefaultWorkspaceName {
return errors.New("cannot delete default workspace")
}

repoLocalName := repoutil.LocalName(wspace.Name)
if err := d.repo.Delete(repoLocalName); err != nil {
if err := d.workspace.Delete(workspace); err != nil {
return err
}

if wspace.Dir != defaultWorkspace {
if err := d.workspace.Delete(wspace); err != nil {
return err
}
}

prompt.Success("Workspace successfully deleted!")

return nil
}
}

func (d *deleteWorkspaceCmd) resolveInput(cmd *cobra.Command) (formula.Workspace, error) {
if IsFlagInput(cmd) {
return d.resolveFlags(cmd)
}
return d.resolvePrompt()
}

func (d *deleteWorkspaceCmd) resolvePrompt() (formula.Workspace, error) {
workspaces, err := d.workspace.List()
if err != nil {
return formula.Workspace{}, err
}

if len(workspaces) == 0 {
return formula.Workspace{}, ErrEmptyWorkspaces
}

wspace, err := WorkspaceListInput(workspaces, d.inList)
if err != nil {
return formula.Workspace{}, err
}

question := fmt.Sprintf("Are you sure you want to delete the workspace: rit %s", wspace.Dir)
ans, err := d.inBool.Bool(question, []string{"no", "yes"})
if err != nil {
return formula.Workspace{}, err
}
if !ans {
return formula.Workspace{}, nil
}
return wspace, nil
}

func (d *deleteWorkspaceCmd) resolveFlags(cmd *cobra.Command) (formula.Workspace, error) {
name, err := cmd.Flags().GetString(nameFlagName)
if err != nil {
return formula.Workspace{}, err
} else if name == "" {
return formula.Workspace{}, errors.New("please provide a value for 'name'")
}

workspaces, err := d.workspace.List()
workspaces[formula.DefaultWorkspaceName] = filepath.Join(d.userHomeDir, formula.DefaultWorkspaceDir)
if err != nil {
return formula.Workspace{}, err
}
for workspaceName, path := range workspaces {
if workspaceName == name {
return formula.Workspace{Name: workspaceName, Dir: path}, nil
}
}

return formula.Workspace{}, errors.New("no workspace found with this name")
}

func WorkspaceListInput(
workspaces formula.Workspaces,
inList prompt.InputList,
Expand Down
47 changes: 46 additions & 1 deletion pkg/cmd/delete_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

func TestDeleteWorkspaces(t *testing.T) {
type in struct {
args []string
wspaceList formula.Workspaces
wspaceListErr error
wspaceDeleteErr error
Expand All @@ -49,6 +50,7 @@ func TestDeleteWorkspaces(t *testing.T) {
{
name: "success",
in: in{
args: []string{},
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
Expand All @@ -60,20 +62,23 @@ func TestDeleteWorkspaces(t *testing.T) {
{
name: "error to list workspace",
in: in{
args: []string{},
wspaceListErr: errors.New("error to list workspace"),
},
want: errors.New("error to list workspace"),
},
{
name: "error empty workspace",
in: in{
args: []string{},
wspaceList: formula.Workspaces{},
},
want: ErrEmptyWorkspaces,
},
{
name: "error to input list",
in: in{
args: []string{},
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
Expand All @@ -86,6 +91,7 @@ func TestDeleteWorkspaces(t *testing.T) {
{
name: "error to accept to delete selected workspace",
in: in{
args: []string{},
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
Expand All @@ -98,6 +104,7 @@ func TestDeleteWorkspaces(t *testing.T) {
{
name: "not accept to delete selected workspace",
in: in{
args: []string{},
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
Expand All @@ -109,6 +116,7 @@ func TestDeleteWorkspaces(t *testing.T) {
{
name: "error to delete repo",
in: in{
args: []string{},
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
Expand All @@ -122,6 +130,7 @@ func TestDeleteWorkspaces(t *testing.T) {
{
name: "error to delete workspace",
in: in{
args: []string{},
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
Expand All @@ -132,6 +141,42 @@ func TestDeleteWorkspaces(t *testing.T) {
},
want: errors.New("error to delete workspace"),
},
{
name: "error on empty flag name",
in: in{
args: []string{"--name="},
},
want: errors.New("please provide a value for 'name'"),
},
{
name: "error on wrong workspace name",
in: in{
args: []string{"--name=Zup"},
wspaceList: formula.Workspaces{
"zup": "/home/user/commons",
},
},
want: errors.New("no workspace found with this name"),
},
{
name: "error to delete default workspace",
in: in{
args: []string{"--name=" + formula.DefaultWorkspaceName},
wspaceList: formula.Workspaces{
formula.DefaultWorkspaceName: "/home/user/commons",
},
},
want: errors.New("cannot delete default workspace"),
},
{
name: "success on workspace name",
in: in{
args: []string{"--name=zup"},
wspaceList: formula.Workspaces{
"zup": "/home/user/commons",
},
},
},
}

for _, tt := range tests {
Expand All @@ -152,10 +197,10 @@ func TestDeleteWorkspaces(t *testing.T) {
os.TempDir(),
workspaceMock,
repoManagerMock,
dirMock,
inListMock,
inBoolMock,
)
cmd.SetArgs(tt.in.args)

got := cmd.Execute()
assert.Equal(t, tt.want, got)
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func Build() *cobra.Command {
autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen)
autocompleteFish := cmd.NewAutocompleteFish(autocompleteGen)
autocompletePowerShell := cmd.NewAutocompletePowerShell(autocompleteGen)
deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, repoDeleter, dirManager, inputList, inputBool)
deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, repoDeleter, inputList, inputBool)
deleteFormulaCmd := cmd.NewDeleteFormulaCmd(userHomeDir, ritchieHomeDir, formulaWorkspace, dirManager, inputBool, inputText, inputList, treeGen, fileManager)
addWorkspaceCmd := cmd.NewAddWorkspaceCmd(formulaWorkspace, inputText)

Expand Down
7 changes: 6 additions & 1 deletion pkg/formula/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package formula

import "github.com/ZupIT/ritchie-cli/pkg/git"
import (
"sort"

"github.com/ZupIT/ritchie-cli/pkg/git"
)

const RepoCommonsName = RepoName("commons")

Expand Down Expand Up @@ -87,6 +91,7 @@ func (re RepoProviders) List() []string {
for provider := range re {
providers = append(providers, provider.String())
}
sort.Strings(providers)

return providers
}
Expand Down

0 comments on commit 88bc0a3

Please sign in to comment.