Add command line flag to use specific config file

This commit is contained in:
Jan-Lukas Else 2021-11-13 15:54:00 +01:00
parent d6ff2f7bd8
commit 2c6cd401bc
4 changed files with 16 additions and 8 deletions

View File

@ -279,10 +279,15 @@ type configWebmention struct {
DisableReceiving bool `mapstructure:"disableReceiving"`
}
func (a *goBlog) initConfig() error {
func (a *goBlog) initConfig(file string) error {
log.Println("Initialize configuration...")
viper.SetConfigName("config")
viper.AddConfigPath("./config/")
if file != "" {
// Use config file from the flag
viper.SetConfigFile(file)
} else {
viper.SetConfigName("config")
viper.AddConfigPath("./config/")
}
err := viper.ReadInConfig()
if err != nil {
return err

2
go.mod
View File

@ -49,7 +49,7 @@ require (
// master
github.com/yuin/goldmark-emoji v1.0.2-0.20210607094911-0487583eca38
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa
golang.org/x/net v0.0.0-20211109214657-ef0fda0de508
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

4
go.sum
View File

@ -593,8 +593,8 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211109214657-ef0fda0de508 h1:v3NKo+t/Kc3EASxaKZ82lwK6mCf4ZeObQBduYFZHo7c=
golang.org/x/net v0.0.0-20211109214657-ef0fda0de508/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

View File

@ -13,9 +13,12 @@ import (
func main() {
var err error
// Init CPU and memory profiling
// Command line flags
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")
configfile := flag.String("config", "", "use a specific config file")
// Init CPU and memory profiling
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
@ -54,7 +57,7 @@ func main() {
}
// Initialize config
if err = app.initConfig(); err != nil {
if err = app.initConfig(*configfile); err != nil {
app.logErrAndQuit("Failed to init config:", err.Error())
return
}