diff --git a/config/example.tpl b/cmd/fuckdb/cmd/example.tpl similarity index 100% rename from config/example.tpl rename to cmd/fuckdb/cmd/example.tpl diff --git a/cmd/fuckdb/cmd/server.go b/cmd/fuckdb/cmd/server.go index 035d9ec..b3db2f9 100644 --- a/cmd/fuckdb/cmd/server.go +++ b/cmd/fuckdb/cmd/server.go @@ -35,7 +35,7 @@ var serverCmd = &cobra.Command{ } logrus.Infoln("config init success") - tplFile := viper.GetString("customTmpl") + tplFile := viper.GetString("example.tpl") if tplFile != "" { tmpl, err := template.ParseFiles(tplFile) if err != nil { diff --git a/config/config.go b/config/config.go index a05f549..46ddc75 100644 --- a/config/config.go +++ b/config/config.go @@ -1,13 +1,16 @@ package config import ( - "strings" - + "bytes" + _ "embed" "github.com/fsnotify/fsnotify" "github.com/sirupsen/logrus" "github.com/spf13/viper" ) +//go:embed config.yaml +var configBytes []byte + type Config struct { Name string } @@ -20,20 +23,12 @@ func (c *Config) WatchConfig() { } func (c *Config) Init() error { - if c.Name != "" { - viper.SetConfigName(c.Name) - } else { - viper.AddConfigPath("./config") - viper.SetConfigName("config") - } viper.SetConfigType("yaml") - viper.AutomaticEnv() - viper.SetEnvPrefix("monitor") - replacer := strings.NewReplacer(".", "_") - viper.SetEnvKeyReplacer(replacer) - if err := viper.ReadInConfig(); err != nil { + err := viper.ReadConfig(bytes.NewBufferString(string(configBytes))) + if err != nil { return err } + viper.AutomaticEnv() return nil } diff --git a/config/config.yaml b/config/config.yaml index c090aa4..f4e31bd 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,9 +1,8 @@ -name: DbToGoStruct # API Server的名字 +name: DbToGoStruct # API Server的名字 gormlog: true server: host: 127.0.0.1 port: 8000 -customTmpl: example.tpl genFilePath: ./