Merge pull request #333 from docker/feat-scan-metrics

Implement metrics for `docker scan`
This commit is contained in:
Djordje Lukic 2020-07-03 14:31:16 +02:00 committed by GitHub
commit c903362d02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) {
root := &cobra.Command{}
root.PersistentFlags().BoolP("debug", "d", false, "debug")
root.PersistentFlags().String("str", "str", "str")
testCases := []struct {
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",
}
const (
scanCommand = "scan"
)
// Track sends the tracking analytics to Docker Desktop
func Track(context string, args []string, flags *flag.FlagSet) {
// 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 {
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 {
currentCommand := args[0]
currentCommand := strippedArgs[0]
if contains(managementCommands, currentCommand) {
if sub := getSubCommand(args[1:]); sub != "" {
if sub := getSubCommand(strippedArgs[1:]); sub != "" {
command += " " + sub
args = args[1:]
strippedArgs = strippedArgs[1:]
continue
}
}
@ -91,6 +100,20 @@ func getCommand(args []string, flags *flag.FlagSet) string {
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 {
if len(args) != 0 && isArg(args[0]) {
return args[0]