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.
pull/78/head
Matt Baer 6 years ago
parent 20c77989ba
commit 111945bc5d
  1. 2
      admin.go
  2. 16
      app.go
  3. 14
      config/config.go
  4. 15
      config/setup.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()
}

@ -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)

@ -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)
}

@ -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)
}

Loading…
Cancel
Save