From 111945bc5d8df3eccc9da62920e83017f64f2eae Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 8 Dec 2018 17:49:19 -0500 Subject: [PATCH] Add new -c [filename] option for supplying config file This allows users to load a different configuration file instead of the default config.ini. It works in combination with other configuration actions, like --config and --create-config. --- admin.go | 2 +- app.go | 16 ++++++++++------ config/config.go | 14 ++++++++++---- config/setup.go | 15 +++++++++------ 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/admin.go b/admin.go index 07974b6..508ac59 100644 --- a/admin.go +++ b/admin.go @@ -128,7 +128,7 @@ func handleAdminUpdateConfig(app *app, u *User, w http.ResponseWriter, r *http.R app.cfg.App.Private = r.FormValue("private") == "on" m := "?cm=Configuration+saved." - err = config.Save(app.cfg) + err = config.Save(app.cfg, app.cfgFile) if err != nil { m = "?cm=" + err.Error() } diff --git a/app.go b/app.go index feb5dd7..8d5fd4d 100644 --- a/app.go +++ b/app.go @@ -56,6 +56,7 @@ type app struct { router *mux.Router db *datastore cfg *config.Config + cfgFile string keys *keychain sessionStore *sessions.CookieStore formDecoder *schema.Decoder @@ -183,12 +184,15 @@ func Serve() { createSchema := flag.Bool("init-db", false, "Initialize app database") createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password") resetPassUser := flag.String("reset-pass", "", "Reset the given user's password") + configFile := flag.String("c", "config.ini", "The configuration file to use") outputVersion := flag.Bool("v", false, "Output the current version") flag.Parse() debugging = *debugPtr - app := &app{} + app := &app{ + cfgFile: *configFile, + } if *outputVersion { fmt.Println(serverSoftware + " " + softwareVer) @@ -196,15 +200,15 @@ func Serve() { } else if *createConfig { log.Info("Creating configuration...") c := config.New() - log.Info("Saving configuration...") - err := config.Save(c) + log.Info("Saving configuration %s...", app.cfgFile) + err := config.Save(c, app.cfgFile) if err != nil { log.Error("Unable to save configuration: %v", err) os.Exit(1) } os.Exit(0) } else if *doConfig { - d, err := config.Configure() + d, err := config.Configure(app.cfgFile) if err != nil { log.Error("Unable to configure: %v", err) os.Exit(1) @@ -468,8 +472,8 @@ func Serve() { } func loadConfig(app *app) { - log.Info("Loading configuration...") - cfg, err := config.Load() + log.Info("Loading %s configuration...", app.cfgFile) + cfg, err := config.Load(app.cfgFile) if err != nil { log.Error("Unable to load configuration: %v", err) os.Exit(1) diff --git a/config/config.go b/config/config.go index ab7cea3..7b4c3e8 100644 --- a/config/config.go +++ b/config/config.go @@ -101,8 +101,11 @@ func (cfg *Config) IsSecureStandalone() bool { return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != "" } -func Load() (*Config, error) { - cfg, err := ini.Load(FileName) +func Load(fname string) (*Config, error) { + if fname == "" { + fname = FileName + } + cfg, err := ini.Load(fname) if err != nil { return nil, err } @@ -116,12 +119,15 @@ func Load() (*Config, error) { return uc, nil } -func Save(uc *Config) error { +func Save(uc *Config, fname string) error { cfg := ini.Empty() err := ini.ReflectFrom(cfg, uc) if err != nil { return err } - return cfg.SaveTo(FileName) + if fname == "" { + fname = FileName + } + return cfg.SaveTo(fname) } diff --git a/config/setup.go b/config/setup.go index d086500..cb73d80 100644 --- a/config/setup.go +++ b/config/setup.go @@ -14,20 +14,23 @@ type SetupData struct { Config *Config } -func Configure() (*SetupData, error) { +func Configure(fname string) (*SetupData, error) { data := &SetupData{} var err error + if fname == "" { + fname = FileName + } - data.Config, err = Load() + data.Config, err = Load(fname) var action string isNewCfg := false if err != nil { - fmt.Println("No configuration yet. Creating new.") + fmt.Printf("No %s configuration yet. Creating new.\n", fname) data.Config = New() action = "generate" isNewCfg = true } else { - fmt.Println("Configuration loaded.") + fmt.Printf("Loaded configuration %s.\n", fname) action = "update" } title := color.New(color.Bold, color.BgGreen).PrintFunc() @@ -36,7 +39,7 @@ func Configure() (*SetupData, error) { fmt.Println() intro(" ✍ Write Freely Configuration ✍") fmt.Println() - fmt.Println(wordwrap.WrapString(" This quick configuration process will "+action+" the application's config file, "+FileName+".\n\n It validates your input along the way, so you can be sure any future errors aren't caused by a bad configuration. If you'd rather configure your server manually, instead run: writefreely --create-config and edit that file.", 75)) + fmt.Println(wordwrap.WrapString(" This quick configuration process will "+action+" the application's config file, "+fname+".\n\n It validates your input along the way, so you can be sure any future errors aren't caused by a bad configuration. If you'd rather configure your server manually, instead run: writefreely --create-config and edit that file.", 75)) fmt.Println() title(" Server setup ") @@ -345,5 +348,5 @@ func Configure() (*SetupData, error) { data.Config.App.Private = fedStatsType == "Private" } - return data, Save(data.Config) + return data, Save(data.Config, fname) }