conf_util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-yaml/yaml"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. )
  9. var fileMap = map[string]string{
  10. "edgex": "/go/kuiper/etc/sources/edgex.yaml",
  11. "mqtt_source": "/go/kuiper/etc/mqtt_source.yaml",
  12. "kuiper": "/go/kuiper/etc/kuiper.yaml",
  13. }
  14. var file_keys_map = map[string]map[string]string{
  15. "edgex": {
  16. "CLIENTID": "ClientId",
  17. "USERNAME": "Username",
  18. "PASSWORD": "Password",
  19. "QOS": "Qos",
  20. "KEEPALIVE": "KeepAlive",
  21. "RETAINED": "Retained",
  22. "CONNECTIONPAYLOAD": "ConnectionPayload",
  23. "CERTFILE": "CertFile",
  24. "KEYFILE": "KeyFile",
  25. "CERTPEMBLOCK": "CertPEMBlock",
  26. "KEYPEMBLOCK": "KeyPEMBlock",
  27. "SKIPCERTVERIFY": "SkipCertVerify",
  28. },
  29. "mqtt_source": {
  30. "SHAREDSUBSCRIPTION": "sharedSubscription",
  31. "CERTIFICATIONPATH": "certificationPath",
  32. "PRIVATEKEYPATH": "privateKeyPath",
  33. },
  34. "kuiper": {
  35. "CONSOLELOG": "consoleLog",
  36. "FILELOG": "fileLog",
  37. "RESTPORT": "restPort",
  38. "PROMETHEUSPORT": "prometheusPort",
  39. },
  40. }
  41. func fileExists(filename string) bool {
  42. info, err := os.Stat(filename)
  43. if os.IsNotExist(err) {
  44. return false
  45. }
  46. return !info.IsDir()
  47. }
  48. func deleteFile(path string) {
  49. // delete file
  50. var err = os.Remove(path)
  51. if err != nil {
  52. return
  53. }
  54. fmt.Println("File Deleted")
  55. }
  56. func main() {
  57. files := make(map[string]map[interface{}]interface{})
  58. ProcessEnv(files, os.Environ())
  59. for f, v := range files {
  60. if bs, err := yaml.Marshal(v); err != nil {
  61. fmt.Println(err)
  62. } else {
  63. message := fmt.Sprintf("-------------------\nConf file %s: \n %s", f, string(bs))
  64. fmt.Println(message)
  65. if fname, ok := fileMap[f]; ok {
  66. if fileExists(fname) {
  67. deleteFile(fname)
  68. }
  69. if e := ioutil.WriteFile(fname, bs, 0644); e != nil {
  70. fmt.Println(e)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. func ProcessEnv(files map[string]map[interface{}]interface{}, vars []string) {
  77. for _, e := range vars {
  78. pair := strings.SplitN(e, "=", 2)
  79. if len(pair) != 2 {
  80. fmt.Printf("invalid env %s, skip it.\n", e)
  81. continue
  82. }
  83. valid := false
  84. for k, _ := range fileMap {
  85. if strings.HasPrefix(pair[0], strings.ToUpper(k)) {
  86. valid = true
  87. break
  88. }
  89. }
  90. if !valid {
  91. continue
  92. } else {
  93. fmt.Printf("Find env: %s, start to handle it.\n", e)
  94. }
  95. env_v := strings.ReplaceAll(pair[0], "__", ".")
  96. keys := strings.Split(env_v, ".")
  97. for i, v := range keys {
  98. keys[i] = v
  99. }
  100. if len(keys) < 2 {
  101. fmt.Printf("not concerned env %s, skip it.\n", e)
  102. continue
  103. } else {
  104. k := strings.ToLower(keys[0])
  105. if v, ok := files[k]; !ok {
  106. if data, err := ioutil.ReadFile(fileMap[k]); err != nil {
  107. fmt.Printf("%s\n", err)
  108. } else {
  109. m := make(map[interface{}]interface{})
  110. err = yaml.Unmarshal([]byte(data), &m)
  111. if err != nil {
  112. fmt.Println(err)
  113. }
  114. files[k] = m
  115. Handle(k, m, keys[1:], pair[1])
  116. }
  117. } else {
  118. Handle(k, v, keys[1:], pair[1])
  119. }
  120. }
  121. }
  122. }
  123. func Handle(file string, conf map[interface{}]interface{}, skeys []string, val string) {
  124. key := getKey(file, skeys[0])
  125. if len(skeys) == 1 {
  126. conf[key] = val
  127. } else if len(skeys) >= 2 {
  128. if v, ok := conf[key]; ok {
  129. if v1, ok1 := v.(map[interface{}]interface{}); ok1 {
  130. Handle(file, v1, skeys[1:], val)
  131. } else {
  132. fmt.Printf("Not expected map: %v\n", v)
  133. }
  134. } else {
  135. v1 := make(map[interface{}]interface{})
  136. conf[key] = v1
  137. Handle(file, v1, skeys[1:], val)
  138. }
  139. }
  140. }
  141. func getKey(file string, key string) string{
  142. if m, ok := file_keys_map[file][key]; ok {
  143. return m
  144. } else {
  145. return strings.ToLower(key)
  146. }
  147. }