From 2c6cd401bc817336d03d34ea82a29fa5e976d788 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sat, 13 Nov 2021 15:54:00 +0100 Subject: [PATCH] Add command line flag to use specific config file --- config.go | 11 ++++++++--- go.mod | 2 +- go.sum | 4 ++-- main.go | 7 +++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index bd6d6ab..f0aff05 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/go.mod b/go.mod index 3915c2a..61ef8bc 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 87d172f..acd8b03 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 4f9816f..7d747bd 100644 --- a/main.go +++ b/main.go @@ -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 }