profile.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package mhayaProfile
  2. import (
  3. "path/filepath"
  4. cerror "github.com/mhaya/error"
  5. cfile "github.com/mhaya/extend/file"
  6. cjson "github.com/mhaya/extend/json"
  7. cstring "github.com/mhaya/extend/string"
  8. cfacade "github.com/mhaya/facade"
  9. )
  10. var (
  11. cfg = &struct {
  12. profilePath string // profile root dir
  13. profileName string // profile name
  14. jsonConfig *Config // profile-x.json parse to json object
  15. env string // env name
  16. debug bool // debug default is true
  17. printLevel string // mhaya log print level
  18. }{}
  19. )
  20. func Path() string {
  21. return cfg.profilePath
  22. }
  23. func Name() string {
  24. return cfg.profileName
  25. }
  26. func Env() string {
  27. return cfg.env
  28. }
  29. func Debug() bool {
  30. return cfg.debug
  31. }
  32. func PrintLevel() string {
  33. return cfg.printLevel
  34. }
  35. func Init(filePath, nodeId string) (cfacade.INode, error) {
  36. if filePath == "" {
  37. return nil, cerror.Error("File path is nil.")
  38. }
  39. if nodeId == "" {
  40. return nil, cerror.Error("NodeId is nil.")
  41. }
  42. judgePath, ok := cfile.JudgeFile(filePath)
  43. if !ok {
  44. return nil, cerror.Errorf("File path error. filePath = %s", filePath)
  45. }
  46. p, f := filepath.Split(judgePath)
  47. jsonConfig, err := loadFile(p, f)
  48. if err != nil || jsonConfig.Any == nil || jsonConfig.LastError() != nil {
  49. return nil, cerror.Errorf("Load profile file error. [err = %v]", err)
  50. }
  51. node, err := GetNodeWithConfig(jsonConfig, nodeId)
  52. if err != nil {
  53. return nil, cerror.Errorf("Failed to get node config from profile file. [err = %v]", err)
  54. }
  55. // init cfg
  56. cfg.profilePath = p
  57. cfg.profileName = f
  58. cfg.jsonConfig = jsonConfig
  59. cfg.env = jsonConfig.GetString("env", "default")
  60. cfg.debug = jsonConfig.GetBool("debug", true)
  61. cfg.printLevel = jsonConfig.GetString("print_level", "debug")
  62. return node, nil
  63. }
  64. func GetConfig(path ...interface{}) cfacade.ProfileJSON {
  65. return cfg.jsonConfig.GetConfig(path...)
  66. }
  67. func loadFile(filePath, fileName string) (*Config, error) {
  68. // merge include json file
  69. var maps = make(map[string]interface{})
  70. // read master json file
  71. fileNamePath := filepath.Join(filePath, fileName)
  72. if err := cjson.ReadMaps(fileNamePath, maps); err != nil {
  73. return nil, err
  74. }
  75. // read include json file
  76. if v, found := maps["include"].([]interface{}); found {
  77. paths := cstring.ToStringSlice(v)
  78. for _, p := range paths {
  79. includePath := filepath.Join(filePath, p)
  80. if err := cjson.ReadMaps(includePath, maps); err != nil {
  81. return nil, err
  82. }
  83. }
  84. }
  85. return Wrap(maps), nil
  86. }
  87. //func judgeNameList(path, name string) ([]string, error) {
  88. // var list []string
  89. //
  90. // if name != "" {
  91. // fileName := mergeProfileName(name)
  92. // list = append(list, fileName)
  93. //
  94. // } else {
  95. // // find path
  96. // filesPath, err := cfile.ReadDir(path, "profile-", ".json")
  97. // if err != nil {
  98. // return nil, err
  99. // }
  100. //
  101. // if len(filesPath) < 1 {
  102. // return nil, cerror.Errorf("[path = %s] profile file not found.", path)
  103. // }
  104. //
  105. // for _, fp := range filesPath {
  106. // list = append(list, fp)
  107. // }
  108. // }
  109. //
  110. // return list, nil
  111. //}
  112. //func mergeProfileName(name string) string {
  113. // return fmt.Sprintf("%s%s%s", profilePrefix, name, profileSuffix)
  114. //}