Add SQLite option to config process

Ref T529
This commit is contained in:
Matt Baer 2018-12-08 13:36:51 -05:00
parent ba3d6ae64c
commit 17c816477b
2 changed files with 98 additions and 53 deletions

View File

@ -60,16 +60,11 @@ type (
) )
func New() *Config { func New() *Config {
return &Config{ c := &Config{
Server: ServerCfg{ Server: ServerCfg{
Port: 8080, Port: 8080,
Bind: "localhost", /* IPV6 support when not using localhost? */ Bind: "localhost", /* IPV6 support when not using localhost? */
}, },
Database: DatabaseCfg{
Type: "mysql",
Host: "localhost",
Port: 3306,
},
App: AppCfg{ App: AppCfg{
Host: "http://localhost:8080", Host: "http://localhost:8080",
Theme: "write", Theme: "write",
@ -81,6 +76,25 @@ func New() *Config {
PublicStats: true, PublicStats: true,
}, },
} }
c.UseMySQL(true)
return c
}
// UseMySQL resets the Config's Database to use default values for a MySQL setup.
func (cfg *Config) UseMySQL(fresh bool) {
cfg.Database.Type = "mysql"
if fresh {
cfg.Database.Host = "localhost"
cfg.Database.Port = 3306
}
}
// UseSQLite resets the Config's Database to use default values for a SQLite setup.
func (cfg *Config) UseSQLite(fresh bool) {
cfg.Database.Type = "sqlite3"
if fresh {
cfg.Database.FileName = "writefreely.db"
}
} }
func (cfg *Config) IsSecureStandalone() bool { func (cfg *Config) IsSecureStandalone() bool {

View File

@ -20,10 +20,12 @@ func Configure() (*SetupData, error) {
data.Config, err = Load() data.Config, err = Load()
var action string var action string
isNewCfg := false
if err != nil { if err != nil {
fmt.Println("No configuration yet. Creating new.") fmt.Println("No configuration yet. Creating new.")
data.Config = New() data.Config = New()
action = "generate" action = "generate"
isNewCfg = true
} else { } else {
fmt.Println("Configuration loaded.") fmt.Println("Configuration loaded.")
action = "update" action = "update"
@ -126,62 +128,91 @@ func Configure() (*SetupData, error) {
title(" Database setup ") title(" Database setup ")
fmt.Println() fmt.Println()
prompt = promptui.Prompt{ selPrompt = promptui.Select{
Templates: tmpls, Templates: selTmpls,
Label: "Username", Label: "Database driver",
Validate: validateNonEmpty, Items: []string{"MySQL", "SQLite"},
Default: data.Config.Database.User,
} }
data.Config.Database.User, err = prompt.Run() sel, _, err := selPrompt.Run()
if err != nil { if err != nil {
return data, err return data, err
} }
prompt = promptui.Prompt{ if sel == 0 {
Templates: tmpls, // Configure for MySQL
Label: "Password", data.Config.UseMySQL(isNewCfg)
Validate: validateNonEmpty,
Default: data.Config.Database.Password,
Mask: '*',
}
data.Config.Database.Password, err = prompt.Run()
if err != nil {
return data, err
}
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Templates: tmpls, Templates: tmpls,
Label: "Database name", Label: "Username",
Validate: validateNonEmpty, Validate: validateNonEmpty,
Default: data.Config.Database.Database, Default: data.Config.Database.User,
} }
data.Config.Database.Database, err = prompt.Run() data.Config.Database.User, err = prompt.Run()
if err != nil { if err != nil {
return data, err return data, err
} }
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Templates: tmpls, Templates: tmpls,
Label: "Host", Label: "Password",
Validate: validateNonEmpty, Validate: validateNonEmpty,
Default: data.Config.Database.Host, Default: data.Config.Database.Password,
} Mask: '*',
data.Config.Database.Host, err = prompt.Run() }
if err != nil { data.Config.Database.Password, err = prompt.Run()
return data, err if err != nil {
} return data, err
}
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Templates: tmpls, Templates: tmpls,
Label: "Port", Label: "Database name",
Validate: validatePort, Validate: validateNonEmpty,
Default: fmt.Sprintf("%d", data.Config.Database.Port), Default: data.Config.Database.Database,
}
data.Config.Database.Database, err = prompt.Run()
if err != nil {
return data, err
}
prompt = promptui.Prompt{
Templates: tmpls,
Label: "Host",
Validate: validateNonEmpty,
Default: data.Config.Database.Host,
}
data.Config.Database.Host, err = prompt.Run()
if err != nil {
return data, err
}
prompt = promptui.Prompt{
Templates: tmpls,
Label: "Port",
Validate: validatePort,
Default: fmt.Sprintf("%d", data.Config.Database.Port),
}
dbPort, err := prompt.Run()
if err != nil {
return data, err
}
data.Config.Database.Port, _ = strconv.Atoi(dbPort) // Ignore error, as we've already validated number
} else if sel == 1 {
// Configure for SQLite
data.Config.UseSQLite(isNewCfg)
prompt = promptui.Prompt{
Templates: tmpls,
Label: "Filename",
Validate: validateNonEmpty,
Default: data.Config.Database.FileName,
}
data.Config.Database.FileName, err = prompt.Run()
if err != nil {
return data, err
}
} }
dbPort, err := prompt.Run()
if err != nil {
return data, err
}
data.Config.Database.Port, _ = strconv.Atoi(dbPort) // Ignore error, as we've already validated number
fmt.Println() fmt.Println()
title(" App setup ") title(" App setup ")