path.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 GetLogLoc() (string, error) {
  51. return GetLoc(logDir)
  52. }
  53. func GetDataLoc() (string, error) {
  54. if IsTesting {
  55. dataDir, err := GetLoc(dataDir)
  56. if err != nil {
  57. return "", err
  58. }
  59. d := path.Join(dataDir, "test")
  60. if _, err := os.Stat(d); os.IsNotExist(err) {
  61. err = os.MkdirAll(d, 0o755)
  62. if err != nil {
  63. return "", err
  64. }
  65. }
  66. return d, nil
  67. }
  68. return GetLoc(dataDir)
  69. }
  70. func GetPluginsLoc() (string, error) {
  71. return GetLoc(pluginsDir)
  72. }
  73. func absolutePath(loc string) (dir string, err error) {
  74. for relDir, absoluteDir := range PathConfig.Dirs {
  75. if strings.HasPrefix(loc, relDir) {
  76. dir = strings.Replace(loc, relDir, absoluteDir, 1)
  77. break
  78. }
  79. }
  80. if 0 == len(dir) {
  81. return "", fmt.Errorf("location %s is not allowed for absolute mode", loc)
  82. }
  83. return dir, nil
  84. }
  85. // GetLoc subdir must be a relative path
  86. func GetLoc(subdir string) (string, error) {
  87. if "relative" == PathConfig.LoadFileType {
  88. return relativePath(subdir)
  89. }
  90. if "absolute" == PathConfig.LoadFileType {
  91. return absolutePath(subdir)
  92. }
  93. return "", fmt.Errorf("Unrecognized loading method.")
  94. }
  95. func relativePath(subdir string) (dir string, err error) {
  96. dir, err = os.Getwd()
  97. if err != nil {
  98. return "", err
  99. }
  100. if base := os.Getenv(KuiperBaseKey); base != "" {
  101. Log.Infof("Specified Kuiper base folder at location %s.\n", base)
  102. dir = base
  103. }
  104. confDir := path.Join(dir, subdir)
  105. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  106. lastdir := dir
  107. for len(dir) > 0 {
  108. dir = filepath.Dir(dir)
  109. if lastdir == dir {
  110. break
  111. }
  112. confDir = path.Join(dir, subdir)
  113. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  114. lastdir = dir
  115. continue
  116. }
  117. // Log.Printf("Trying to load file from %s", confDir)
  118. return confDir, nil
  119. }
  120. } else {
  121. // Log.Printf("Trying to load file from %s", confDir)
  122. return confDir, nil
  123. }
  124. return "", fmt.Errorf("dir %s not found, please make sure it is created.", confDir)
  125. }
  126. func ProcessPath(p string) (string, error) {
  127. if abs, err := filepath.Abs(p); err != nil {
  128. return "", nil
  129. } else {
  130. if _, err := os.Stat(abs); os.IsNotExist(err) {
  131. return "", err
  132. }
  133. return abs, nil
  134. }
  135. }