metrics.go 3.1 KB

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