metrics.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. "github.com/docker/compose-cli/utils"
  17. )
  18. // Track sends the tracking analytics to Docker Desktop
  19. func Track(context string, args []string, status string) {
  20. command := GetCommand(args)
  21. if command != "" {
  22. c := NewClient()
  23. c.Send(Command{
  24. Command: command,
  25. Context: context,
  26. Source: CLISource,
  27. Status: status,
  28. })
  29. }
  30. }
  31. func isCommand(word string) bool {
  32. return utils.StringContains(commands, word) || isManagementCommand(word)
  33. }
  34. func isManagementCommand(word string) bool {
  35. return utils.StringContains(managementCommands, word)
  36. }
  37. func isCommandFlag(word string) bool {
  38. return utils.StringContains(commandFlags, word)
  39. }
  40. // GetCommand get the invoked command
  41. func GetCommand(args []string) string {
  42. result := ""
  43. onlyFlags := false
  44. for _, arg := range args {
  45. if arg == "--help" {
  46. result = strings.TrimSpace(arg + " " + result)
  47. continue
  48. }
  49. if arg == "--" {
  50. break
  51. }
  52. if isCommandFlag(arg) || (!onlyFlags && isCommand(arg)) {
  53. result = strings.TrimSpace(result + " " + arg)
  54. if isCommand(arg) && !isManagementCommand(arg) {
  55. onlyFlags = true
  56. }
  57. }
  58. }
  59. return result
  60. }