Implement metrics for `docker scan`

This commit is contained in:
Djordje Lukic 2020-07-03 10:55:28 +02:00
parent ae76e0cf27
commit a97a26bfc3
2 changed files with 77 additions and 8 deletions

View File

@ -95,8 +95,6 @@ func TestFlag(t *testing.T) {
func TestEcs(t *testing.T) { func TestEcs(t *testing.T) {
root := &cobra.Command{} root := &cobra.Command{}
root.PersistentFlags().BoolP("debug", "d", false, "debug")
root.PersistentFlags().String("str", "str", "str")
testCases := []struct { testCases := []struct {
name string name string
@ -167,3 +165,51 @@ func TestEcs(t *testing.T) {
}) })
} }
} }
func TestScan(t *testing.T) {
root := &cobra.Command{}
testCases := []struct {
name string
args []string
expected string
}{
{
name: "scan",
args: []string{"scan"},
expected: "scan",
},
{
name: "scan image with long flags",
args: []string{"scan", "--file", "file", "image"},
expected: "scan",
},
{
name: "scan image with short flags",
args: []string{"scan", "-f", "file", "image"},
expected: "scan",
},
{
name: "scan with long flag",
args: []string{"scan", "--dependency-tree", "image"},
expected: "scan",
},
{
name: "auth",
args: []string{"scan", "--auth"},
expected: "scan auth",
},
{
name: "version",
args: []string{"scan", "--version"},
expected: "scan version",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := getCommand(testCase.args, root.PersistentFlags())
assert.Equal(t, testCase.expected, result)
})
}
}

View File

@ -50,6 +50,10 @@ var managementCommands = []string{
"volume", "volume",
} }
const (
scanCommand = "scan"
)
// Track sends the tracking analytics to Docker Desktop // Track sends the tracking analytics to Docker Desktop
func Track(context string, args []string, flags *flag.FlagSet) { func Track(context string, args []string, flags *flag.FlagSet) {
// Fire and forget, we don't want to slow down the user waiting for DD // Fire and forget, we don't want to slow down the user waiting for DD
@ -71,16 +75,21 @@ func Track(context string, args []string, flags *flag.FlagSet) {
func getCommand(args []string, flags *flag.FlagSet) string { func getCommand(args []string, flags *flag.FlagSet) string {
command := "" command := ""
args = stripFlags(args, flags) strippedArgs := stripFlags(args, flags)
if len(strippedArgs) != 0 {
command = strippedArgs[0]
if command == scanCommand {
return getScanCommand(args)
}
if len(args) != 0 {
command = args[0]
for { for {
currentCommand := args[0] currentCommand := strippedArgs[0]
if contains(managementCommands, currentCommand) { if contains(managementCommands, currentCommand) {
if sub := getSubCommand(args[1:]); sub != "" { if sub := getSubCommand(strippedArgs[1:]); sub != "" {
command += " " + sub command += " " + sub
args = args[1:] strippedArgs = strippedArgs[1:]
continue continue
} }
} }
@ -91,6 +100,20 @@ func getCommand(args []string, flags *flag.FlagSet) string {
return command return command
} }
func getScanCommand(args []string) string {
command := args[0]
if contains(args, "--auth") {
return command + " auth"
}
if contains(args, "--version") {
return command + " version"
}
return command
}
func getSubCommand(args []string) string { func getSubCommand(args []string) string {
if len(args) != 0 && isArg(args[0]) { if len(args) != 0 && isArg(args[0]) {
return args[0] return args[0]