diff --git a/cmd/main.go b/cmd/main.go index 63f35a522..2c038da7c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -22,9 +22,6 @@ import ( "os" "time" - "github.com/ZupIT/ritchie-cli/pkg/git/github" - "github.com/ZupIT/ritchie-cli/pkg/git/gitlab" - "k8s.io/kubectl/pkg/util/templates" "github.com/ZupIT/ritchie-cli/pkg/credential" @@ -34,6 +31,9 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/formula/repo" "github.com/ZupIT/ritchie-cli/pkg/formula/runner" "github.com/ZupIT/ritchie-cli/pkg/formula/tree" + "github.com/ZupIT/ritchie-cli/pkg/git/github" + "github.com/ZupIT/ritchie-cli/pkg/git/gitlab" + "github.com/ZupIT/ritchie-cli/pkg/metrics" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" "github.com/ZupIT/ritchie-cli/pkg/upgrade" @@ -116,7 +116,7 @@ func buildCommands() *cobra.Command { tutorialFinder := rtutorial.NewFinder(ritchieHomeDir, fileManager) tutorialSetter := rtutorial.NewSetter(ritchieHomeDir, fileManager) tutorialFindSetter := rtutorial.NewFindSetter(ritchieHomeDir, tutorialFinder, tutorialSetter) - + metricsChecker := metrics.NewChecker(fileManager) formBuildMake := builder.NewBuildMake() formBuildBat := builder.NewBuildBat(fileManager) formBuildDocker := builder.NewBuildDocker() @@ -154,7 +154,7 @@ func buildCommands() *cobra.Command { updateCmd := cmd.NewUpdateCmd() buildCmd := cmd.NewBuildCmd() upgradeCmd := cmd.NewUpgradeCmd(defaultUpgradeResolver, upgradeManager, defaultUrlFinder) - metricsCmd := cmd.NewMetricsCmd(fileManager, inputList) + metricsCmd := cmd.NewMetricsCmd(fileManager, inputList, metricsChecker) tutorialCmd := cmd.NewTutorialCmd(ritchieHomeDir, inputList, tutorialFindSetter) // level 2 diff --git a/pkg/cmd/metrics.go b/pkg/cmd/metrics.go index 03896d46f..d23d3f8fc 100644 --- a/pkg/cmd/metrics.go +++ b/pkg/cmd/metrics.go @@ -3,7 +3,7 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/ZupIT/ritchie-cli/pkg/metric" + "github.com/ZupIT/ritchie-cli/pkg/metrics" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/stream" ) @@ -11,12 +11,14 @@ import ( type metricsCmd struct { stream.FileWriteReadExister prompt.InputList + metrics.Checker } -func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList) *cobra.Command { +func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList, checker metrics.Checker) *cobra.Command { m := &metricsCmd{ FileWriteReadExister: file, InputList: inList, + Checker: checker, } cmd := &cobra.Command{ @@ -32,33 +34,34 @@ func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList) *c func (m metricsCmd) run() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { - if !m.FileWriteReadExister.Exists(metric.MetricsPath()) { + path := metrics.FilePath + if !m.FileWriteReadExister.Exists(path) { options := []string{"yes", "no"} choose, err := m.InputList.List("You want to send anonymous data about the product, feature use, statistics and crash reports?", options) if err != nil { return err } - err = m.FileWriteReadExister.Write(metric.MetricsPath(), []byte(choose)) + err = m.FileWriteReadExister.Write(path, []byte(choose)) if err != nil { return err } return nil } - metricsStatus, err := m.FileWriteReadExister.Read(metric.MetricsPath()) + metricsStatus, err := m.Check() if err != nil { return err } - changeTo := "no" - message := "You are no longer sending anonymous metrics." - if string(metricsStatus) == changeTo { - changeTo = "yes" - message = "You are now sending anonymous metrics. Thank you!" + changeTo := "yes" + message := "You are now sending anonymous metrics. Thank you!" + if metricsStatus { + changeTo = "no" + message = "You are no longer sending anonymous metrics." } - err = m.FileWriteReadExister.Write(metric.MetricsPath(), []byte(changeTo)) + err = m.FileWriteReadExister.Write(path, []byte(changeTo)) if err != nil { return err } diff --git a/pkg/cmd/metrics_test.go b/pkg/cmd/metrics_test.go index 8241c2d48..ce56288a8 100644 --- a/pkg/cmd/metrics_test.go +++ b/pkg/cmd/metrics_test.go @@ -11,8 +11,9 @@ import ( func Test_metricsCmd_runPrompt(t *testing.T) { type in struct { - file stream.FileWriteReadExister - InputList prompt.InputList + file stream.FileWriteReadExister + InputList prompt.InputList + checkerMock CheckerMock } var tests = []struct { @@ -89,30 +90,30 @@ func Test_metricsCmd_runPrompt(t *testing.T) { ExistsMock: func(path string) bool { return true }, - ReadMock: func(path string) ([]byte, error) { - return []byte("no"), nil - }, WriteMock: func(path string, content []byte) error { return nil }, }, + checkerMock: CheckerMock{func() (bool, error) { + return true, nil + }}, }, wantErr: false, }, { - name: "fail on read when metrics file exist", + name: "fail on check when metrics file exist", in: in{ file: sMocks.FileWriteReadExisterCustomMock{ ExistsMock: func(path string) bool { return true }, - ReadMock: func(path string) ([]byte, error) { - return []byte("no"), errors.New("error reading file") - }, WriteMock: func(path string, content []byte) error { return nil }, }, + checkerMock: CheckerMock{func() (bool, error) { + return false, errors.New("error reading file") + }}, }, wantErr: true, }, @@ -123,13 +124,13 @@ func Test_metricsCmd_runPrompt(t *testing.T) { ExistsMock: func(path string) bool { return true }, - ReadMock: func(path string) ([]byte, error) { - return []byte("no"), nil - }, WriteMock: func(path string, content []byte) error { return errors.New("error writing file") }, }, + checkerMock: CheckerMock{func() (bool, error) { + return false, nil + }}, }, wantErr: true, }, @@ -137,11 +138,18 @@ func Test_metricsCmd_runPrompt(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - metricsCmd := NewMetricsCmd(tt.in.file, tt.in.InputList) + metricsCmd := NewMetricsCmd(tt.in.file, tt.in.InputList, tt.in.checkerMock) if err := metricsCmd.Execute(); (err != nil) != tt.wantErr { - t.Errorf("metrics command error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("metrics command error = %v | error wanted: %v", err, tt.wantErr) } }) } +} + +type CheckerMock struct { + CheckMock func() (bool, error) +} +func (cm CheckerMock) Check() (bool, error) { + return cm.CheckMock() } diff --git a/pkg/metrics/checker.go b/pkg/metrics/checker.go new file mode 100644 index 000000000..97ac59c86 --- /dev/null +++ b/pkg/metrics/checker.go @@ -0,0 +1,48 @@ +/* + * 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 metrics + +import ( + "github.com/ZupIT/ritchie-cli/pkg/stream" +) + +type CheckManager struct { + file stream.FileReadExister +} + +func NewChecker(file stream.FileReadExister) CheckManager { + return CheckManager{file: file} +} + +func (c CheckManager) Check() (bool, error) { + if !c.file.Exists(FilePath) { + return false, nil + } + + bytes, err := c.file.Read(FilePath) + if err != nil { + return false, err + } + + result := true + if string(bytes) == "no" { + result = false + } + + return result, nil +} + diff --git a/pkg/metrics/checker_test.go b/pkg/metrics/checker_test.go new file mode 100644 index 000000000..5b6068b5f --- /dev/null +++ b/pkg/metrics/checker_test.go @@ -0,0 +1,110 @@ +/* + * 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 metrics + +import ( + "errors" + "testing" + + "github.com/ZupIT/ritchie-cli/pkg/stream" + sMocks "github.com/ZupIT/ritchie-cli/pkg/stream/mocks" +) + +func Test_Check(t *testing.T) { + type in struct { + file stream.FileReadExister + } + + var tests = []struct { + name string + wantErr bool + expectedResult bool + in in + }{ + { + name: "success case expecting true", + wantErr: false, + expectedResult: true, + in: in{ + file: sMocks.FileReadExisterCustomMock{ + ReadMock: func(path string) ([]byte, error) { + return []byte("yes"), nil + }, + ExistsMock: func(path string) bool { + return true + }, + }, + }, + }, + { + name: "success case expecting false", + wantErr: false, + expectedResult: false, + in: in{ + file: sMocks.FileReadExisterCustomMock{ + ReadMock: func(path string) ([]byte, error) { + return []byte("no"), nil + }, + ExistsMock: func(path string) bool { + return true + }, + }, + }, + }, + { + name: "success case when metrics file doesn't exist", + wantErr: false, + expectedResult: false, + in: in{ + file: sMocks.FileReadExisterCustomMock{ + ExistsMock: func(path string) bool { + return false + }, + }, + }, + }, + { + name: "fail case error on reading file", + wantErr: true, + expectedResult: false, + in: in{ + file: sMocks.FileReadExisterCustomMock{ + ReadMock: func(path string) ([]byte, error) { + return nil, errors.New("error reading file") + }, + ExistsMock: func(path string) bool { + return true + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + check := NewChecker(tt.in.file) + result, err := check.Check() + if (err != nil) != tt.wantErr { + t.Errorf("execution test failed: %s\nwant error: %t | got: %s", tt.name, tt.wantErr, err) + } + if result != tt.expectedResult { + t.Errorf("behavior test failed: %s\nwant: %t | got: %t", tt.name, tt.expectedResult, result) + } + }) + } +} + diff --git a/pkg/metric/metric.go b/pkg/metrics/metric.go similarity index 85% rename from pkg/metric/metric.go rename to pkg/metrics/metric.go index dd532926a..be788079f 100644 --- a/pkg/metric/metric.go +++ b/pkg/metrics/metric.go @@ -13,18 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package metric + +package metrics import ( - "os" "path/filepath" "time" + + "github.com/ZupIT/ritchie-cli/pkg/api" ) -func MetricsPath()string { - hd, _ := os.UserHomeDir() - return filepath.Join(hd, ".rit", "metrics") -} +var FilePath = filepath.Join(api.RitchieHomeDir(), "metrics") type Id string @@ -44,3 +43,7 @@ type Sender interface { type UserIdGenerator interface { Generate() (UserId, error) } + +type Checker interface { + Check() (bool, error) +} diff --git a/pkg/metric/user_id_generator.go b/pkg/metrics/user_id_generator.go similarity index 98% rename from pkg/metric/user_id_generator.go rename to pkg/metrics/user_id_generator.go index e2dcd6faa..bd22aa253 100644 --- a/pkg/metric/user_id_generator.go +++ b/pkg/metrics/user_id_generator.go @@ -13,7 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package metric + +package metrics import ( "crypto/sha256" diff --git a/pkg/metric/user_id_generator_test.go b/pkg/metrics/user_id_generator_test.go similarity index 98% rename from pkg/metric/user_id_generator_test.go rename to pkg/metrics/user_id_generator_test.go index 31c566b40..71a10ec37 100644 --- a/pkg/metric/user_id_generator_test.go +++ b/pkg/metrics/user_id_generator_test.go @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package metric +package metrics import "testing" diff --git a/pkg/stream/mocks/mock.go b/pkg/stream/mocks/mock.go index b401f62d5..f40c4bbc8 100644 --- a/pkg/stream/mocks/mock.go +++ b/pkg/stream/mocks/mock.go @@ -21,7 +21,6 @@ type FileReadExisterCustomMock struct { ExistsMock func(path string) bool } - // Read of FileManagerCustomMock func (fmc FileReadExisterCustomMock) Read(path string) ([]byte, error) { return fmc.ReadMock(path)