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

Commit

Permalink
Merge pull request #394 from victor-schumacher/feature/metricsCmd
Browse files Browse the repository at this point in the history
[FEATURE] add metrics command
  • Loading branch information
victor-schumacher authored Aug 10, 2020
2 parents 2e5fb05 + ae6150c commit e6267dd
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
tutorialCmd := cmd.NewTutorialCmd(ritchieHomeDir, inputList, tutorialFindSetter)

// level 2
Expand Down Expand Up @@ -216,6 +216,7 @@ func buildCommands() *cobra.Command {
buildCmd,
upgradeCmd,
tutorialCmd,
metricsCmd,
},
},
}
Expand Down
68 changes: 68 additions & 0 deletions pkg/cmd/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"github.com/spf13/cobra"

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

type metricsCmd struct {
stream.FileWriteReadExister
prompt.InputList
}

func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList) *cobra.Command {
m := &metricsCmd{
FileWriteReadExister: file,
InputList: inList,
}

cmd := &cobra.Command{
Use: "metrics",
Short: "Turn metrics on and off",
Long: "Stop or start to send anonymous metrics to ritchie team.",
RunE: m.run(),
}

return cmd

}

func (m metricsCmd) run() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
if !m.FileWriteReadExister.Exists(metric.MetricsPath()) {
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))
if err != nil {
return err
}
return nil
}

metricsStatus, err := m.FileWriteReadExister.Read(metric.MetricsPath())
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!"
}

err = m.FileWriteReadExister.Write(metric.MetricsPath(), []byte(changeTo))
if err != nil {
return err
}
prompt.Info(message)
return nil
}
}
139 changes: 139 additions & 0 deletions pkg/cmd/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package cmd

import (
"errors"
"testing"

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

func Test_metricsCmd_runPrompt(t *testing.T) {
type in struct {
file stream.FileWriteReadExister
InputList prompt.InputList
}

var tests = []struct {
name string
wantErr bool
in in
}{
{
name: "success when metrics file dont exist",
wantErr: false,
in: in{
file: sMocks.FileWriteReadExisterCustomMock{
ExistsMock: func(path string) bool {
return false
},
ReadMock: func(path string) ([]byte, error) {
return []byte("some data"), nil
},
WriteMock: func(path string, content []byte) error {
return nil
},
},
InputList: inputListCustomMock{name: "yes"},
},
},
{
name: "fail on write file when metrics file dont exist",
wantErr: true,
in: in{
file: sMocks.FileWriteReadExisterCustomMock{
ExistsMock: func(path string) bool {
return false
},
ReadMock: func(path string) ([]byte, error) {
return []byte("some data"), nil
},
WriteMock: func(path string, content []byte) error {
return errors.New("reading file error")
},
},
InputList: inputListCustomMock{name: "yes"},
},
},
{
name: "fail on input list when metrics file dont exist",
wantErr: true,
in: in{
file: sMocks.FileWriteReadExisterCustomMock{
ExistsMock: func(path string) bool {
return false
},
ReadMock: func(path string) ([]byte, error) {
return []byte("some data"), nil
},
WriteMock: func(path string, content []byte) error {
return nil
},
},
InputList: inputListErrorMock{},
},
},
{
name: "success 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"), nil
},
WriteMock: func(path string, content []byte) error {
return nil
},
},
},
wantErr: false,
},
{
name: "fail on read 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
},
},
},
wantErr: true,
},
{
name: "fail on write 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"), nil
},
WriteMock: func(path string, content []byte) error {
return errors.New("error writing file")
},
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metricsCmd := NewMetricsCmd(tt.in.file, tt.in.InputList)
if err := metricsCmd.Execute(); (err != nil) != tt.wantErr {
t.Errorf("metrics command error = %v, wantErr %v", err, tt.wantErr)
}
})
}

}
1 change: 0 additions & 1 deletion pkg/cmd/set_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func Test_setCredentialCmd_runPrompt(t *testing.T) {
name string
in in
wantErr bool
want string
}{
{
name: "success run with no data",
Expand Down
11 changes: 10 additions & 1 deletion pkg/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
*/
package metric

import "time"
import (
"os"
"path/filepath"
"time"
)

func MetricsPath()string {
hd, _ := os.UserHomeDir()
return filepath.Join(hd, ".rit", "metrics")
}

type Id string

Expand Down
21 changes: 21 additions & 0 deletions pkg/stream/mocks/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type FileReadExisterCustomMock struct {
ExistsMock func(path string) bool
}


// Read of FileManagerCustomMock
func (fmc FileReadExisterCustomMock) Read(path string) ([]byte, error) {
return fmc.ReadMock(path)
Expand All @@ -44,3 +45,23 @@ type FileWriterMock struct{}
func (FileWriterMock) Write(path string, content []byte) error {
return nil
}

type FileWriteReadExisterCustomMock struct {
WriteMock func(path string, content []byte) error
ReadMock func(path string) ([]byte, error)
ExistsMock func(path string) bool
}

func (f FileWriteReadExisterCustomMock) Read(path string) ([]byte, error) {
return f.ReadMock(path)
}


func (f FileWriteReadExisterCustomMock) Exists(path string) bool {
return f.ExistsMock(path)
}

func (f FileWriteReadExisterCustomMock) Write(path string, content []byte) error {
return f.WriteMock(path, content)
}

0 comments on commit e6267dd

Please sign in to comment.