metrics.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // Track sends the tracking analytics to Docker Desktop
  46. func Track(context string, args []string, flags *flag.FlagSet) {
  47. // Fire and forget, we don't want to slow down the user waiting for DD
  48. // metrics endpoint to respond. We could lose some events but that's ok.
  49. go func() {
  50. defer func() {
  51. _ = recover()
  52. }()
  53. command := getCommand(args, flags)
  54. if command != "" {
  55. c := NewClient()
  56. c.Send(Command{
  57. Command: command,
  58. Context: context,
  59. })
  60. }
  61. }()
  62. }
  63. func getCommand(args []string, flags *flag.FlagSet) string {
  64. command := ""
  65. args = stripFlags(args, flags)
  66. if len(args) != 0 {
  67. command = args[0]
  68. for {
  69. currentCommand := args[0]
  70. if contains(managementCommands, currentCommand) {
  71. if sub := getSubCommand(args[1:]); sub != "" {
  72. command += " " + sub
  73. args = args[1:]
  74. continue
  75. }
  76. }
  77. break
  78. }
  79. }
  80. return command
  81. }
  82. func getSubCommand(args []string) string {
  83. if len(args) != 0 && isArg(args[0]) {
  84. return args[0]
  85. }
  86. return ""
  87. }
  88. func contains(array []string, needle string) bool {
  89. for _, val := range array {
  90. if val == needle {
  91. return true
  92. }
  93. }
  94. return false
  95. }
  96. func stripFlags(args []string, flags *flag.FlagSet) []string {
  97. commands := []string{}
  98. for len(args) > 0 {
  99. s := args[0]
  100. args = args[1:]
  101. if s == "--" {
  102. return commands
  103. }
  104. if flagArg(s, flags) {
  105. if len(args) <= 1 {
  106. return commands
  107. }
  108. args = args[1:]
  109. }
  110. if isArg(s) {
  111. commands = append(commands, s)
  112. }
  113. }
  114. return commands
  115. }
  116. func flagArg(s string, flags *flag.FlagSet) bool {
  117. return strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags) ||
  118. strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags)
  119. }
  120. func isArg(s string) bool {
  121. return s != "" && !strings.HasPrefix(s, "-")
  122. }
  123. func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
  124. flag := fs.Lookup(name)
  125. if flag == nil {
  126. return false
  127. }
  128. return flag.NoOptDefVal != ""
  129. }
  130. func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
  131. flag := fs.ShorthandLookup(name[:1])
  132. if flag == nil {
  133. return false
  134. }
  135. return flag.NoOptDefVal != ""
  136. }