From 017abef8f2352c4cb1a811e49f00c9e9fcba9d1c Mon Sep 17 00:00:00 2001 From: hantmac Date: Mon, 31 May 2021 00:44:10 +0800 Subject: [PATCH 1/2] add config.yaml to embed Signed-off-by: hantmac --- config/{ => conf}/config.yaml | 2 +- config/config.go | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) rename config/{ => conf}/config.yaml (78%) diff --git a/config/config.yaml b/config/conf/config.yaml similarity index 78% rename from config/config.yaml rename to config/conf/config.yaml index c090aa4..b78cfb3 100644 --- a/config/config.yaml +++ b/config/conf/config.yaml @@ -4,6 +4,6 @@ server: host: 127.0.0.1 port: 8000 -customTmpl: example.tpl +customTmpl: ./config/example.tpl genFilePath: ./ diff --git a/config/config.go b/config/config.go index a05f549..6e79dbd 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 conf +var configBytes embed.FS + type Config struct { Name string } @@ -20,20 +23,13 @@ func (c *Config) WatchConfig() { } func (c *Config) Init() error { - if c.Name != "" { - viper.SetConfigName(c.Name) - } else { - viper.AddConfigPath("./config") - viper.SetConfigName("config") - } + by, err := configBytes.ReadFile("conf/config.yaml") 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(by))) + if err != nil { return err } + viper.AutomaticEnv() return nil } From 7025e44d02e636bcff9400e464696a5ed92ea11c Mon Sep 17 00:00:00 2001 From: hantmac Date: Mon, 31 May 2021 10:40:52 +0800 Subject: [PATCH 2/2] add config file to embed Signed-off-by: hantmac --- {config => cmd/fuckdb/cmd}/example.tpl | 0 cmd/fuckdb/cmd/server.go | 2 +- config/conf/config.yaml | 9 --------- config/config.go | 9 ++++----- config/config.yaml | 8 ++++++++ 5 files changed, 13 insertions(+), 15 deletions(-) rename {config => cmd/fuckdb/cmd}/example.tpl (100%) delete mode 100644 config/conf/config.yaml create mode 100644 config/config.yaml 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/conf/config.yaml b/config/conf/config.yaml deleted file mode 100644 index b78cfb3..0000000 --- a/config/conf/config.yaml +++ /dev/null @@ -1,9 +0,0 @@ -name: DbToGoStruct # API Server的名字 -gormlog: true -server: - host: 127.0.0.1 - port: 8000 - -customTmpl: ./config/example.tpl - -genFilePath: ./ diff --git a/config/config.go b/config/config.go index 6e79dbd..46ddc75 100644 --- a/config/config.go +++ b/config/config.go @@ -2,14 +2,14 @@ package config import ( "bytes" - "embed" + _ "embed" "github.com/fsnotify/fsnotify" "github.com/sirupsen/logrus" "github.com/spf13/viper" ) -//go:embed conf -var configBytes embed.FS +//go:embed config.yaml +var configBytes []byte type Config struct { Name string @@ -23,9 +23,8 @@ func (c *Config) WatchConfig() { } func (c *Config) Init() error { - by, err := configBytes.ReadFile("conf/config.yaml") viper.SetConfigType("yaml") - err = viper.ReadConfig(bytes.NewBufferString(string(by))) + err := viper.ReadConfig(bytes.NewBufferString(string(configBytes))) if err != nil { return err } diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 0000000..f4e31bd --- /dev/null +++ b/config/config.yaml @@ -0,0 +1,8 @@ +name: DbToGoStruct # API Server的名字 +gormlog: true +server: + host: 127.0.0.1 + port: 8000 + + +genFilePath: ./