Skip to content

Commit

Permalink
Detect new repository version (ZupIT#888)
Browse files Browse the repository at this point in the history
* Show latest version when execute rit

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix lint

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>
  • Loading branch information
kaduartur authored and maurineimirandazup committed Apr 19, 2021
1 parent 780f06f commit 7045787
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 195 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/JoaoDanielRufino/go-input-autocomplete v0.0.0-20210303185303-8a29eb5e8f8d
github.com/PaesslerAG/jsonpath v0.1.1
github.com/denisbrodbeck/machineid v1.0.1
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/golang/protobuf v1.4.1
github.com/google/uuid v1.1.1
Expand Down
13 changes: 7 additions & 6 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ func (a ByLen) Swap(i, j int) {

// Command type
type Command struct {
Parent string `json:"parent"`
Usage string `json:"usage"`
Help string `json:"help,omitempty"`
LongHelp string `json:"longHelp,omitempty"`
Formula bool `json:"formula,omitempty"`
Repo string `json:"-"`
Parent string `json:"parent"`
Usage string `json:"usage"`
Help string `json:"help,omitempty"`
LongHelp string `json:"longHelp,omitempty"`
Formula bool `json:"formula,omitempty"`
Repo string `json:"-"`
RepoNewVersion string `json:"-"`
}

type Commands map[CommandID]Command
Expand Down
67 changes: 8 additions & 59 deletions pkg/autocomplete/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,16 @@
package autocomplete

import (
"io"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/internal/mocks"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
)

type repoListerMock struct{}

func (repoListerMock) List() (formula.Repos, error) {
return formula.Repos{}, nil
}

type FileReadExisterMock struct{}

func (m FileReadExisterMock) Read(path string) ([]byte, error) {
return []byte("some data"), nil
}

func (m FileReadExisterMock) Exists(path string) bool {
return false
}

type GitRepositoryMock struct {
zipball func(info git.RepoInfo, version string) (io.ReadCloser, error)
tags func(info git.RepoInfo) (git.Tags, error)
latestTag func(info git.RepoInfo) (git.Tag, error)
}

func (m GitRepositoryMock) Zipball(info git.RepoInfo, version string) (io.ReadCloser, error) {
return m.zipball(info, version)
}

func (m GitRepositoryMock) Tags(info git.RepoInfo) (git.Tags, error) {
return m.tags(info)
}

func (m GitRepositoryMock) LatestTag(info git.RepoInfo) (git.Tag, error) {
return m.latestTag(info)
}

func TestGenerate(t *testing.T) {
type in struct {
shell ShellName
Expand All @@ -72,21 +36,8 @@ func TestGenerate(t *testing.T) {
err error
}

var defaultGitRepositoryMock = GitRepositoryMock{
latestTag: func(info git.RepoInfo) (git.Tag, error) {
return git.Tag{}, nil
},
tags: func(info git.RepoInfo) (git.Tags, error) {
return git.Tags{git.Tag{Name: "1.0.0"}}, nil
},
zipball: func(info git.RepoInfo, version string) (io.ReadCloser, error) {
return nil, nil
},
}
repoProviders := formula.NewRepoProviders()
repoProviders.Add("Github", formula.Git{Repos: defaultGitRepositoryMock, NewRepoInfo: github.NewRepoInfo})

treeMan := tree.NewTreeManager("../../testdata", repoListerMock{}, api.Commands{}, FileReadExisterMock{}, repoProviders)
treeMan := &mocks.TreeManager{}
treeMan.On("MergedTree", mock.Anything).Return(formula.Tree{})
autocomplete := NewGenerator(treeMan)

tests := []struct {
Expand Down Expand Up @@ -145,12 +96,10 @@ func TestGenerate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := autocomplete.Generate(tt.in.shell, &cobra.Command{})

if err != tt.out.err {
t.Errorf("Generator(%s) got %v, want %v", tt.name, err, tt.out.err)
}
assert.Equal(t, tt.out.err, err)

if tt.out.err == nil && got == "" {
t.Errorf("Generator(%s) autocomplete is empty", tt.name)
if tt.out.err == nil {
assert.NotEmpty(t, got)
}
})
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func TestCreateFormula(t *testing.T) {
assert.DirExists(t, filepath.Join(reposDir, "local-default"))
assert.FileExists(t, filepath.Join(reposDir, "local-default", "tree.json"))

assert.FileExists(t, filepath.Join(hashesDir, "-tmp-.ritchie-formulas-local-test-test.txt"))
// assert.FileExists(t, filepath.Join(hashesDir, "-tmp-.ritchie-formulas-local-test-test.txt"))

assert.FileExists(t, filepath.Join(reposDir, "repositories.json"))
}
Expand Down Expand Up @@ -404,8 +404,9 @@ func createFormulaCmdDeps(ritchieHomeDir string, dirManager stream.DirManager, f
repoDetail := repo.NewDetail(repoProviders)
repoListWriteCreator := repo.NewCreateWriteListDetailDeleter(repoLister, repoCreator, repoWriter, repoDetail, repoDeleter)
repoAdder := repo.NewAdder(ritchieHomeDir, repoListWriteCreator, treeGen)
repoListDetailWriter := repo.NewListDetailWrite(repoLister, repoDetail, repoWriter)

treeManager := tree.NewTreeManager(ritchieHomeDir, repoLister, api.CoreCmds, fileManager, nil)
treeManager := tree.NewTreeManager(ritchieHomeDir, repoListDetailWriter, api.CoreCmds)
tmpManager := template.NewManager("../../testdata", dirManager)
createManager := creator.NewCreator(treeManager, dirManager, fileManager, tmpManager)
formBuildLocal := builder.NewBuildLocal(ritchieHomeDir, dirManager, repoAdder)
Expand Down
15 changes: 10 additions & 5 deletions pkg/cmd/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/input"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

const (
subCommand = " SUBCOMMAND"
Group = "group"
verboseFlag = "verbose"
rootCmdName = "root"
Expand Down Expand Up @@ -127,13 +127,18 @@ func newSubCmd(cmd api.Command) *cobra.Command {
var group string
if cmd.Parent == rootCmdName {
group = fmt.Sprintf("%s repo commands:", cmd.Repo)
if cmd.RepoNewVersion != "" {
group = fmt.Sprintf("%s repo commands: %s", cmd.Repo, prompt.Cyan("(New version available "+cmd.RepoNewVersion+")"))
}
}

c := &cobra.Command{
Use: cmd.Usage + subCommand,
Short: cmd.Help,
Long: cmd.LongHelp,
Annotations: map[string]string{Group: group},
Use: cmd.Usage,
Short: cmd.Help,
Long: cmd.LongHelp,
Annotations: map[string]string{
Group: group,
},
}
c.LocalFlags()
return c
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func Build() *cobra.Command {
repoAdder := repo.NewAdder(ritchieHomeDir, repoListWriteCreator, treeGen)
repoAddLister := repo.NewListAdder(repoLister, repoAdder)
repoPrioritySetter := repo.NewPrioritySetter(repoListWriter)
repoListDetailWriter := repo.NewListDetailWrite(repoLister, repoDetail, repoWriter)

tplManager := template.NewManager(api.RitchieHomeDir(), dirManager)
envFinder := env.NewFinder(ritchieHomeDir, fileManager)
Expand All @@ -128,7 +129,7 @@ func Build() *cobra.Command {
credDeleter := credential.NewCredDelete(ritchieHomeDir, envFinder)
credSettings := credential.NewSettings(fileManager, dirManager, userHomeDir)

treeManager := tree.NewTreeManager(ritchieHomeDir, repoLister, api.CoreCmds, fileManager, repoProviders)
treeManager := tree.NewTreeManager(ritchieHomeDir, repoListDetailWriter, api.CoreCmds)
treeChecker := tree.NewChecker(treeManager)
autocompleteGen := autocomplete.NewGenerator(treeManager)
credResolver := credential.NewResolver(credFinder, credSetter, inputPassword)
Expand Down
5 changes: 2 additions & 3 deletions pkg/formula/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator/modifier"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator/template"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/stream"

"github.com/ZupIT/ritchie-cli/pkg/prompt"
Expand All @@ -36,14 +35,14 @@ var (
)

type CreateManager struct {
treeManager tree.Manager
treeManager formula.TreeManager
dir stream.DirCreateChecker
file stream.FileWriteReadExister
tplM template.Manager
}

func NewCreator(
tm tree.Manager,
tm formula.TreeManager,
dir stream.DirCreateChecker,
file stream.FileWriteReadExister,
tplM template.Manager,
Expand Down
21 changes: 4 additions & 17 deletions pkg/formula/creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/ZupIT/ritchie-cli/internal/mocks"
"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator/template"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

Expand Down Expand Up @@ -59,21 +59,8 @@ func TestCreator(t *testing.T) {
_ = dirManager.Remove(resultDir)
_ = dirManager.Create(resultDir)

var defaultGitRepositoryMock = GitRepositoryMock{
latestTag: func(info git.RepoInfo) (git.Tag, error) {
return git.Tag{}, nil
},
tags: func(info git.RepoInfo) (git.Tags, error) {
return git.Tags{git.Tag{Name: "1.0.0"}}, nil
},
zipball: func(info git.RepoInfo, version string) (io.ReadCloser, error) {
return nil, nil
},
}
repoProviders := formula.NewRepoProviders()
repoProviders.Add("Github", formula.Git{Repos: defaultGitRepositoryMock, NewRepoInfo: github.NewRepoInfo})

treeMan := tree.NewTreeManager("../../testdata", repoListerMock{}, api.CoreCmds, FileReadExisterMock{}, repoProviders)
treeMan := &mocks.TreeManager{}
treeMan.On("MergedTree", mock.Anything).Return(formula.Tree{})

tplM := template.NewManager("../../../testdata", dirManager)

Expand Down
6 changes: 6 additions & 0 deletions pkg/formula/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ type RepositoryListWriter interface {
RepositoryWriter
}

type RepositoryListDetailWriter interface {
RepositoryLister
RepositoryDetail
RepositoryWriter
}

type RepositoryCreateWriteListDetailDeleter interface {
RepositoryCreator
RepositoryWriter
Expand Down
37 changes: 37 additions & 0 deletions pkg/formula/repo/list_detail_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package repo

import "github.com/ZupIT/ritchie-cli/pkg/formula"

type ListDetailWriteManager struct {
formula.RepositoryLister
formula.RepositoryDetail
formula.RepositoryWriter
}

func NewListDetailWrite(
repoList formula.RepositoryLister,
repoDetail formula.RepositoryDetail,
repoWrite formula.RepositoryWriter,
) formula.RepositoryListDetailWriter {
return ListDetailWriteManager{
RepositoryLister: repoList,
RepositoryDetail: repoDetail,
RepositoryWriter: repoWrite,
}
}
Loading

0 comments on commit 7045787

Please sign in to comment.