Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #333 from docker/feat-scan-metrics
Browse files Browse the repository at this point in the history
Implement metrics for `docker scan`
  • Loading branch information
rumpl authored Jul 3, 2020
2 parents ae76e0c + a97a26b commit c903362
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
50 changes: 48 additions & 2 deletions metrics/metics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}
}
35 changes: 29 additions & 6 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -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]
Expand Down

0 comments on commit c903362

Please sign in to comment.