Add SQLite option to config process

Ref T529
pull/45/head
Matt Baer 6 years ago
parent ba3d6ae64c
commit 17c816477b
  1. 26
      config/config.go
  2. 31
      config/setup.go

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

@ -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,6 +128,20 @@ func Configure() (*SetupData, error) {
title(" Database setup ") title(" Database setup ")
fmt.Println() fmt.Println()
selPrompt = promptui.Select{
Templates: selTmpls,
Label: "Database driver",
Items: []string{"MySQL", "SQLite"},
}
sel, _, err := selPrompt.Run()
if err != nil {
return data, err
}
if sel == 0 {
// Configure for MySQL
data.Config.UseMySQL(isNewCfg)
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Templates: tmpls, Templates: tmpls,
Label: "Username", Label: "Username",
@ -182,6 +198,21 @@ func Configure() (*SetupData, error) {
return data, err return data, err
} }
data.Config.Database.Port, _ = strconv.Atoi(dbPort) // Ignore error, as we've already validated number 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
}
}
fmt.Println() fmt.Println()
title(" App setup ") title(" App setup ")

Loading…
Cancel
Save