config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package config
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "gopkg.in/yaml.v2"
  10. )
  11. const (
  12. configFile = "config.yaml"
  13. configFolder = "config"
  14. DefaultFormat = "gozero"
  15. )
  16. const defaultYaml = `# namingFormat is used to define the naming format of the generated file name.
  17. # just like time formatting, you can specify the formatting style through the
  18. # two format characters go, and zero. for example: snake format you can
  19. # define as go_zero, camel case format you can it is defined as goZero,
  20. # and even split characters can be specified, such as go#zero. in theory,
  21. # any combination can be used, but the prerequisite must meet the naming conventions
  22. # of each operating system file name. if you want to independently control the file
  23. # naming style of the api, rpc, and model layers, you can set it through apiNamingFormat,
  24. # rpcNamingFormat, modelNamingFormat, and independent control is not enabled by default.
  25. # for more information, please see #{apiNamingFormat},#{rpcNamingFormat},#{modelNamingFormat}
  26. # Note: namingFormat is based on snake or camel string
  27. namingFormat: gozero
  28. `
  29. type Config struct {
  30. // NamingFormat is used to define the naming format of the generated file name.
  31. // just like time formatting, you can specify the formatting style through the
  32. // two format characters go, and zero. for example: snake format you can
  33. // define as go_zero, camel case format you can it is defined as goZero,
  34. // and even split characters can be specified, such as go#zero. in theory,
  35. // any combination can be used, but the prerequisite must meet the naming conventions
  36. // of each operating system file name.
  37. // Note: NamingFormat is based on snake or camel string
  38. NamingFormat string `yaml:"namingFormat"`
  39. }
  40. func NewConfig(format string) (*Config, error) {
  41. if len(format) == 0 {
  42. format = DefaultFormat
  43. }
  44. cfg := &Config{NamingFormat: format}
  45. err := validate(cfg)
  46. return cfg, err
  47. }
  48. func InitOrGetConfig() (*Config, error) {
  49. var (
  50. defaultConfig Config
  51. )
  52. err := yaml.Unmarshal([]byte(defaultYaml), &defaultConfig)
  53. if err != nil {
  54. return nil, err
  55. }
  56. goctlHome, err := util.GetGoctlHome()
  57. if err != nil {
  58. return nil, err
  59. }
  60. configDir := filepath.Join(goctlHome, configFolder)
  61. configFilename := filepath.Join(configDir, configFile)
  62. if util.FileExists(configFilename) {
  63. data, err := ioutil.ReadFile(configFilename)
  64. if err != nil {
  65. return nil, err
  66. }
  67. err = yaml.Unmarshal(data, &defaultConfig)
  68. if err != nil {
  69. return nil, err
  70. }
  71. err = validate(&defaultConfig)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &defaultConfig, nil
  76. }
  77. err = util.MkdirIfNotExist(configDir)
  78. if err != nil {
  79. return nil, err
  80. }
  81. f, err := os.Create(configFilename)
  82. if err != nil {
  83. return nil, err
  84. }
  85. defer func() {
  86. _ = f.Close()
  87. }()
  88. _, err = f.WriteString(defaultYaml)
  89. if err != nil {
  90. return nil, err
  91. }
  92. err = validate(&defaultConfig)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &defaultConfig, nil
  97. }
  98. func validate(cfg *Config) error {
  99. if len(strings.TrimSpace(cfg.NamingFormat)) == 0 {
  100. return errors.New("missing namingFormat")
  101. }
  102. return nil
  103. }