diff --git a/cmd/main.go b/cmd/main.go index 2c038da7c..fd1b24710 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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() diff --git a/functional/init/init_feature.json b/functional/init/init_feature.json index ab0d1bd61..317a5e415 100644 --- a/functional/init/init_feature.json +++ b/functional/init/init_feature.json @@ -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":"" diff --git a/functional/runner_unix.go b/functional/runner_unix.go index b8c6bf33b..197c9a24b 100644 --- a/functional/runner_unix.go +++ b/functional/runner_unix.go @@ -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 { diff --git a/pkg/cmd/init.go b/pkg/cmd/init.go index a30ffc399..a1ccf7dac 100644 --- a/pkg/cmd/init.go +++ b/pkg/cmd/init.go @@ -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/metrics" + "github.com/ZupIT/ritchie-cli/pkg/stream" "github.com/kaduartur/go-cli-spinner/pkg/spinner" @@ -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" @@ -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", @@ -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 { @@ -126,7 +134,7 @@ 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) @@ -134,3 +142,30 @@ func tutorialInit(tutorialStatus string) { 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(metrics.FilePath, []byte(responseToWrite)) + if err != nil { + return err + } + return nil +} diff --git a/pkg/cmd/init_test.go b/pkg/cmd/init_test.go index 98400d383..68732f1d6 100644 --- a/pkg/cmd/init_test.go +++ b/pkg/cmd/init_test.go @@ -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 { @@ -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 { @@ -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, }, @@ -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 }, }, }, @@ -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)