path.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2022-present Datadog, Inc.
  5. package state
  6. import (
  7. "fmt"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. )
  12. var (
  13. // matches datadog/<int>/<string>/<string>/<string> for datadog/<org_id>/<product>/<config_id>/<file>
  14. datadogPathRegexp = regexp.MustCompile(`^datadog/(\d+)/([^/]+)/([^/]+)/([^/]+)$`)
  15. datadogPathRegexpGroups = 4
  16. // matches employee/<string>/<string>/<string> for employee/<org_id>/<product>/<config_id>/<file>
  17. employeePathRegexp = regexp.MustCompile(`^employee/([^/]+)/([^/]+)/([^/]+)$`)
  18. employeePathRegexpGroups = 3
  19. )
  20. type source uint
  21. const (
  22. sourceUnknown source = iota
  23. sourceDatadog
  24. sourceEmployee
  25. )
  26. type configPath struct {
  27. Source source
  28. OrgID int64
  29. Product string
  30. ConfigID string
  31. Name string
  32. }
  33. func parseConfigPath(path string) (configPath, error) {
  34. configType := parseConfigPathSource(path)
  35. switch configType {
  36. case sourceDatadog:
  37. return parseDatadogConfigPath(path)
  38. case sourceEmployee:
  39. return parseEmployeeConfigPath(path)
  40. }
  41. return configPath{}, fmt.Errorf("config path '%s' has unknown source", path)
  42. }
  43. func parseDatadogConfigPath(path string) (configPath, error) {
  44. matchedGroups := datadogPathRegexp.FindStringSubmatch(path)
  45. if len(matchedGroups) != datadogPathRegexpGroups+1 {
  46. return configPath{}, fmt.Errorf("config file path '%s' has wrong format", path)
  47. }
  48. rawOrgID := matchedGroups[1]
  49. orgID, err := strconv.ParseInt(rawOrgID, 10, 64)
  50. if err != nil {
  51. return configPath{}, fmt.Errorf("could not parse orgID '%s' in config file path: %v", rawOrgID, err)
  52. }
  53. rawProduct := matchedGroups[2]
  54. if len(rawProduct) == 0 {
  55. return configPath{}, fmt.Errorf("product is empty")
  56. }
  57. return configPath{
  58. Source: sourceDatadog,
  59. OrgID: orgID,
  60. Product: rawProduct,
  61. ConfigID: matchedGroups[3],
  62. Name: matchedGroups[4],
  63. }, nil
  64. }
  65. func parseEmployeeConfigPath(path string) (configPath, error) {
  66. matchedGroups := employeePathRegexp.FindStringSubmatch(path)
  67. if len(matchedGroups) != employeePathRegexpGroups+1 {
  68. return configPath{}, fmt.Errorf("config file path '%s' has wrong format", path)
  69. }
  70. rawProduct := matchedGroups[1]
  71. if len(rawProduct) == 0 {
  72. return configPath{}, fmt.Errorf("product is empty")
  73. }
  74. return configPath{
  75. Source: sourceEmployee,
  76. Product: rawProduct,
  77. ConfigID: matchedGroups[2],
  78. Name: matchedGroups[3],
  79. }, nil
  80. }
  81. func parseConfigPathSource(path string) source {
  82. switch {
  83. case strings.HasPrefix(path, "datadog/"):
  84. return sourceDatadog
  85. case strings.HasPrefix(path, "employee/"):
  86. return sourceEmployee
  87. }
  88. return sourceUnknown
  89. }