path.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. func init() {
  23. PathConfig.LoadFileType = "relative"
  24. PathConfig.Dirs = AbsoluteMapping
  25. }
  26. type PathConfigure struct {
  27. LoadFileType string
  28. Dirs map[string]string
  29. }
  30. const (
  31. etcDir = "etc"
  32. dataDir = "data"
  33. logDir = "log"
  34. pluginsDir = "plugins"
  35. KuiperBaseKey = "KuiperBaseKey"
  36. KuiperSyslogKey = "KuiperSyslogKey"
  37. )
  38. var (
  39. PathConfig PathConfigure
  40. AbsoluteMapping = map[string]string{
  41. etcDir: "/etc/kuiper",
  42. dataDir: "/var/lib/kuiper/data",
  43. logDir: "/var/log/kuiper",
  44. pluginsDir: "/var/lib/kuiper/plugins",
  45. }
  46. )
  47. func GetConfLoc() (string, error) {
  48. return GetLoc(etcDir)
  49. }
  50. func GetDataLoc() (string, error) {
  51. if IsTesting {
  52. dataDir, err := GetLoc(dataDir)
  53. if err != nil {
  54. return "", err
  55. }
  56. d := path.Join(dataDir, "test")
  57. if _, err := os.Stat(d); os.IsNotExist(err) {
  58. err = os.MkdirAll(d, 0o755)
  59. if err != nil {
  60. return "", err
  61. }
  62. }
  63. return d, nil
  64. }
  65. return GetLoc(dataDir)
  66. }
  67. func GetPluginsLoc() (string, error) {
  68. return GetLoc(pluginsDir)
  69. }
  70. func absolutePath(loc string) (dir string, err error) {
  71. for relDir, absoluteDir := range PathConfig.Dirs {
  72. if strings.HasPrefix(loc, relDir) {
  73. dir = strings.Replace(loc, relDir, absoluteDir, 1)
  74. break
  75. }
  76. }
  77. if 0 == len(dir) {
  78. return "", fmt.Errorf("location %s is not allowed for absolute mode", loc)
  79. }
  80. return dir, nil
  81. }
  82. // GetLoc subdir must be a relative path
  83. func GetLoc(subdir string) (string, error) {
  84. if "relative" == PathConfig.LoadFileType {
  85. return relativePath(subdir)
  86. }
  87. if "absolute" == PathConfig.LoadFileType {
  88. return absolutePath(subdir)
  89. }
  90. return "", fmt.Errorf("Unrecognized loading method.")
  91. }
  92. func relativePath(subdir string) (dir string, err error) {
  93. dir, err = os.Getwd()
  94. if err != nil {
  95. return "", err
  96. }
  97. if base := os.Getenv(KuiperBaseKey); base != "" {
  98. Log.Infof("Specified Kuiper base folder at location %s.\n", base)
  99. dir = base
  100. }
  101. confDir := path.Join(dir, subdir)
  102. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  103. lastdir := dir
  104. for len(dir) > 0 {
  105. dir = filepath.Dir(dir)
  106. if lastdir == dir {
  107. break
  108. }
  109. confDir = path.Join(dir, subdir)
  110. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  111. lastdir = dir
  112. continue
  113. }
  114. // Log.Printf("Trying to load file from %s", confDir)
  115. return confDir, nil
  116. }
  117. } else {
  118. // Log.Printf("Trying to load file from %s", confDir)
  119. return confDir, nil
  120. }
  121. return "", fmt.Errorf("dir %s not found, please make sure it is created.", confDir)
  122. }
  123. func ProcessPath(p string) (string, error) {
  124. if abs, err := filepath.Abs(p); err != nil {
  125. return "", nil
  126. } else {
  127. if _, err := os.Stat(abs); os.IsNotExist(err) {
  128. return "", err
  129. }
  130. return abs, nil
  131. }
  132. }