Fix T657: add --sections argument to allow partial configuration.

Use the split argument list (slice) just for validation purposes
as it's substantially easier to do `.contains` in a string instead
of a slice. As such, pass the `configSections` arguments to
`Configure()` and check the existence of each one before showing
the options to the user.

An empty argument list is replaced by "server db app" so everything
is there negating the need to check anything else in `Configure()`.
In the same vein the default is "server db app".

The parsing is done in `app.go` alongside the other flags instead
of `main.go` as described in T657.
pull/127/head
Michael Demetriou 6 years ago
parent 1d5c396327
commit 07fe366c15
  1. 5
      app.go
  2. 18
      config/setup.go

@ -235,15 +235,14 @@ func Serve() {
if *configSections == "" {
*configSections = "server db app"
}
configSectionsArray := strings.Split(*configSections, " ")
// let's check there aren't any garbage in the list
configSectionsArray := strings.Split(*configSections, " ")
for _, element := range configSectionsArray {
if element != "server" && element != "db" && element != "app" {
log.Error("Invalid argument to --sections. Valid arguments are only \"server\", \"db\" and \"app\"")
}
}
d, err := config.Configure(app.cfgFile, configSectionsArray)
d, err := config.Configure(app.cfgFile, *configSections)
if err != nil {
log.Error("Unable to configure: %v", err)
os.Exit(1)

@ -17,6 +17,7 @@ import (
"github.com/mitchellh/go-wordwrap"
"github.com/writeas/web-core/auth"
"strconv"
"strings"
)
type SetupData struct {
@ -24,7 +25,7 @@ type SetupData struct {
Config *Config
}
func Configure(fname string, configSections []string) (*SetupData, error) {
func Configure(fname string, configSections string) (*SetupData, error) {
data := &SetupData{}
var err error
if fname == "" {
@ -52,9 +53,6 @@ func Configure(fname string, configSections []string) (*SetupData, error) {
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 ")
fmt.Println()
tmpls := &promptui.PromptTemplates{
Success: "{{ . | bold | faint }}: ",
}
@ -78,6 +76,12 @@ func Configure(fname string, configSections []string) (*SetupData, error) {
data.Config.Server.Dev = isDevEnv
var prompt promptui.Prompt
if strings.Contains(configSections, "server"){
title(" Server setup ")
fmt.Println()
if isDevEnv || !isStandalone {
// Running in dev environment or behind reverse proxy; ask for port
prompt = promptui.Prompt{
@ -138,6 +142,8 @@ func Configure(fname string, configSections []string) (*SetupData, error) {
}
fmt.Println()
}
if strings.Contains(configSections, "db"){
title(" Database setup ")
fmt.Println()
@ -228,6 +234,9 @@ func Configure(fname string, configSections []string) (*SetupData, error) {
}
fmt.Println()
}
if strings.Contains(configSections, "app"){
title(" App setup ")
fmt.Println()
@ -357,6 +366,7 @@ func Configure(fname string, configSections []string) (*SetupData, error) {
}
data.Config.App.Private = fedStatsType == "Private"
}
}
return data, Save(data.Config, fname)
}

Loading…
Cancel
Save