path.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "fmt"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. )
  22. const (
  23. etcDir = "etc"
  24. dataDir = "data"
  25. logDir = "log"
  26. pluginsDir = "plugins"
  27. KuiperBaseKey = "KuiperBaseKey"
  28. KuiperSyslogKey = "KuiperSyslogKey"
  29. )
  30. var (
  31. LoadFileType = "relative"
  32. AbsoluteMapping = map[string]string{
  33. etcDir: "/etc/kuiper",
  34. dataDir: "/var/lib/kuiper/data",
  35. logDir: "/var/log/kuiper",
  36. pluginsDir: "/var/lib/kuiper/plugins",
  37. }
  38. )
  39. func GetConfLoc() (string, error) {
  40. return GetLoc(etcDir)
  41. }
  42. func GetDataLoc() (string, error) {
  43. if IsTesting {
  44. dataDir, err := GetLoc(dataDir)
  45. if err != nil {
  46. return "", err
  47. }
  48. d := path.Join(dataDir, "test")
  49. if _, err := os.Stat(d); os.IsNotExist(err) {
  50. err = os.MkdirAll(d, 0o755)
  51. if err != nil {
  52. return "", err
  53. }
  54. }
  55. return d, nil
  56. }
  57. return GetLoc(dataDir)
  58. }
  59. func GetPluginsLoc() (string, error) {
  60. return GetLoc(pluginsDir)
  61. }
  62. func absolutePath(loc string) (dir string, err error) {
  63. for relDir, absoluteDir := range AbsoluteMapping {
  64. if strings.HasPrefix(loc, relDir) {
  65. dir = strings.Replace(loc, relDir, absoluteDir, 1)
  66. break
  67. }
  68. }
  69. if 0 == len(dir) {
  70. return "", fmt.Errorf("location %s is not allowed for absolute mode", loc)
  71. }
  72. return dir, nil
  73. }
  74. // GetLoc subdir must be a relative path
  75. func GetLoc(subdir string) (string, error) {
  76. if "relative" == LoadFileType {
  77. return relativePath(subdir)
  78. }
  79. if "absolute" == LoadFileType {
  80. return absolutePath(subdir)
  81. }
  82. return "", fmt.Errorf("Unrecognized loading method.")
  83. }
  84. func relativePath(subdir string) (dir string, err error) {
  85. dir, err = os.Getwd()
  86. if err != nil {
  87. return "", err
  88. }
  89. if base := os.Getenv(KuiperBaseKey); base != "" {
  90. Log.Infof("Specified Kuiper base folder at location %s.\n", base)
  91. dir = base
  92. }
  93. confDir := path.Join(dir, subdir)
  94. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  95. lastdir := dir
  96. for len(dir) > 0 {
  97. dir = filepath.Dir(dir)
  98. if lastdir == dir {
  99. break
  100. }
  101. confDir = path.Join(dir, subdir)
  102. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  103. lastdir = dir
  104. continue
  105. }
  106. // Log.Printf("Trying to load file from %s", confDir)
  107. return confDir, nil
  108. }
  109. } else {
  110. // Log.Printf("Trying to load file from %s", confDir)
  111. return confDir, nil
  112. }
  113. return "", fmt.Errorf("dir %s not found, please make sure it is created.", confDir)
  114. }
  115. func ProcessPath(p string) (string, error) {
  116. if abs, err := filepath.Abs(p); err != nil {
  117. return "", nil
  118. } else {
  119. if _, err := os.Stat(abs); os.IsNotExist(err) {
  120. return "", err
  121. }
  122. return abs, nil
  123. }
  124. }