diff --git a/config/config.go b/config/config.go index ea8754f..d59543a 100644 --- a/config/config.go +++ b/config/config.go @@ -35,6 +35,7 @@ type ( SingleUser bool `ini:"single_user"` OpenRegistration bool `ini:"open_registration"` MinUsernameLen int `ini:"min_username_len"` + MaxBlogs int `ini:"max_blogs"` // Federation Federation bool `ini:"federation"` @@ -65,6 +66,7 @@ func New() *Config { WebFonts: true, SingleUser: true, MinUsernameLen: 3, + MaxBlogs: 1, Federation: true, PublicStats: true, }, diff --git a/config/funcs.go b/config/funcs.go new file mode 100644 index 0000000..2fc130a --- /dev/null +++ b/config/funcs.go @@ -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 +} diff --git a/config/setup.go b/config/setup.go index 5ba2053..a89a148 100644 --- a/config/setup.go +++ b/config/setup.go @@ -28,35 +28,34 @@ func Configure() error { title(" Server setup ") fmt.Println() - prompt := promptui.Prompt{ - Label: "Local port", - Validate: validatePort, - Default: fmt.Sprintf("%d", c.Server.Port), + tmpls := &promptui.PromptTemplates{ + Success: "{{ . | bold | faint }}: ", } - port, err := prompt.Run() - if err != nil { - return err + selTmpls := &promptui.SelectTemplates{ + Selected: fmt.Sprintf(`{{.Label}} {{ . | faint }}`, promptui.IconGood), } - 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, + prompt := promptui.Prompt{ + Templates: tmpls, + Label: "Local port", + Validate: validatePort, + Default: fmt.Sprintf("%d", c.Server.Port), } - c.App.Host, err = prompt.Run() + port, err := prompt.Run() if err != nil { return err } + c.Server.Port, _ = strconv.Atoi(port) // Ignore error, as we've already validated number fmt.Println() title(" Database setup ") fmt.Println() prompt = promptui.Prompt{ - Label: "Username", - Validate: validateNonEmpty, - Default: c.Database.User, + Templates: tmpls, + Label: "Username", + Validate: validateNonEmpty, + Default: c.Database.User, } c.Database.User, err = prompt.Run() if err != nil { @@ -64,10 +63,11 @@ func Configure() error { } prompt = promptui.Prompt{ - Label: "Password", - Validate: validateNonEmpty, - Default: c.Database.Password, - Mask: '*', + Templates: tmpls, + Label: "Password", + Validate: validateNonEmpty, + Default: c.Database.Password, + Mask: '*', } c.Database.Password, err = prompt.Run() if err != nil { @@ -75,9 +75,10 @@ func Configure() error { } prompt = promptui.Prompt{ - Label: "Database name", - Validate: validateNonEmpty, - Default: c.Database.Database, + Templates: tmpls, + Label: "Database name", + Validate: validateNonEmpty, + Default: c.Database.Database, } c.Database.Database, err = prompt.Run() if err != nil { @@ -85,9 +86,10 @@ func Configure() error { } prompt = promptui.Prompt{ - Label: "Host", - Validate: validateNonEmpty, - Default: c.Database.Host, + Templates: tmpls, + Label: "Host", + Validate: validateNonEmpty, + Default: c.Database.Host, } c.Database.Host, err = prompt.Run() if err != nil { @@ -95,9 +97,10 @@ func Configure() error { } prompt = promptui.Prompt{ - Label: "Port", - Validate: validatePort, - Default: fmt.Sprintf("%d", c.Database.Port), + Templates: tmpls, + Label: "Port", + Validate: validatePort, + Default: fmt.Sprintf("%d", c.Database.Port), } dbPort, err := prompt.Run() if err != nil { @@ -110,44 +113,74 @@ func Configure() error { fmt.Println() selPrompt := promptui.Select{ - Label: "Site type", - Items: []string{"Single user", "Multiple users"}, + Templates: selTmpls, + Label: "Site type", + Items: []string{"Single user blog", "Multi-user instance"}, } _, usersType, err := selPrompt.Run() if err != nil { return err } c.App.SingleUser = usersType == "Single user" + // TODO: if c.App.SingleUser { + // prompt for username + // prompt for password + // create blog siteNameLabel := "Instance name" if c.App.SingleUser { siteNameLabel = "Blog name" } prompt = promptui.Prompt{ - Label: siteNameLabel, - Validate: validateNonEmpty, - Default: c.App.SiteName, + Templates: tmpls, + Label: siteNameLabel, + Validate: validateNonEmpty, + Default: c.App.SiteName, } c.App.SiteName, err = prompt.Run() if err != nil { 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 { selPrompt = promptui.Select{ - Label: "Registration", - Items: []string{"Open", "Closed"}, + Templates: selTmpls, + Label: "Registration", + Items: []string{"Open", "Closed"}, } _, regType, err := selPrompt.Run() if err != nil { return err } 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{ - Label: "Federation", - Items: []string{"Enabled", "Disabled"}, + Templates: selTmpls, + Label: "Federation", + Items: []string{"Enabled", "Disabled"}, } _, fedType, err := selPrompt.Run() if err != nil { @@ -157,8 +190,9 @@ func Configure() error { if c.App.Federation { selPrompt = promptui.Select{ - Label: "Federation usage stats", - Items: []string{"Public", "Private"}, + Templates: selTmpls, + Label: "Federation usage stats", + Items: []string{"Public", "Private"}, } _, fedStatsType, err := selPrompt.Run() if err != nil { @@ -167,8 +201,9 @@ func Configure() error { c.App.PublicStats = fedStatsType == "Public" selPrompt = promptui.Select{ - Label: "Instance metadata privacy", - Items: []string{"Public", "Private"}, + Templates: selTmpls, + Label: "Instance metadata privacy", + Items: []string{"Public", "Private"}, } _, fedStatsType, err = selPrompt.Run() if err != nil {