metrics.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "cluster",
  24. "compose",
  25. "config",
  26. "container",
  27. "context",
  28. "help",
  29. "image",
  30. "manifest",
  31. "network",
  32. "node",
  33. "plugin",
  34. "registry",
  35. "secret",
  36. "service",
  37. "stack",
  38. "swarm",
  39. "system",
  40. "template",
  41. "trust",
  42. "volume",
  43. }
  44. // Track sends the tracking analytics to Docker Desktop
  45. func Track(context string, args []string, flags *flag.FlagSet) {
  46. // Fire and forget, we don't want to slow down the user waiting for DD
  47. // metrics endpoint to respond. We could lose some events but that's ok.
  48. go func() {
  49. defer func() {
  50. _ = recover()
  51. }()
  52. command := getCommand(args, flags)
  53. if command != "" {
  54. c := NewClient()
  55. c.Send(Command{
  56. Command: command,
  57. Context: context,
  58. })
  59. }
  60. }()
  61. }
  62. func getCommand(args []string, flags *flag.FlagSet) string {
  63. command := ""
  64. args = stripFlags(args, flags)
  65. if len(args) != 0 {
  66. command = args[0]
  67. if contains(managementCommands, command) {
  68. if sub := getSubCommand(args[1:]); sub != "" {
  69. return command + " " + sub
  70. }
  71. }
  72. }
  73. return command
  74. }
  75. func getSubCommand(args []string) string {
  76. if len(args) != 0 && isArg(args[0]) {
  77. return args[0]
  78. }
  79. return ""
  80. }
  81. func contains(array []string, needle string) bool {
  82. for _, val := range array {
  83. if val == needle {
  84. return true
  85. }
  86. }
  87. return false
  88. }
  89. func stripFlags(args []string, flags *flag.FlagSet) []string {
  90. commands := []string{}
  91. for len(args) > 0 {
  92. s := args[0]
  93. args = args[1:]
  94. if s == "--" {
  95. return commands
  96. }
  97. if flagArg(s, flags) {
  98. if len(args) <= 1 {
  99. return commands
  100. }
  101. args = args[1:]
  102. }
  103. if isArg(s) {
  104. commands = append(commands, s)
  105. }
  106. }
  107. return commands
  108. }
  109. func flagArg(s string, flags *flag.FlagSet) bool {
  110. return strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags) ||
  111. strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags)
  112. }
  113. func isArg(s string) bool {
  114. return s != "" && !strings.HasPrefix(s, "-")
  115. }
  116. func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
  117. flag := fs.Lookup(name)
  118. if flag == nil {
  119. return false
  120. }
  121. return flag.NoOptDefVal != ""
  122. }
  123. func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
  124. flag := fs.ShorthandLookup(name[:1])
  125. if flag == nil {
  126. return false
  127. }
  128. return flag.NoOptDefVal != ""
  129. }