2018-10-25 13:15:10 +00:00
package config
import (
"fmt"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-wordwrap"
2018-11-10 03:16:13 +00:00
"github.com/writeas/web-core/auth"
2018-10-25 13:15:10 +00:00
"strconv"
)
2018-11-10 03:16:13 +00:00
type SetupData struct {
User * UserCreation
Config * Config
}
func Configure ( ) ( * SetupData , error ) {
data := & SetupData { }
var err error
data . Config , err = Load ( )
2018-11-08 06:31:01 +00:00
var action string
2018-10-25 13:15:10 +00:00
if err != nil {
fmt . Println ( "No configuration yet. Creating new." )
2018-11-10 03:16:13 +00:00
data . Config = New ( )
2018-11-08 06:31:01 +00:00
action = "generate"
2018-10-25 13:15:10 +00:00
} else {
fmt . Println ( "Configuration loaded." )
2018-11-08 06:31:01 +00:00
action = "update"
2018-10-25 13:15:10 +00:00
}
title := color . New ( color . Bold , color . BgGreen ) . PrintlnFunc ( )
intro := color . New ( color . Bold , color . FgWhite ) . PrintlnFunc ( )
fmt . Println ( )
intro ( " ✍ Write Freely Configuration ✍" )
fmt . Println ( )
2018-11-08 06:31:01 +00:00
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 ) )
2018-10-25 13:15:10 +00:00
fmt . Println ( )
title ( " Server setup " )
fmt . Println ( )
2018-11-08 03:06:34 +00:00
tmpls := & promptui . PromptTemplates {
Success : "{{ . | bold | faint }}: " ,
2018-10-25 13:15:10 +00:00
}
2018-11-08 03:06:34 +00:00
selTmpls := & promptui . SelectTemplates {
2018-11-08 17:16:52 +00:00
Selected : fmt . Sprintf ( ` {{ .Label }} {{ . | faint }} ` ) ,
2018-10-25 13:15:10 +00:00
}
2018-11-08 03:06:34 +00:00
prompt := promptui . Prompt {
Templates : tmpls ,
Label : "Local port" ,
Validate : validatePort ,
2018-11-10 03:16:13 +00:00
Default : fmt . Sprintf ( "%d" , data . Config . Server . Port ) ,
2018-10-25 13:15:10 +00:00
}
2018-11-08 03:06:34 +00:00
port , err := prompt . Run ( )
2018-10-25 13:15:10 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . Server . Port , _ = strconv . Atoi ( port ) // Ignore error, as we've already validated number
2018-10-25 13:15:10 +00:00
fmt . Println ( )
title ( " Database setup " )
fmt . Println ( )
prompt = promptui . Prompt {
2018-11-08 03:06:34 +00:00
Templates : tmpls ,
Label : "Username" ,
Validate : validateNonEmpty ,
2018-11-10 03:16:13 +00:00
Default : data . Config . Database . User ,
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . Database . User , err = prompt . Run ( )
2018-10-25 13:15:10 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
prompt = promptui . Prompt {
2018-11-08 03:06:34 +00:00
Templates : tmpls ,
Label : "Password" ,
Validate : validateNonEmpty ,
2018-11-10 03:16:13 +00:00
Default : data . Config . Database . Password ,
2018-11-08 03:06:34 +00:00
Mask : '*' ,
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . Database . Password , err = prompt . Run ( )
2018-10-25 13:15:10 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
prompt = promptui . Prompt {
2018-11-08 03:06:34 +00:00
Templates : tmpls ,
Label : "Database name" ,
Validate : validateNonEmpty ,
2018-11-10 03:16:13 +00:00
Default : data . Config . Database . Database ,
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . Database . Database , err = prompt . Run ( )
2018-10-25 13:15:10 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
prompt = promptui . Prompt {
2018-11-08 03:06:34 +00:00
Templates : tmpls ,
Label : "Host" ,
Validate : validateNonEmpty ,
2018-11-10 03:16:13 +00:00
Default : data . Config . Database . Host ,
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . Database . Host , err = prompt . Run ( )
2018-10-25 13:15:10 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
prompt = promptui . Prompt {
2018-11-08 03:06:34 +00:00
Templates : tmpls ,
Label : "Port" ,
Validate : validatePort ,
2018-11-10 03:16:13 +00:00
Default : fmt . Sprintf ( "%d" , data . Config . Database . Port ) ,
2018-10-25 13:15:10 +00:00
}
dbPort , err := prompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . Database . Port , _ = strconv . Atoi ( dbPort ) // Ignore error, as we've already validated number
2018-10-25 13:15:10 +00:00
fmt . Println ( )
title ( " App setup " )
fmt . Println ( )
selPrompt := promptui . Select {
2018-11-08 03:06:34 +00:00
Templates : selTmpls ,
Label : "Site type" ,
Items : [ ] string { "Single user blog" , "Multi-user instance" } ,
2018-10-25 13:15:10 +00:00
}
_ , usersType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
}
data . Config . App . SingleUser = usersType == "Single user blog"
if data . Config . App . SingleUser {
data . User = & UserCreation { }
// prompt for username
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Admin username" ,
Validate : validateNonEmpty ,
}
data . User . Username , err = prompt . Run ( )
if err != nil {
return data , err
}
// prompt for password
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Admin password" ,
Validate : validateNonEmpty ,
}
newUserPass , err := prompt . Run ( )
if err != nil {
return data , err
}
data . User . HashedPass , err = auth . HashPass ( [ ] byte ( newUserPass ) )
if err != nil {
return data , err
}
2018-10-25 13:15:10 +00:00
}
siteNameLabel := "Instance name"
2018-11-10 03:16:13 +00:00
if data . Config . App . SingleUser {
2018-10-25 13:15:10 +00:00
siteNameLabel = "Blog name"
}
prompt = promptui . Prompt {
2018-11-08 03:06:34 +00:00
Templates : tmpls ,
Label : siteNameLabel ,
Validate : validateNonEmpty ,
2018-11-10 03:16:13 +00:00
Default : data . Config . App . SiteName ,
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . SiteName , err = prompt . Run ( )
2018-10-25 13:15:10 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-08 03:06:34 +00:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Public URL" ,
Validate : validateDomain ,
2018-11-10 03:16:13 +00:00
Default : data . Config . App . Host ,
2018-11-08 03:06:34 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . Host , err = prompt . Run ( )
2018-11-08 03:06:34 +00:00
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-11-08 03:06:34 +00:00
}
2018-11-10 03:16:13 +00:00
if ! data . Config . App . SingleUser {
2018-10-25 13:15:10 +00:00
selPrompt = promptui . Select {
2018-11-08 03:06:34 +00:00
Templates : selTmpls ,
Label : "Registration" ,
Items : [ ] string { "Open" , "Closed" } ,
2018-10-25 13:15:10 +00:00
}
_ , regType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . OpenRegistration = regType == "Open"
2018-11-08 03:06:34 +00:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Max blogs per user" ,
2018-11-10 03:16:13 +00:00
Default : fmt . Sprintf ( "%d" , data . Config . App . MaxBlogs ) ,
2018-11-08 03:06:34 +00:00
}
maxBlogs , err := prompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-11-08 03:06:34 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . MaxBlogs , _ = strconv . Atoi ( maxBlogs ) // Ignore error, as we've already validated number
2018-10-25 13:15:10 +00:00
}
selPrompt = promptui . Select {
2018-11-08 03:06:34 +00:00
Templates : selTmpls ,
Label : "Federation" ,
Items : [ ] string { "Enabled" , "Disabled" } ,
2018-10-25 13:15:10 +00:00
}
_ , fedType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . Federation = fedType == "Enabled"
2018-10-25 13:15:10 +00:00
2018-11-10 03:16:13 +00:00
if data . Config . App . Federation {
2018-10-25 13:15:10 +00:00
selPrompt = promptui . Select {
2018-11-08 03:06:34 +00:00
Templates : selTmpls ,
Label : "Federation usage stats" ,
Items : [ ] string { "Public" , "Private" } ,
2018-10-25 13:15:10 +00:00
}
_ , fedStatsType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . PublicStats = fedStatsType == "Public"
2018-10-25 13:15:10 +00:00
selPrompt = promptui . Select {
2018-11-08 03:06:34 +00:00
Templates : selTmpls ,
Label : "Instance metadata privacy" ,
Items : [ ] string { "Public" , "Private" } ,
2018-10-25 13:15:10 +00:00
}
_ , fedStatsType , err = selPrompt . Run ( )
if err != nil {
2018-11-10 03:16:13 +00:00
return data , err
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
data . Config . App . Private = fedStatsType == "Private"
2018-10-25 13:15:10 +00:00
}
2018-11-10 03:16:13 +00:00
return data , Save ( data . Config )
2018-10-25 13:15:10 +00:00
}