Add MaxBlogs config value

Plus update copy / options / templates in the config process.
This commit is contained in:
Matt Baer 2018-11-07 22:06:34 -05:00
parent 342542444d
commit 8ab29d89da
3 changed files with 96 additions and 42 deletions

View file

@ -35,6 +35,7 @@ type (
SingleUser bool `ini:"single_user"` SingleUser bool `ini:"single_user"`
OpenRegistration bool `ini:"open_registration"` OpenRegistration bool `ini:"open_registration"`
MinUsernameLen int `ini:"min_username_len"` MinUsernameLen int `ini:"min_username_len"`
MaxBlogs int `ini:"max_blogs"`
// Federation // Federation
Federation bool `ini:"federation"` Federation bool `ini:"federation"`
@ -65,6 +66,7 @@ func New() *Config {
WebFonts: true, WebFonts: true,
SingleUser: true, SingleUser: true,
MinUsernameLen: 3, MinUsernameLen: 3,
MaxBlogs: 1,
Federation: true, Federation: true,
PublicStats: true, PublicStats: true,
}, },

17
config/funcs.go Normal file
View file

@ -0,0 +1,17 @@
package config
import (
"strings"
)
// FriendlyHost returns the app's Host sans any schema
func (ac AppCfg) FriendlyHost() string {
return ac.Host[strings.Index(ac.Host, "://")+len("://"):]
}
func (ac AppCfg) CanCreateBlogs(currentlyUsed uint64) bool {
if ac.MaxBlogs <= 0 {
return true
}
return int(currentlyUsed) < ac.MaxBlogs
}

View file

@ -28,10 +28,18 @@ func Configure() error {
title(" Server setup ") title(" Server setup ")
fmt.Println() fmt.Println()
tmpls := &promptui.PromptTemplates{
Success: "{{ . | bold | faint }}: ",
}
selTmpls := &promptui.SelectTemplates{
Selected: fmt.Sprintf(`{{.Label}} {{ . | faint }}`, promptui.IconGood),
}
prompt := promptui.Prompt{ prompt := promptui.Prompt{
Label: "Local port", Templates: tmpls,
Validate: validatePort, Label: "Local port",
Default: fmt.Sprintf("%d", c.Server.Port), Validate: validatePort,
Default: fmt.Sprintf("%d", c.Server.Port),
} }
port, err := prompt.Run() port, err := prompt.Run()
if err != nil { if err != nil {
@ -39,24 +47,15 @@ func Configure() error {
} }
c.Server.Port, _ = strconv.Atoi(port) // Ignore error, as we've already validated number c.Server.Port, _ = strconv.Atoi(port) // Ignore error, as we've already validated number
prompt = promptui.Prompt{
Label: "Public-facing host",
Validate: validateDomain,
Default: c.App.Host,
}
c.App.Host, err = prompt.Run()
if err != nil {
return err
}
fmt.Println() fmt.Println()
title(" Database setup ") title(" Database setup ")
fmt.Println() fmt.Println()
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Label: "Username", Templates: tmpls,
Validate: validateNonEmpty, Label: "Username",
Default: c.Database.User, Validate: validateNonEmpty,
Default: c.Database.User,
} }
c.Database.User, err = prompt.Run() c.Database.User, err = prompt.Run()
if err != nil { if err != nil {
@ -64,10 +63,11 @@ func Configure() error {
} }
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Label: "Password", Templates: tmpls,
Validate: validateNonEmpty, Label: "Password",
Default: c.Database.Password, Validate: validateNonEmpty,
Mask: '*', Default: c.Database.Password,
Mask: '*',
} }
c.Database.Password, err = prompt.Run() c.Database.Password, err = prompt.Run()
if err != nil { if err != nil {
@ -75,9 +75,10 @@ func Configure() error {
} }
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Label: "Database name", Templates: tmpls,
Validate: validateNonEmpty, Label: "Database name",
Default: c.Database.Database, Validate: validateNonEmpty,
Default: c.Database.Database,
} }
c.Database.Database, err = prompt.Run() c.Database.Database, err = prompt.Run()
if err != nil { if err != nil {
@ -85,9 +86,10 @@ func Configure() error {
} }
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Label: "Host", Templates: tmpls,
Validate: validateNonEmpty, Label: "Host",
Default: c.Database.Host, Validate: validateNonEmpty,
Default: c.Database.Host,
} }
c.Database.Host, err = prompt.Run() c.Database.Host, err = prompt.Run()
if err != nil { if err != nil {
@ -95,9 +97,10 @@ func Configure() error {
} }
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Label: "Port", Templates: tmpls,
Validate: validatePort, Label: "Port",
Default: fmt.Sprintf("%d", c.Database.Port), Validate: validatePort,
Default: fmt.Sprintf("%d", c.Database.Port),
} }
dbPort, err := prompt.Run() dbPort, err := prompt.Run()
if err != nil { if err != nil {
@ -110,44 +113,74 @@ func Configure() error {
fmt.Println() fmt.Println()
selPrompt := promptui.Select{ selPrompt := promptui.Select{
Label: "Site type", Templates: selTmpls,
Items: []string{"Single user", "Multiple users"}, Label: "Site type",
Items: []string{"Single user blog", "Multi-user instance"},
} }
_, usersType, err := selPrompt.Run() _, usersType, err := selPrompt.Run()
if err != nil { if err != nil {
return err return err
} }
c.App.SingleUser = usersType == "Single user" c.App.SingleUser = usersType == "Single user"
// TODO: if c.App.SingleUser {
// prompt for username
// prompt for password
// create blog
siteNameLabel := "Instance name" siteNameLabel := "Instance name"
if c.App.SingleUser { if c.App.SingleUser {
siteNameLabel = "Blog name" siteNameLabel = "Blog name"
} }
prompt = promptui.Prompt{ prompt = promptui.Prompt{
Label: siteNameLabel, Templates: tmpls,
Validate: validateNonEmpty, Label: siteNameLabel,
Default: c.App.SiteName, Validate: validateNonEmpty,
Default: c.App.SiteName,
} }
c.App.SiteName, err = prompt.Run() c.App.SiteName, err = prompt.Run()
if err != nil { if err != nil {
return err return err
} }
prompt = promptui.Prompt{
Templates: tmpls,
Label: "Public URL",
Validate: validateDomain,
Default: c.App.Host,
}
c.App.Host, err = prompt.Run()
if err != nil {
return err
}
if !c.App.SingleUser { if !c.App.SingleUser {
selPrompt = promptui.Select{ selPrompt = promptui.Select{
Label: "Registration", Templates: selTmpls,
Items: []string{"Open", "Closed"}, Label: "Registration",
Items: []string{"Open", "Closed"},
} }
_, regType, err := selPrompt.Run() _, regType, err := selPrompt.Run()
if err != nil { if err != nil {
return err return err
} }
c.App.OpenRegistration = regType == "Open" c.App.OpenRegistration = regType == "Open"
prompt = promptui.Prompt{
Templates: tmpls,
Label: "Max blogs per user",
Default: fmt.Sprintf("%d", c.App.MaxBlogs),
}
maxBlogs, err := prompt.Run()
if err != nil {
return err
}
c.App.MaxBlogs, _ = strconv.Atoi(maxBlogs) // Ignore error, as we've already validated number
} }
selPrompt = promptui.Select{ selPrompt = promptui.Select{
Label: "Federation", Templates: selTmpls,
Items: []string{"Enabled", "Disabled"}, Label: "Federation",
Items: []string{"Enabled", "Disabled"},
} }
_, fedType, err := selPrompt.Run() _, fedType, err := selPrompt.Run()
if err != nil { if err != nil {
@ -157,8 +190,9 @@ func Configure() error {
if c.App.Federation { if c.App.Federation {
selPrompt = promptui.Select{ selPrompt = promptui.Select{
Label: "Federation usage stats", Templates: selTmpls,
Items: []string{"Public", "Private"}, Label: "Federation usage stats",
Items: []string{"Public", "Private"},
} }
_, fedStatsType, err := selPrompt.Run() _, fedStatsType, err := selPrompt.Run()
if err != nil { if err != nil {
@ -167,8 +201,9 @@ func Configure() error {
c.App.PublicStats = fedStatsType == "Public" c.App.PublicStats = fedStatsType == "Public"
selPrompt = promptui.Select{ selPrompt = promptui.Select{
Label: "Instance metadata privacy", Templates: selTmpls,
Items: []string{"Public", "Private"}, Label: "Instance metadata privacy",
Items: []string{"Public", "Private"},
} }
_, fedStatsType, err = selPrompt.Run() _, fedStatsType, err = selPrompt.Run()
if err != nil { if err != nil {