source.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2022 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. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. "strings"
  19. )
  20. func GetSourceConf(sourceType string, options *ast.Options) map[string]interface{} {
  21. confkey := options.CONF_KEY
  22. confPath := "sources/" + sourceType + ".yaml"
  23. if sourceType == "mqtt" {
  24. confPath = "mqtt_source.yaml"
  25. }
  26. props := make(map[string]interface{})
  27. cfg := make(map[string]interface{})
  28. err := conf.LoadConfigByName(confPath, &cfg)
  29. if err != nil {
  30. conf.Log.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  31. } else {
  32. def, ok := cfg["default"]
  33. if !ok {
  34. conf.Log.Warnf("default conf %s is not found", confkey)
  35. } else {
  36. if def1, ok1 := def.(map[string]interface{}); ok1 {
  37. props = def1
  38. }
  39. if c, ok := cfg[strings.ToLower(confkey)]; ok {
  40. if c1, ok := c.(map[string]interface{}); ok {
  41. c2 := c1
  42. for k, v := range c2 {
  43. props[k] = v
  44. }
  45. }
  46. }
  47. }
  48. }
  49. f := options.FORMAT
  50. if f == "" {
  51. f = "json"
  52. }
  53. props["format"] = strings.ToLower(f)
  54. props["key"] = strings.ToLower(options.KEY)
  55. conf.Log.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, printable(props))
  56. return props
  57. }
  58. func printable(m map[string]interface{}) map[string]interface{} {
  59. printableMap := make(map[string]interface{})
  60. for k, v := range m {
  61. if strings.EqualFold(k, "password") {
  62. printableMap[k] = "*"
  63. } else {
  64. if vm, ok := v.(map[string]interface{}); ok {
  65. printableMap[k] = printable(vm)
  66. } else {
  67. printableMap[k] = v
  68. }
  69. }
  70. }
  71. return printableMap
  72. }