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

Adds question about metrics in init #420

Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func buildCommands() *cobra.Command {
addCmd := cmd.NewAddCmd()
createCmd := cmd.NewCreateCmd()
deleteCmd := cmd.NewDeleteCmd()
initCmd := cmd.NewInitCmd(repoAdder, githubRepo, tutorialFinder, inputBool)
initCmd := cmd.NewInitCmd(repoAdder, githubRepo, tutorialFinder, inputList, fileManager)
listCmd := cmd.NewListCmd()
setCmd := cmd.NewSetCmd()
showCmd := cmd.NewShowCmd()
Expand Down
5 changes: 5 additions & 0 deletions functional/init/init_feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
"key":"Would you like to add the community repository? [https://github.com/ZupIT/ritchie-formulas]",
"value":"yes",
"action":"select"
},
{
"key":"To help us improve and deliver more value to the community, do you agree to let us collect anonymous data about product and feature use statistics and crash reports?",
"value":"Yes, I agree to contribute with data anonymously",
"action":"select"
}
],
"result":""
Expand Down
13 changes: 10 additions & 3 deletions functional/runner_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,16 @@ func setUpRitSingleUnix() {

fmt.Println("Running INIT")
initStepRit := Step{Key: "", Value: "init", Action: "rit"}
initAddRepo := Step{Key: "Would you like to add the community repository? [https://github.com/ZupIT/ritchie-formulas]", Value: "yes", Action: "select"}

init := Scenario{Entry: "Running Init", Result: "", Steps: []Step{initStepRit, initAddRepo}}
initAddRepo := Step{
Key: "Would you like to add the community repository? [https://github.com/ZupIT/ritchie-formulas]",
Value: "yes",
Action: "select"}
initAcceptsMetrics := Step{
Key: "To help us improve and deliver more value to the community, do you agree to let us collect anonymous data about product and feature use statistics and crash reports?",
Value: "Yes, I agree to contribute with data anonymously",
Action: "select"}

init := Scenario{Entry: "Running Init", Result: "", Steps: []Step{initStepRit, initAddRepo, initAcceptsMetrics}}

err, _ := init.runStepsForUnix()
if err != nil {
Expand Down
105 changes: 70 additions & 35 deletions pkg/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/metric"
"github.com/ZupIT/ritchie-cli/pkg/stream"

"github.com/kaduartur/go-cli-spinner/pkg/spinner"

Expand All @@ -34,15 +36,19 @@ import (
)

const (
addRepoMsg = "Run \"rit add repo\" to add a new repository manually."
addRepoMsg = "Run \"rit add repo\" to add a new repository manually."
AddCommonsQuestion = "Would you like to add the community repository? [https://github.com/ZupIT/ritchie-formulas]"
AddMetricsQuestion = "To help us improve and deliver more value to the community, do you agree to let us collect anonymous data about product and feature use statistics and crash reports?"
AcceptMetrics = "Yes, I agree to contribute with data anonymously"
DoNotAcceptMetrics = "No, not for now."
)

var (
addRepoInfo = `You can keep the configuration without adding the community repository,
but you will need to provide a git repo with the formulas templates and add them with
rit add repo command, naming this repository obligatorily as "commons".

See how to do this on the example: [https://github.com/ZupIT/ritchie-formulas/blob/master/templates/create_formula/README.md]`
but you will need to provide a git repo with the formulas templates and add them with
rit add repo command, naming this repository obligatorily as "commons".
See how to do this on the example: [https://github.com/ZupIT/ritchie-formulas/blob/master/templates/create_formula/README.md]`
errMsg = prompt.Yellow("It was not possible to add the commons repository at this time, please try again later.")
ErrInitCommonsRepo = errors.New(errMsg)
CommonsRepoURL = "https://github.com/ZupIT/ritchie-formulas"
Expand All @@ -52,11 +58,12 @@ type initCmd struct {
repo formula.RepositoryAdder
git git.Repositories
rt rtutorial.Finder
prompt.InputBool
prompt.InputList
stream.FileWriteReadExister
}

func NewInitCmd(repo formula.RepositoryAdder, git git.Repositories, rtf rtutorial.Finder, inBool prompt.InputBool) *cobra.Command {
o := initCmd{repo: repo, git: git, rt: rtf, InputBool: inBool}
func NewInitCmd(repo formula.RepositoryAdder, git git.Repositories, rtf rtutorial.Finder, inList prompt.InputList, file stream.FileWriteReadExister) *cobra.Command {
o := initCmd{repo: repo, git: git, rt: rtf, InputList: inList, FileWriteReadExister: file}

cmd := &cobra.Command{
Use: "init",
Expand All @@ -70,48 +77,49 @@ func NewInitCmd(repo formula.RepositoryAdder, git git.Repositories, rtf rtutoria

func (in initCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
label := "Would you like to add the community repository? [https://github.com/ZupIT/ritchie-formulas]"
choose, err := in.Bool(label, []string{"yes", "no"})
choose, err := in.List(AddCommonsQuestion, []string{"yes", "no"})
if err != nil {
return err
}

if !choose {
if choose != "yes" {
fmt.Println()
prompt.Warning(addRepoInfo)
fmt.Println()
fmt.Println(addRepoMsg)
return nil
}
} else {
repo := formula.Repo{
Provider: "Github",
Name: "commons",
Url: CommonsRepoURL,
Priority: 0,
}

repo := formula.Repo{
Provider: "Github",
Name: "commons",
Url: CommonsRepoURL,
Priority: 0,
}
s := spinner.StartNew("Adding the commons repository...")
time.Sleep(time.Second * 2)

s := spinner.StartNew("Adding the commons repository...")
time.Sleep(time.Second * 2)
repoInfo := github.NewRepoInfo(repo.Url, repo.Token)

repoInfo := github.NewRepoInfo(repo.Url, repo.Token)
tag, err := in.git.LatestTag(repoInfo)
if err != nil {
s.Error(ErrInitCommonsRepo)
fmt.Println(addRepoMsg)
}

tag, err := in.git.LatestTag(repoInfo)
if err != nil {
s.Error(ErrInitCommonsRepo)
fmt.Println(addRepoMsg)
return nil
}
repo.Version = formula.RepoVersion(tag.Name)

repo.Version = formula.RepoVersion(tag.Name)
if err := in.repo.Add(repo); err != nil {
s.Error(ErrInitCommonsRepo)
fmt.Println(addRepoMsg)
}

if err := in.repo.Add(repo); err != nil {
s.Error(ErrInitCommonsRepo)
fmt.Println(addRepoMsg)
return nil
s.Success(prompt.Green("Commons repository added successfully!"))
}

s.Success(prompt.Green("Initialization successful!"))
err = metricsAuthorization(in.InputList, in.FileWriteReadExister)
if err != nil {
return err
}

tutorialHolder, err := in.rt.Find()
if err != nil {
Expand All @@ -126,11 +134,38 @@ func tutorialInit(tutorialStatus string) {
const tagTutorial = "\n[TUTORIAL]"
const MessageTitle = "How to create new formulas:"
const MessageBody = ` ∙ Run "rit create formula"
∙ Open the project with your favorite text editor.` + "\n"
∙ Open the project with your favorite text editor.` + "\n"

if tutorialStatus == tutorialStatusEnabled {
prompt.Info(tagTutorial)
prompt.Info(MessageTitle)
fmt.Println(MessageBody)
}
}

func metricsAuthorization(inList prompt.InputList, file stream.FileWriteReadExister) error {
const welcome = "\n\nWelcome to Ritchie!"
const header = "Ritchie is a platform that helps you and your team to save time by giving you the power to create powerful templates to execute important tasks across your team and organization with minimum time and with standards, delivering autonomy to developers with security.\nYou can view our Privacy Policy (http://insights.zup.com.br/politica-privacidade) to better understand our commitment."
const footer = "You can always modify your choice using the \"rit metrics\" command."
options := []string{AcceptMetrics, DoNotAcceptMetrics}

prompt.Info(welcome)
fmt.Println(header)

choose, err := inList.List(AddMetricsQuestion, options)
if err != nil {
return err
}
fmt.Println(footer)

responseToWrite := "yes"
if choose == DoNotAcceptMetrics {
responseToWrite = "no"
}

err = file.Write(metric.MetricsPath(), []byte(responseToWrite))
if err != nil {
return err
}
return nil
}
85 changes: 78 additions & 7 deletions pkg/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,29 @@ import (

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
sMocks "github.com/ZupIT/ritchie-cli/pkg/stream/mocks"
)

func TestNewSingleInitCmd(t *testing.T) {
cmd := NewInitCmd(defaultRepoAdderMock, defaultGitRepositoryMock, TutorialFinderMock{}, inputTrueMock{})
func TestNewInitCmd(t *testing.T) {
cmd := NewInitCmd(
defaultRepoAdderMock,
defaultGitRepositoryMock,
TutorialFinderMock{},
inputListCustomMock{
list: func(name string, items []string) (string, error) {
if name == AddCommonsQuestion {
return "yes", nil
}
return AcceptMetrics, nil
},
},
sMocks.FileWriteReadExisterCustomMock{
WriteMock: func(path string, content []byte) error {
return nil
},
},
)
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")

if cmd == nil {
Expand All @@ -39,9 +58,11 @@ func TestNewSingleInitCmd(t *testing.T) {
}

func Test_initCmd_runPrompt(t *testing.T) {
someError := errors.New("some error")
type fields struct {
repo formula.RepositoryAdder
git git.Repositories
repo formula.RepositoryAdder
git git.Repositories
inputList prompt.InputList
}

tests := []struct {
Expand All @@ -54,6 +75,14 @@ func Test_initCmd_runPrompt(t *testing.T) {
fields: fields{
repo: defaultRepoAdderMock,
git: defaultGitRepositoryMock,
inputList: inputListCustomMock{
list: func(name string, items []string) (string, error) {
if name == AddCommonsQuestion {
return "yes", nil
}
return AcceptMetrics, nil
},
},
},
wantErr: false,
},
Expand All @@ -63,7 +92,15 @@ func Test_initCmd_runPrompt(t *testing.T) {
repo: defaultRepoAdderMock,
git: GitRepositoryMock{
latestTag: func(info git.RepoInfo) (git.Tag, error) {
return git.Tag{}, errors.New("some error")
return git.Tag{}, someError
},
},
inputList: inputListCustomMock{
list: func(name string, items []string) (string, error) {
if name == AddCommonsQuestion {
return "yes", nil
}
return AcceptMetrics, nil
},
},
},
Expand All @@ -74,17 +111,51 @@ func Test_initCmd_runPrompt(t *testing.T) {
fields: fields{
repo: repoListerAdderCustomMock{
add: func(d formula.Repo) error {
return errors.New("some error")
return someError
},
},
git: defaultGitRepositoryMock,
inputList: inputListCustomMock{
list: func(name string, items []string) (string, error) {
if name == AddCommonsQuestion {
return "yes", nil
}
return AcceptMetrics, nil
},
},
},
wantErr: false,
},
{
name: "Error in select response of metrics",
fields: fields{
repo: defaultRepoAdderMock,
git: defaultGitRepositoryMock,
inputList: inputListCustomMock{
list: func(name string, items []string) (string, error) {
if name == AddCommonsQuestion {
return "yes", nil
}
return "any", someError
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := NewInitCmd(tt.fields.repo, tt.fields.git, TutorialFinderMock{}, inputTrueMock{})
o := NewInitCmd(
tt.fields.repo,
tt.fields.git,
TutorialFinderMock{},
tt.fields.inputList,
sMocks.FileWriteReadExisterCustomMock{
WriteMock: func(path string, content []byte) error {
return nil
},
},
)
o.PersistentFlags().Bool("stdin", false, "input by stdin")
if err := o.Execute(); (err != nil) != tt.wantErr {
t.Errorf("init_runPrompt() error = %v, wantErr %v", err, tt.wantErr)
Expand Down