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

Support flags for the rit update repo command. #916

Merged
merged 29 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9d7b1b2
Support flags for the git update repo command.
maurineimirandazup May 3, 2021
5306c87
Support flags for the rit update repo command.
maurineimirandazup May 3, 2021
5031ca6
Merge branch 'mm/update-repo-flags' of https://github.com/maurineimir…
maurineimirandazup May 3, 2021
99afaff
Internationalization Ritchie-cli (#847)
kaduartur Apr 29, 2021
a83797d
Adding flags, delete confirmation and new tests to delete repo (#908)
fernandobelettizup May 3, 2021
0199fb7
Feature/add formula rit list formulas (#913)
fernandobelettizup May 4, 2021
9210f06
Removal of the Run FuncEF method that I had created. Standardize meth…
maurineimirandazup May 4, 2021
33df5e4
Remove comment
maurineimirandazup May 4, 2021
7584106
Adjust in return to perform only an update repo on runCmd.
maurineimirandazup May 6, 2021
9bcc594
Merge remote-tracking branch 'upstream/master' into mm/update-repo-flags
maurineimirandazup May 6, 2021
4897528
Variable renamed with a more complete name.
maurineimirandazup May 6, 2021
bdca280
Linter Fix, gofmt.
maurineimirandazup May 6, 2021
8ed3241
Fix version update prompt, fix array lenght in update flags
maurineimirandazup May 8, 2021
f2dacbb
Change error msg
maurineimirandazup May 10, 2021
d075433
Change return 'err' to nil
maurineimirandazup May 10, 2021
b88f7f2
add 'externalRepos', with only remote repositorys
maurineimirandazup May 11, 2021
b54f171
change err to nil
maurineimirandazup May 11, 2021
e7f000a
change err to nil
maurineimirandazup May 11, 2021
610ea21
Merge branch 'mm/update-repo-flags' of https://github.com/maurineimir…
maurineimirandazup May 11, 2021
7e2fc8d
First tests
maurineimirandazup May 12, 2021
2862841
gofmt file
maurineimirandazup May 13, 2021
a921ef7
small changes and working tests
maurineimirandazup May 14, 2021
d37607d
All Tests Working, Need Refactor
maurineimirandazup May 18, 2021
b4bc96f
Working Tests with assert
maurineimirandazup May 19, 2021
8757e47
Last test, invalid repo
maurineimirandazup May 19, 2021
bee0d1a
Change output error to fmt.Errorf
maurineimirandazup May 19, 2021
4c002ff
remove variable flagAll
maurineimirandazup May 20, 2021
0545a5f
declaration of success message in just one place
maurineimirandazup May 20, 2021
3eea57a
addition of the help message
maurineimirandazup May 21, 2021
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
216 changes: 162 additions & 54 deletions pkg/cmd/update_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package cmd

import (
"errors"
"fmt"
"net/http"
"reflect"

"github.com/spf13/cobra"

Expand All @@ -30,8 +32,26 @@ import (
const (
questionSelectARepo = "Select a repository to update: "
updateOptionAll = "ALL"
repoName = "name"
repoVersion = "version"
successUpdate = "The %q repository was updated with success to version %q\n"
)

var updateRepoFlags = flags{
{
name: repoName,
kind: reflect.String,
defValue: "",
description: "repository name",
},
{
name: repoVersion,
kind: reflect.String,
defValue: "latest",
description: "repository version",
},
}

type updateRepoCmd struct {
client *http.Client
repo formula.RepositoryListUpdater
Expand Down Expand Up @@ -71,99 +91,187 @@ func NewUpdateRepoCmd(
Use: "repo",
Short: "Update a repository.",
Example: "rit update repo",
RunE: RunFuncE(updateRepo.runStdin(), updateRepo.runPrompt()),
RunE: RunFuncE(updateRepo.runStdin(), updateRepo.runCmd()),
ValidArgs: []string{""},
Args: cobra.OnlyValidArgs,
}
cmd.LocalFlags()
addReservedFlags(cmd.Flags(), updateRepoFlags)

return cmd
}

func (up updateRepoCmd) runPrompt() CommandRunnerFunc {
func (up updateRepoCmd) runCmd() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
repos, err := up.repo.List()
reposToUpdate, err := up.resolveInput(cmd)
if err != nil {
return err
}
maurineimirandazup marked this conversation as resolved.
Show resolved Hide resolved

var reposName []string
var externalRepos formula.Repos
reposName = append(reposName, updateOptionAll)
for i := range repos {
if !repos[i].IsLocal {
externalRepos = append(externalRepos, repos[i])
reposName = append(reposName, repos[i].Name.String())
for _, value := range reposToUpdate {
err := up.repo.Update(value.Name, value.Version)
if err != nil {
return err
}
s := fmt.Sprintf(successUpdate, value.Name, value.Version)
prompt.Success(s)
}
return nil
}
}

helper := "Select a repository to update your version. P.S. Local repositories cannot be updated."
name, err := up.List(questionSelectARepo, reposName, helper)
if err != nil {
func (up updateRepoCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
r := formula.Repo{}

if err := stdin.ReadJson(cmd.InOrStdin(), &r); err != nil {
return err
}

flagAll := name == updateOptionAll

var repoToUpdate []formula.Repo

if flagAll {
repoToUpdate = externalRepos
} else {
for i := range externalRepos {
if externalRepos[i].Name == formula.RepoName(name) {
repoToUpdate = append(repoToUpdate, externalRepos[i])
break
}
}
if err := up.repo.Update(r.Name, r.Version); err != nil {
return err
}

for _, currRepo := range repoToUpdate {
s := fmt.Sprintf(successUpdate, r.Name, r.Version)
prompt.Success(s)

git := up.repoProviders.Resolve(currRepo.Provider)
return nil
}
}

repoInfo := git.NewRepoInfo(currRepo.Url, currRepo.Token)
tags, err := git.Repos.Tags(repoInfo)
if err != nil {
return err
}
func (up updateRepoCmd) resolveInput(cmd *cobra.Command) (formula.Repos, error) {
if IsFlagInput(cmd) {
return up.resolveFlags(cmd)
}
return up.resolvePrompt()
}

currRepoVersion := fmt.Sprintf("Select your new version for %q:", currRepo.Name.String())
func (up updateRepoCmd) resolvePrompt() (formula.Repos, error) {
repos, err := up.repo.List()
if err != nil {
return formula.Repos{}, err
}

version, err := up.List(currRepoVersion, tags.Names())
if err != nil {
return err
}
var reposName []string
var externalRepos formula.Repos
reposName = append(reposName, updateOptionAll)
for i := range repos {
if !repos[i].IsLocal {
externalRepos = append(externalRepos, repos[i])
reposName = append(reposName, repos[i].Name.String())
}
}

helper := "Select a repository to update your version. PS: To update local repositories (workspaces), use the *rit update workspace* command."
name, err := up.List(questionSelectARepo, reposName, helper)
if err != nil {
return formula.Repos{}, err
}

currRepoName := string(currRepo.Name)
var repoToUpdate []formula.Repo

if err := up.repo.Update(formula.RepoName(currRepoName), formula.RepoVersion(version)); err != nil {
return err
if name == updateOptionAll {
repoToUpdate = externalRepos
} else {
for i := range externalRepos {
if externalRepos[i].Name == formula.RepoName(name) {
repoToUpdate = append(repoToUpdate, externalRepos[i])
break
}
}
}

successMsg := fmt.Sprintf("The %q repository was updated with success to version %q\n", currRepo.Name, version)
prompt.Success(successMsg)
for i, currRepo := range repoToUpdate {
repoInfo, err := up.getRepoInfo(currRepo)
if err != nil {
return formula.Repos{}, err
}

return nil
currRepoVersion := fmt.Sprintf("Select your new version for %q:", currRepo.Name.String())

version, err := up.List(currRepoVersion, repoInfo)
if err != nil {
return formula.Repos{}, err
}
repoToUpdate[i].Version = formula.RepoVersion(version)
}
return repoToUpdate, nil
}

func (up updateRepoCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
r := formula.Repo{}
func (up *updateRepoCmd) resolveFlags(cmd *cobra.Command) (formula.Repos, error) {
name, err := cmd.Flags().GetString(repoName)
if err != nil {
return nil, err
}

if err := stdin.ReadJson(cmd.InOrStdin(), &r); err != nil {
return err
if name == "" {
return nil, errors.New(missingFlagText(repoName))
}

version, err := cmd.Flags().GetString(repoVersion)
if err != nil {
return nil, err
}
if version == "" {
return formula.Repos{}, errors.New(missingFlagText(repoVersion))
}

repoTarget := formula.Repo{Name: formula.RepoName(name), Version: formula.RepoVersion(version)}
var repoToUpdate []formula.Repo

repos, err := up.repo.List()
if err != nil {
return nil, err
}

var externalRepos formula.Repos
for i := range repos {
if !repos[i].IsLocal {
externalRepos = append(externalRepos, repos[i])
}
}

if err := up.repo.Update(r.Name, r.Version); err != nil {
return err
for _, currRepo := range externalRepos {
if repoTarget.Name == currRepo.Name {
info, _ := up.getRepoInfo(currRepo)
if version == "latest" {
repoTarget.Version = currRepo.LatestVersion
repoToUpdate = append(repoToUpdate, repoTarget)
return repoToUpdate, nil
} else if findVersion(info, repoTarget.Version) {
repoToUpdate = append(repoToUpdate, repoTarget)
return repoToUpdate, nil
} else {
errorMsg := fmt.Sprintf("The version %q of repository %q was not found.\n", repoTarget.Version, repoTarget.Name)
return repoToUpdate, errors.New(errorMsg)
}
}
}

successMsg := fmt.Sprintf("The %q repository was updated with success to version %q", r.Name, r.Version)
prompt.Success(successMsg)
if len(repoToUpdate) == 0 {
errorMsg := fmt.Sprintf("The repository %q was not found.\n", repoTarget.Name)
return formula.Repos{}, errors.New(errorMsg)
}

return nil
return repoToUpdate, nil
}

func (up *updateRepoCmd) getRepoInfo(repoToUpdate formula.Repo) ([]string, error) {
gitResp := up.repoProviders.Resolve(repoToUpdate.Provider)
repoInfo := gitResp.NewRepoInfo(repoToUpdate.Url, repoToUpdate.Token)
tags, err := gitResp.Repos.Tags(repoInfo)
if err != nil {
return nil, err
}
stringTags := tags.Names()
return stringTags, nil
}

func findVersion(source []string, value formula.RepoVersion) bool {
for _, item := range source {
if item == string(value) {
return true
}
}
return false
}
Loading