Merge pull request #906 from gtardif/help_metrics

Collect metrics --help flag
This commit is contained in:
Guillaume Tardif 2020-11-13 15:10:26 +01:00 committed by GitHub
commit 0ae42dea04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 33 deletions

View File

@ -23,6 +23,7 @@ var commandFlags = []string{
// Generated with generatecommands/main.go // Generated with generatecommands/main.go
var managementCommands = []string{ var managementCommands = []string{
"help",
"ecs", "ecs",
"scan", "scan",
"app", "app",

View File

@ -35,6 +35,7 @@ func main() {
fmt.Printf(` fmt.Printf(`
var managementCommands = []string{ var managementCommands = []string{
"help",
"%s", "%s",
} }

View File

@ -17,6 +17,8 @@
package metrics package metrics
import ( import (
"strings"
"github.com/docker/compose-cli/utils" "github.com/docker/compose-cli/utils"
) )
@ -52,18 +54,15 @@ func GetCommand(args []string) string {
onlyFlags := false onlyFlags := false
for _, arg := range args { for _, arg := range args {
if arg == "--help" { if arg == "--help" {
return "" result = strings.TrimSpace(arg + " " + result)
continue
} }
if arg == "--" { if arg == "--" {
break break
} }
if isCommandFlag(arg) || (!onlyFlags && isCommand(arg)) { if isCommandFlag(arg) || (!onlyFlags && isCommand(arg)) {
if result == "" { result = strings.TrimSpace(result + " " + arg)
result = arg if isCommand(arg) && !isManagementCommand(arg) {
} else {
result = result + " " + arg
}
if !isManagementCommand(arg) {
onlyFlags = true onlyFlags = true
} }
} }

View File

@ -153,26 +153,31 @@ func TestGetCommand(t *testing.T) {
} }
} }
func TestIgnoreHelpCommands(t *testing.T) { func TestKeepHelpCommands(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
args []string args []string
expected string expected string
}{ }{
{ {
name: "help", name: "run with help flag",
args: []string{"--help"},
expected: "",
},
{
name: "help on run",
args: []string{"run", "--help"}, args: []string{"run", "--help"},
expected: "", expected: "--help run",
}, },
{ {
name: "help on compose up", name: "with help flag before-after commands",
args: []string{"compose", "up", "--help"}, args: []string{"compose", "--help", "up"},
expected: "", expected: "--help compose up",
},
{
name: "help flag",
args: []string{"--help"},
expected: "--help",
},
{
name: "help commands",
args: []string{"help", "run"},
expected: "help run",
}, },
} }

View File

@ -161,15 +161,19 @@ func TestContextMetrics(t *testing.T) {
s.Start() s.Start()
defer s.Stop() defer s.Stop()
t.Run("do not send metrics on help commands", func(t *testing.T) { t.Run("send metrics on help commands", func(t *testing.T) {
s.ResetUsage() s.ResetUsage()
c.RunDockerCmd("help", "run")
c.RunDockerCmd("--help") c.RunDockerCmd("--help")
c.RunDockerCmd("ps", "--help")
c.RunDockerCmd("run", "--help") c.RunDockerCmd("run", "--help")
usage := s.GetUsage() usage := s.GetUsage()
assert.Equal(t, 0, len(usage)) assert.DeepEqual(t, []string{
`{"command":"help run","context":"moby","source":"cli","status":"success"}`,
`{"command":"--help","context":"moby","source":"cli","status":"success"}`,
`{"command":"--help run","context":"moby","source":"cli","status":"success"}`,
}, usage)
}) })
t.Run("metrics on default context", func(t *testing.T) { t.Run("metrics on default context", func(t *testing.T) {
@ -180,10 +184,11 @@ func TestContextMetrics(t *testing.T) {
c.RunDockerOrExitError("version", "--xxx") c.RunDockerOrExitError("version", "--xxx")
usage := s.GetUsage() usage := s.GetUsage()
assert.Equal(t, 3, len(usage)) assert.DeepEqual(t, []string{
assert.Equal(t, `{"command":"ps","context":"moby","source":"cli","status":"success"}`, usage[0]) `{"command":"ps","context":"moby","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"version","context":"moby","source":"cli","status":"success"}`, usage[1]) `{"command":"version","context":"moby","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"version","context":"moby","source":"cli","status":"failure"}`, usage[2]) `{"command":"version","context":"moby","source":"cli","status":"failure"}`,
}, usage)
}) })
t.Run("metrics on other context type", func(t *testing.T) { t.Run("metrics on other context type", func(t *testing.T) {
@ -198,14 +203,15 @@ func TestContextMetrics(t *testing.T) {
c.RunDockerCmd("--context", "test-example", "ps") c.RunDockerCmd("--context", "test-example", "ps")
usage := s.GetUsage() usage := s.GetUsage()
assert.Equal(t, 7, len(usage)) assert.DeepEqual(t, []string{
assert.Equal(t, `{"command":"context create","context":"moby","source":"cli","status":"success"}`, usage[0]) `{"command":"context create","context":"moby","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"ps","context":"moby","source":"cli","status":"success"}`, usage[1]) `{"command":"ps","context":"moby","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"context use","context":"moby","source":"cli","status":"success"}`, usage[2]) `{"command":"context use","context":"moby","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"ps","context":"example","source":"cli","status":"success"}`, usage[3]) `{"command":"ps","context":"example","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"stop","context":"example","source":"cli","status":"failure"}`, usage[4]) `{"command":"stop","context":"example","source":"cli","status":"failure"}`,
assert.Equal(t, `{"command":"context use","context":"example","source":"cli","status":"success"}`, usage[5]) `{"command":"context use","context":"example","source":"cli","status":"success"}`,
assert.Equal(t, `{"command":"ps","context":"example","source":"cli","status":"success"}`, usage[6]) `{"command":"ps","context":"example","source":"cli","status":"success"}`,
}, usage)
}) })
} }