Merge branch 'develop'

This commit is contained in:
Matt Baer 2018-12-10 14:22:53 -05:00
commit 7828bf6ba2
4 changed files with 30 additions and 17 deletions

View file

@ -128,7 +128,7 @@ func handleAdminUpdateConfig(app *app, u *User, w http.ResponseWriter, r *http.R
app.cfg.App.Private = r.FormValue("private") == "on" app.cfg.App.Private = r.FormValue("private") == "on"
m := "?cm=Configuration+saved." m := "?cm=Configuration+saved."
err = config.Save(app.cfg) err = config.Save(app.cfg, app.cfgFile)
if err != nil { if err != nil {
m = "?cm=" + err.Error() m = "?cm=" + err.Error()
} }

16
app.go
View file

@ -56,6 +56,7 @@ type app struct {
router *mux.Router router *mux.Router
db *datastore db *datastore
cfg *config.Config cfg *config.Config
cfgFile string
keys *keychain keys *keychain
sessionStore *sessions.CookieStore sessionStore *sessions.CookieStore
formDecoder *schema.Decoder formDecoder *schema.Decoder
@ -183,12 +184,15 @@ func Serve() {
createSchema := flag.Bool("init-db", false, "Initialize app database") createSchema := flag.Bool("init-db", false, "Initialize app database")
createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password") createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password")
resetPassUser := flag.String("reset-pass", "", "Reset the given user's 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") outputVersion := flag.Bool("v", false, "Output the current version")
flag.Parse() flag.Parse()
debugging = *debugPtr debugging = *debugPtr
app := &app{} app := &app{
cfgFile: *configFile,
}
if *outputVersion { if *outputVersion {
fmt.Println(serverSoftware + " " + softwareVer) fmt.Println(serverSoftware + " " + softwareVer)
@ -196,15 +200,15 @@ func Serve() {
} else if *createConfig { } else if *createConfig {
log.Info("Creating configuration...") log.Info("Creating configuration...")
c := config.New() c := config.New()
log.Info("Saving configuration...") log.Info("Saving configuration %s...", app.cfgFile)
err := config.Save(c) err := config.Save(c, app.cfgFile)
if err != nil { if err != nil {
log.Error("Unable to save configuration: %v", err) log.Error("Unable to save configuration: %v", err)
os.Exit(1) os.Exit(1)
} }
os.Exit(0) os.Exit(0)
} else if *doConfig { } else if *doConfig {
d, err := config.Configure() d, err := config.Configure(app.cfgFile)
if err != nil { if err != nil {
log.Error("Unable to configure: %v", err) log.Error("Unable to configure: %v", err)
os.Exit(1) os.Exit(1)
@ -468,8 +472,8 @@ func Serve() {
} }
func loadConfig(app *app) { func loadConfig(app *app) {
log.Info("Loading configuration...") log.Info("Loading %s configuration...", app.cfgFile)
cfg, err := config.Load() cfg, err := config.Load(app.cfgFile)
if err != nil { if err != nil {
log.Error("Unable to load configuration: %v", err) log.Error("Unable to load configuration: %v", err)
os.Exit(1) os.Exit(1)

View file

@ -101,8 +101,11 @@ func (cfg *Config) IsSecureStandalone() bool {
return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != "" return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
} }
func Load() (*Config, error) { func Load(fname string) (*Config, error) {
cfg, err := ini.Load(FileName) if fname == "" {
fname = FileName
}
cfg, err := ini.Load(fname)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -116,12 +119,15 @@ func Load() (*Config, error) {
return uc, nil return uc, nil
} }
func Save(uc *Config) error { func Save(uc *Config, fname string) error {
cfg := ini.Empty() cfg := ini.Empty()
err := ini.ReflectFrom(cfg, uc) err := ini.ReflectFrom(cfg, uc)
if err != nil { if err != nil {
return err return err
} }
return cfg.SaveTo(FileName) if fname == "" {
fname = FileName
}
return cfg.SaveTo(fname)
} }

View file

@ -14,20 +14,23 @@ type SetupData struct {
Config *Config Config *Config
} }
func Configure() (*SetupData, error) { func Configure(fname string) (*SetupData, error) {
data := &SetupData{} data := &SetupData{}
var err error var err error
if fname == "" {
fname = FileName
}
data.Config, err = Load() data.Config, err = Load(fname)
var action string var action string
isNewCfg := false isNewCfg := false
if err != nil { if err != nil {
fmt.Println("No configuration yet. Creating new.") fmt.Printf("No %s configuration yet. Creating new.\n", fname)
data.Config = New() data.Config = New()
action = "generate" action = "generate"
isNewCfg = true isNewCfg = true
} else { } else {
fmt.Println("Configuration loaded.") fmt.Printf("Loaded configuration %s.\n", fname)
action = "update" action = "update"
} }
title := color.New(color.Bold, color.BgGreen).PrintFunc() title := color.New(color.Bold, color.BgGreen).PrintFunc()
@ -36,7 +39,7 @@ func Configure() (*SetupData, error) {
fmt.Println() fmt.Println()
intro(" ✍ Write Freely Configuration ✍") intro(" ✍ Write Freely Configuration ✍")
fmt.Println() 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() fmt.Println()
title(" Server setup ") title(" Server setup ")
@ -345,5 +348,5 @@ func Configure() (*SetupData, error) {
data.Config.App.Private = fedStatsType == "Private" data.Config.App.Private = fedStatsType == "Private"
} }
return data, Save(data.Config) return data, Save(data.Config, fname)
} }