This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from victor-schumacher/feature/metricsCmd
[FEATURE] add metrics command
- Loading branch information
Showing
6 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters