conf_util.go 3.2 KB

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