diff --git a/cmd/main.go b/cmd/main.go index 4c82edeff..fa0e7a3d9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -76,6 +76,7 @@ func executionTime(startTime time.Time) float64 { } var Data metric.DataCollectorManager +var MetricSender = metric.NewHttpSender(metric.ServerRestURL, http.DefaultClient) func buildCommands() *cobra.Command { userHomeDir := api.UserHomeDir() @@ -176,7 +177,7 @@ func buildCommands() *cobra.Command { addCmd := cmd.NewAddCmd() createCmd := cmd.NewCreateCmd() deleteCmd := cmd.NewDeleteCmd() - initCmd := cmd.NewInitCmd(repoAdder, githubRepo, tutorialFinder, configManager, fileManager, inputList, inputBool) + initCmd := cmd.NewInitCmd(repoAdder, githubRepo, tutorialFinder, configManager, fileManager, inputList, inputBool, MetricSender) listCmd := cmd.NewListCmd() setCmd := cmd.NewSetCmd() showCmd := cmd.NewShowCmd() @@ -285,8 +286,7 @@ func sendMetric(commandExecutionTime float64, err ...string) { metricEnable := metric.NewChecker(stream.NewFileManager()) if metricEnable.Check() { var collectData metric.APIData - metricManager := metric.NewHttpSender(metric.ServerRestURL, http.DefaultClient) collectData, _ = Data.Collect(commandExecutionTime, cmd.Version, err...) - metricManager.Send(collectData) + MetricSender.Send(collectData) } } diff --git a/pkg/cmd/init.go b/pkg/cmd/init.go index a29ad4a09..741be98ed 100644 --- a/pkg/cmd/init.go +++ b/pkg/cmd/init.go @@ -78,6 +78,7 @@ type initCmd struct { file stream.FileWriteReadExister prompt.InputList prompt.InputBool + metricSender metric.SendManagerHttp } func NewInitCmd( @@ -88,8 +89,18 @@ func NewInitCmd( file stream.FileWriteReadExister, inList prompt.InputList, inBool prompt.InputBool, + metricSender metric.SendManagerHttp, ) *cobra.Command { - o := initCmd{repo: repo, git: git, tutorial: tutorial, config: config, file: file, InputList: inList, InputBool: inBool} + o := initCmd{ + repo: repo, + git: git, + tutorial: tutorial, + config: config, + file: file, + InputList: inList, + InputBool: inBool, + metricSender: metricSender, + } cmd := &cobra.Command{ Use: "init", @@ -232,6 +243,13 @@ You can view our Privacy Policy (http://insights.zup.com.br/politica-privacidade responseToWrite := "yes" if choose == DoNotAcceptMetrics { responseToWrite = "no" + in.metricSender.Send(metric.APIData{ + Id: "rit_init", + Timestamp: time.Now(), + Data: metric.Data{ + MetricsAcceptance: responseToWrite, + }, + }) } if err = in.file.Write(metric.FilePath, []byte(responseToWrite)); err != nil { diff --git a/pkg/cmd/init_test.go b/pkg/cmd/init_test.go index 43dfb76e7..facab3aa0 100644 --- a/pkg/cmd/init_test.go +++ b/pkg/cmd/init_test.go @@ -18,11 +18,13 @@ package cmd import ( "errors" + "net/http" "strings" "testing" "github.com/ZupIT/ritchie-cli/pkg/formula" "github.com/ZupIT/ritchie-cli/pkg/git" + "github.com/ZupIT/ritchie-cli/pkg/metric" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" "github.com/ZupIT/ritchie-cli/pkg/stream" @@ -359,8 +361,28 @@ func Test_initCmd_runAnyEntry(t *testing.T) { t.Run(tt.name, func(t *testing.T) { field := tt.fields - initPrompt := NewInitCmd(field.repo, field.git, field.tutorial, field.config, field.file, field.inList, field.inBool) - initStdin := NewInitCmd(field.repo, field.git, field.tutorial, field.config, field.file, field.inList, field.inBool) + metricSender := metric.NewHttpSender("", http.DefaultClient) + + initPrompt := NewInitCmd( + field.repo, + field.git, + field.tutorial, + field.config, + field.file, + field.inList, + field.inBool, + metricSender, + ) + initStdin := NewInitCmd( + field.repo, + field.git, + field.tutorial, + field.config, + field.file, + field.inList, + field.inBool, + metricSender, + ) initPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") initStdin.PersistentFlags().Bool("stdin", true, "input by stdin") diff --git a/pkg/metric/metric.go b/pkg/metric/metric.go index 46e957bec..0ec6460db 100644 --- a/pkg/metric/metric.go +++ b/pkg/metric/metric.go @@ -55,6 +55,7 @@ type Data struct { CommandError string `json:"commandError,omitempty"` CommonsRepoAdded string `json:"commonsRepoAdded,omitempty"` CommandExecutionTime float64 `json:"commandExecutionTime"` + MetricsAcceptance string `json:"metricsAcceptance,omitempty"` FormulaRepo formula.Repo `json:"repo,omitempty"` }