diff --git a/cmd/single/main.go b/cmd/single/main.go index 72a1aa6e0..d9fd7596e 100644 --- a/cmd/single/main.go +++ b/cmd/single/main.go @@ -67,6 +67,7 @@ func buildCommands() *cobra.Command { treeGen := tree.NewGenerator(dirManager, fileManager) repoAdder := repo.NewAdder(ritchieHomeDir, http.DefaultClient, treeGen, dirManager, fileManager) repoLister := repo.NewLister(ritchieHomeDir, fileManager) + repoDeleter := repo.NewDeleter(ritchieHomeDir, fileManager, dirManager) repoPrioritySetter := repo.NewPrioritySetter(ritchieHomeDir, fileManager, dirManager) sessionManager := session.NewManager(ritchieHomeDir) @@ -139,6 +140,7 @@ func buildCommands() *cobra.Command { showCtxCmd := cmd.NewShowContextCmd(ctxFinder) addRepoCmd := cmd.NewAddRepoCmd(http.DefaultClient, repoAdder, inputText, inputPassword, inputURL, inputList, inputBool, inputInt) listRepoCmd := cmd.NewListRepoCmd(repoLister) + deleteRepoCmd := cmd.NewDeleteRepoCmd(repoLister, inputList, repoDeleter) setPriorityCmd := cmd.NewSetPriorityCmd(inputList, inputInt, repoLister, repoPrioritySetter) autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen) autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen) @@ -151,7 +153,7 @@ func buildCommands() *cobra.Command { autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell) addCmd.AddCommand(addRepoCmd) createCmd.AddCommand(createFormulaCmd) - deleteCmd.AddCommand(deleteCtxCmd) + deleteCmd.AddCommand(deleteCtxCmd, deleteRepoCmd) listCmd.AddCommand(listRepoCmd) setCmd.AddCommand(setCredentialCmd, setCtxCmd, setPriorityCmd) showCmd.AddCommand(showCtxCmd) diff --git a/pkg/cmd/delete_repo.go b/pkg/cmd/delete_repo.go new file mode 100644 index 000000000..7b2137933 --- /dev/null +++ b/pkg/cmd/delete_repo.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/prompt" +) + +const ( + deleteSuccessMsg = "Repository %q was deleted with success" +) + +type DeleteRepoCmd struct { + formula.RepositoryLister + prompt.InputList + formula.RepositoryDeleter +} + +func NewDeleteRepoCmd(rl formula.RepositoryLister, il prompt.InputList, rd formula.RepositoryDeleter) *cobra.Command { + dr := DeleteRepoCmd{rl, il, rd} + cmd := &cobra.Command{ + Use: "repo", + Short: "Delete a repository", + Example: "rit delete repo", + RunE: dr.runFunc(), + } + return cmd +} + +func (dr DeleteRepoCmd) runFunc() CommandRunnerFunc { + return func(cmd *cobra.Command, args []string) error { + repos, err := dr.RepositoryLister.List() + if err != nil { + return err + } + + if len(repos) == 0 { + prompt.Warning("You don't have any repositories") + return nil + } + + var reposNames []string + for _, r := range repos { + reposNames = append(reposNames, r.Name) + } + + repo, err := dr.InputList.List("Repository:", reposNames) + if err != nil { + return err + } + + if err = dr.Delete(repo); err != nil { + return err + } + + prompt.Success(fmt.Sprintf(deleteSuccessMsg, repo)) + return nil + } +} diff --git a/pkg/cmd/list_repo.go b/pkg/cmd/list_repo.go index 8c7489c78..3c0fd7d7d 100644 --- a/pkg/cmd/list_repo.go +++ b/pkg/cmd/list_repo.go @@ -7,6 +7,12 @@ import ( "github.com/spf13/cobra" "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/prompt" +) + +const ( + totalReposMsg = "There are %v repos" + totalOneRepoMsg = "There is 1 repo" ) type ListRepoCmd struct { @@ -33,15 +39,21 @@ func (lr ListRepoCmd) runFunc() CommandRunnerFunc { printRepos(repos) + if len(repos) != 1 { + prompt.Info(fmt.Sprintf(totalReposMsg, len(repos))) + } else { + prompt.Info(totalOneRepoMsg) + } + return nil } } func printRepos(repos formula.Repos) { table := uitable.New() - table.AddRow("PRIORITY", "NAME", "VERSION") + table.AddRow("NAME", "VERSION", "PRIORITY") for _, repo := range repos { - table.AddRow(repo.Priority, repo.Name, repo.Version) + table.AddRow(repo.Name, repo.Version, repo.Priority) } raw := table.Bytes() raw = append(raw, []byte("\n")...) diff --git a/pkg/formula/repo/deleter.go b/pkg/formula/repo/deleter.go new file mode 100644 index 000000000..c570214c9 --- /dev/null +++ b/pkg/formula/repo/deleter.go @@ -0,0 +1,75 @@ +package repo + +import ( + "encoding/json" + "path" + + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/stream" +) + +type DeleteManager struct { + ritHome string + file stream.FileWriteReadExister + dir stream.DirRemover +} + +func NewDeleter(ritHome string, file stream.FileWriteReadExister, dir stream.DirRemover) DeleteManager { + return DeleteManager{ + ritHome: ritHome, + file: file, + dir: dir, + } +} + +func (dm DeleteManager) Delete(repoName string) error { + if err := dm.deleteRepoDir(repoName); err != nil { + return err + } + if err := dm.deleteFromReposFile(repoName); err != nil { + return err + } + return nil +} + +func (dm DeleteManager) deleteRepoDir(repoName string) error { + path := path.Join(dm.ritHome, reposDirName, repoName) + if err := dm.dir.Remove(path); err != nil { + return err + } + return nil +} + +func (dm DeleteManager) deleteFromReposFile(repoName string) error { + repos := formula.Repos{} + + path := path.Join(dm.ritHome, reposDirName, reposFileName) + file, err := dm.file.Read(path) + if err != nil { + return err + } + + if err = json.Unmarshal(file, &repos); err != nil { + return err + } + + var idx int + for i := range repos { + if repos[i].Name == repoName { + idx = i + break + } + } + repos = append(repos[:idx], repos[idx+1:]...) + + newFile, err := json.MarshalIndent(repos, "", "\t") + if err != nil { + return err + } + + if err = dm.file.Write(path, newFile); err != nil { + return err + } + + return nil +} diff --git a/pkg/formula/repo/lister.go b/pkg/formula/repo/lister.go index de1b05108..5b05b4ff4 100644 --- a/pkg/formula/repo/lister.go +++ b/pkg/formula/repo/lister.go @@ -18,6 +18,7 @@ func NewLister(ritHome string, file stream.FileReadExister) ListManager { return ListManager{ritHome: ritHome, file: file} } +// List method returns an empty []formula.Repo if there is no repositories.json func (li ListManager) List() (formula.Repos, error) { repos := formula.Repos{} reposFilePath := path.Join(li.ritHome, reposDirName, reposFileName)