metrics.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "strings"
  16. flag "github.com/spf13/pflag"
  17. )
  18. var managementCommands = []string{
  19. "app",
  20. "assemble",
  21. "builder",
  22. "buildx",
  23. "ecs",
  24. "cluster",
  25. "compose",
  26. "config",
  27. "container",
  28. "context",
  29. "help",
  30. "image",
  31. "manifest",
  32. "network",
  33. "node",
  34. "plugin",
  35. "registry",
  36. "secret",
  37. "service",
  38. "stack",
  39. "swarm",
  40. "system",
  41. "template",
  42. "trust",
  43. "volume",
  44. }
  45. const (
  46. scanCommand = "scan"
  47. )
  48. // Track sends the tracking analytics to Docker Desktop
  49. func Track(context string, args []string, flags *flag.FlagSet) {
  50. // Fire and forget, we don't want to slow down the user waiting for DD
  51. // metrics endpoint to respond. We could lose some events but that's ok.
  52. go func() {
  53. defer func() {
  54. _ = recover()
  55. }()
  56. command := getCommand(args, flags)
  57. if command != "" {
  58. c := NewClient()
  59. c.Send(Command{
  60. Command: command,
  61. Context: context,
  62. })
  63. }
  64. }()
  65. }
  66. func getCommand(args []string, flags *flag.FlagSet) string {
  67. command := ""
  68. strippedArgs := stripFlags(args, flags)
  69. if len(strippedArgs) != 0 {
  70. command = strippedArgs[0]
  71. if command == scanCommand {
  72. return getScanCommand(args)
  73. }
  74. for {
  75. currentCommand := strippedArgs[0]
  76. if contains(managementCommands, currentCommand) {
  77. if sub := getSubCommand(strippedArgs[1:]); sub != "" {
  78. command += " " + sub
  79. strippedArgs = strippedArgs[1:]
  80. continue
  81. }
  82. }
  83. break
  84. }
  85. }
  86. return command
  87. }
  88. func getScanCommand(args []string) string {
  89. command := args[0]
  90. if contains(args, "--auth") {
  91. return command + " auth"
  92. }
  93. if contains(args, "--version") {
  94. return command + " version"
  95. }
  96. return command
  97. }
  98. func getSubCommand(args []string) string {
  99. if len(args) != 0 && isArg(args[0]) {
  100. return args[0]
  101. }
  102. return ""
  103. }
  104. func contains(array []string, needle string) bool {
  105. for _, val := range array {
  106. if val == needle {
  107. return true
  108. }
  109. }
  110. return false
  111. }
  112. func stripFlags(args []string, flags *flag.FlagSet) []string {
  113. commands := []string{}
  114. for len(args) > 0 {
  115. s := args[0]
  116. args = args[1:]
  117. if s == "--" {
  118. return commands
  119. }
  120. if flagArg(s, flags) {
  121. if len(args) <= 1 {
  122. return commands
  123. }
  124. args = args[1:]
  125. }
  126. if isArg(s) {
  127. commands = append(commands, s)
  128. }
  129. }
  130. return commands
  131. }
  132. func flagArg(s string, flags *flag.FlagSet) bool {
  133. return strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags) ||
  134. strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags)
  135. }
  136. func isArg(s string) bool {
  137. return s != "" && !strings.HasPrefix(s, "-")
  138. }
  139. func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
  140. flag := fs.Lookup(name)
  141. if flag == nil {
  142. return false
  143. }
  144. return flag.NoOptDefVal != ""
  145. }
  146. func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
  147. flag := fs.ShorthandLookup(name[:1])
  148. if flag == nil {
  149. return false
  150. }
  151. return flag.NoOptDefVal != ""
  152. }