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

[FEATURE] Create command to update repositories #272

Merged
merged 5 commits into from
Jul 13, 2020
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
14 changes: 12 additions & 2 deletions cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula/repo"
"github.com/ZupIT/ritchie-cli/pkg/formula/runner"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/github"

"k8s.io/kubectl/pkg/util/templates"

Expand Down Expand Up @@ -64,9 +65,16 @@ func buildCommands() *cobra.Command {
fileManager := stream.NewFileManager()
dirManager := stream.NewDirManager(fileManager)

gitRepo := github.NewRepoManager(http.DefaultClient)
treeGen := tree.NewGenerator(dirManager, fileManager)
repoAdder := repo.NewAdder(ritchieHomeDir, http.DefaultClient, treeGen, dirManager, fileManager)

repoCreator := repo.NewCreator(ritchieHomeDir, gitRepo, dirManager, fileManager)
repoLister := repo.NewLister(ritchieHomeDir, fileManager)
repoAdder := repo.NewAdder(ritchieHomeDir, repoCreator, treeGen, dirManager, fileManager)
repoListCreator := repo.NewListCreator(repoLister, repoCreator)
repoUpdater := repo.NewUpdater(ritchieHomeDir, repoListCreator, treeGen, fileManager)
repoAddLister := repo.NewListAdder(repoLister, repoAdder)
repoListUpdater := repo.NewListUpdater(repoLister, repoUpdater)
repoDeleter := repo.NewDeleter(ritchieHomeDir, fileManager, dirManager)
repoPrioritySetter := repo.NewPrioritySetter(ritchieHomeDir, fileManager, dirManager)

Expand Down Expand Up @@ -138,7 +146,8 @@ func buildCommands() *cobra.Command {
deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
addRepoCmd := cmd.NewAddRepoCmd(http.DefaultClient, repoAdder, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, gitRepo, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
updateRepoCmd := cmd.NewUpdateRepoCmd(http.DefaultClient, repoListUpdater, gitRepo, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
listRepoCmd := cmd.NewListRepoCmd(repoLister)
deleteRepoCmd := cmd.NewDeleteRepoCmd(repoLister, inputList, repoDeleter)
setPriorityCmd := cmd.NewSetPriorityCmd(inputList, inputInt, repoLister, repoPrioritySetter)
Expand All @@ -152,6 +161,7 @@ func buildCommands() *cobra.Command {

autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell)
addCmd.AddCommand(addRepoCmd)
updateCmd.AddCommand(updateRepoCmd)
createCmd.AddCommand(createFormulaCmd)
deleteCmd.AddCommand(deleteCtxCmd, deleteRepoCmd)
listCmd.AddCommand(listRepoCmd)
Expand Down
14 changes: 12 additions & 2 deletions cmd/team/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula/repo"
"github.com/ZupIT/ritchie-cli/pkg/formula/runner"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/github"
"github.com/ZupIT/ritchie-cli/pkg/security/otp"

"k8s.io/kubectl/pkg/util/templates"
Expand Down Expand Up @@ -75,9 +76,16 @@ func buildCommands() *cobra.Command {
fileManager := stream.NewFileManager()
dirManager := stream.NewDirManager(fileManager)

gitRepo := github.NewRepoManager(http.DefaultClient)
treeGen := tree.NewGenerator(dirManager, fileManager)
repoAdder := repo.NewAdder(ritchieHomeDir, http.DefaultClient, treeGen, dirManager, fileManager)

repoCreator := repo.NewCreator(ritchieHomeDir, gitRepo, dirManager, fileManager)
repoLister := repo.NewLister(ritchieHomeDir, fileManager)
repoAdder := repo.NewAdder(ritchieHomeDir, repoCreator, treeGen, dirManager, fileManager)
repoListCreator := repo.NewListCreator(repoLister, repoCreator)
repoUpdater := repo.NewUpdater(ritchieHomeDir, repoListCreator, treeGen, fileManager)
repoAddLister := repo.NewListAdder(repoLister, repoAdder)
repoListUpdater := repo.NewListUpdater(repoLister, repoUpdater)
repoPrioritySetter := repo.NewPrioritySetter(ritchieHomeDir, fileManager, dirManager)

sessionManager := session.NewManager(ritchieHomeDir)
Expand Down Expand Up @@ -173,7 +181,8 @@ func buildCommands() *cobra.Command {
deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
addRepoCmd := cmd.NewAddRepoCmd(http.DefaultClient, repoAdder, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, gitRepo, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
updateRepoCmd := cmd.NewUpdateRepoCmd(http.DefaultClient, repoListUpdater, gitRepo, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
setPriorityCmd := cmd.NewSetPriorityCmd(inputList, inputInt, repoLister, repoPrioritySetter)
listRepoCmd := cmd.NewListRepoCmd(repoLister)
autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen)
Expand All @@ -186,6 +195,7 @@ func buildCommands() *cobra.Command {

autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell)
addCmd.AddCommand(addRepoCmd)
updateCmd.AddCommand(updateRepoCmd)
createCmd.AddCommand(createFormulaCmd)
deleteCmd.AddCommand(deleteCtxCmd)
listCmd.AddCommand(listRepoCmd)
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ go 1.14
require (
github.com/AlecAivazis/survey/v2 v2.0.7
github.com/denisbrodbeck/machineid v1.0.1
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/gofrs/flock v0.7.1 // indirect
github.com/google/uuid v1.1.1
github.com/gookit/color v1.2.5
github.com/gosuri/uitable v0.0.4
Expand All @@ -19,6 +17,5 @@ require (
github.com/pkg/errors v0.9.1
github.com/radovskyb/watcher v1.0.7
github.com/spf13/cobra v1.0.0
github.com/thoas/go-funk v0.6.0
k8s.io/kubectl v0.18.4
)
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
{Parent: "root", Usage: "set"},
{Parent: "root_set", Usage: "context"},
{Parent: "root_set", Usage: "credential"},
{Parent: "root_set", Usage: "repo-priority"},
{Parent: "root", Usage: "show"},
{Parent: "root_show", Usage: "context"},
{Parent: "root", Usage: "create"},
Expand Down
120 changes: 40 additions & 80 deletions pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/http/headers"
"github.com/ZupIT/ritchie-cli/pkg/github"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

type AddRepoCmd struct {
client *http.Client
repo formula.RepositoryAdder
repo formula.RepositoryAddLister
github github.Repositories
prompt.InputText
prompt.InputPassword
prompt.InputURL
Expand All @@ -29,8 +26,8 @@ type AddRepoCmd struct {
}

func NewAddRepoCmd(
client *http.Client,
repo formula.RepositoryAdder,
repo formula.RepositoryAddLister,
github github.Repositories,
inText prompt.InputText,
inPass prompt.InputPassword,
inUrl prompt.InputURL,
Expand All @@ -39,8 +36,8 @@ func NewAddRepoCmd(
inInt prompt.InputInt,
) *cobra.Command {
addRepo := AddRepoCmd{
client: client,
repo: repo,
github: github,
InputText: inText,
InputURL: inUrl,
InputList: inList,
Expand All @@ -59,62 +56,78 @@ func NewAddRepoCmd(
return cmd
}

func (a AddRepoCmd) runPrompt() CommandRunnerFunc {
func (ad AddRepoCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
name, err := a.Text("Repository name: ", true)
name, err := ad.Text("Repository name: ", true)
if err != nil {
return err
}

isPrivate, err := a.Bool("Is a private repository? ", []string{"no", "yes"})
repos, err := ad.repo.List()
if err != nil {
return err
}

for i := range repos {
repo := repos[i]
if repo.Name == formula.RepoName(name) {
prompt.Warning(fmt.Sprintf("Your repository %q is gonna be overwritten.", repo.Name))
choice, _ := ad.Bool("Want to proceed?", []string{"yes", "no"})
if !choice {
prompt.Info("Operation cancelled")
return nil
}
}
}

isPrivate, err := ad.Bool("Is a private repository? ", []string{"no", "yes"})
if err != nil {
return err
}

var token string
if isPrivate {
token, err = a.Password("Personal access tokens: ")
token, err = ad.Password("Personal access tokens: ")
if err != nil {
return err
}
}

url, err := a.URL("Repository URL: ", "https://github.com/kaduartur/ritchie-formulas")
url, err := ad.URL("Repository URL: ", "https://github.com/kaduartur/ritchie-formulas")
if err != nil {
return err
}

tags, err := a.tags(url, token)
gitRepoInfo := github.NewRepoInfo(url, token)
tags, err := ad.github.Tags(gitRepoInfo)
if err != nil {
return err
}

var tagNames []string
for k := range tags {
tagNames = append(tagNames, k)
for i := range tags {
tagNames = append(tagNames, tags[i].Name)
}

version, err := a.List("Select a tag version: ", tagNames)
version, err := ad.List("Select a tag version:", tagNames)
if err != nil {
return err
}

zipUrl := tags[version]

priority, err := a.Int("Set the priority [ps.: 0 is higher priority, the lower higher the priority] :")
priority, err := ad.Int("Set the priority:", "0 is higher priority, the lower higher the priority")
if err != nil {
return err
}

repository := formula.Repo{
Name: name,
Name: formula.RepoName(name),
Version: formula.RepoVersion(version),
Token: token,
ZipUrl: zipUrl,
Version: version,
Url: url,
Priority: int(priority),
}

if err := a.repo.Add(repository); err != nil {
if err := ad.repo.Add(repository); err != nil {
return err
}

Expand All @@ -124,7 +137,7 @@ func (a AddRepoCmd) runPrompt() CommandRunnerFunc {
}
}

func (a AddRepoCmd) runStdin() CommandRunnerFunc {
func (ad AddRepoCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {

r := formula.Repo{}
Expand All @@ -135,7 +148,7 @@ func (a AddRepoCmd) runStdin() CommandRunnerFunc {
return err
}

if err := a.repo.Add(r); err != nil {
if err := ad.repo.Add(r); err != nil {
return err
}

Expand All @@ -144,56 +157,3 @@ func (a AddRepoCmd) runStdin() CommandRunnerFunc {
return nil
}
}

func (a AddRepoCmd) tags(url string, token string) (formula.Tags, error) {
apiUrl, err := tagsUrl(url)
if err != nil {
return formula.Tags{}, err
}

req, err := http.NewRequest(http.MethodGet, apiUrl, nil)
if err != nil {
return formula.Tags{}, err
}

if token != "" {
authToken := fmt.Sprintf("token %s", token)
req.Header.Add(headers.Authorization, authToken)
}

req.Header.Add(headers.Accept, "application/vnd.github.v3+json")
resp, err := a.client.Do(req)
if err != nil {
return formula.Tags{}, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return formula.Tags{}, err
}
return formula.Tags{}, errors.New(string(b))
}

var tags []formula.Tag
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
return formula.Tags{}, err
}

tagsUrl := make(formula.Tags)
for _, tag := range tags {
tagsUrl[tag.Name] = tag.ZipUrl
}

return tagsUrl, nil
}

func tagsUrl(url string) (string, error) {
split := strings.Split(url, "/")
repo := split[len(split)-1]
owner := split[len(split)-2]

return fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo), nil
}
4 changes: 2 additions & 2 deletions pkg/cmd/delete_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func (dr DeleteRepoCmd) runFunc() CommandRunnerFunc {

var reposNames []string
for _, r := range repos {
reposNames = append(reposNames, r.Name)
reposNames = append(reposNames, r.Name.String())
}

repo, err := dr.InputList.List("Repository:", reposNames)
if err != nil {
return err
}

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

Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/set_priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ func (s SetPriorityCmd) runFunc() CommandRunnerFunc {

if len(repositories) == 0 {
prompt.Warning("You should add a repository first")
prompt.Info("Command: rit add repo")
return nil
}

var reposNames []string
for _, r := range repositories {
reposNames = append(reposNames, r.Name)
reposNames = append(reposNames, r.Name.String())
}

repoName, err := s.InputList.List("Repository:", reposNames)
Expand All @@ -60,7 +61,7 @@ func (s SetPriorityCmd) runFunc() CommandRunnerFunc {

var repo formula.Repo
for _, r := range repositories {
if r.Name == repoName {
if r.Name == formula.RepoName(repoName) {
repo = r
break
}
Expand Down
Loading