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

[FEATURE] Added rit delete repo command and added to list repo a total info a… #268

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions pkg/cmd/delete_repo.go
Original file line number Diff line number Diff line change
@@ -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)
marcoscostazup marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

if err = dr.Delete(repo); err != nil {
return err
}

prompt.Success(fmt.Sprintf(deleteSuccessMsg, repo))
return nil
}
}
16 changes: 14 additions & 2 deletions pkg/cmd/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")...)
Expand Down
75 changes: 75 additions & 0 deletions pkg/formula/repo/deleter.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/formula/repo/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down