118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var auto bool
|
|
var force bool
|
|
|
|
// genconfigCmd represents the genconfig command
|
|
var genconfigCmd = &cobra.Command{
|
|
Use: "genconfig",
|
|
Short: "Interactively generate config file",
|
|
Long: `Use this command to interactively create a configuration file for this
|
|
application, `,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var err error
|
|
fmt.Println("Force:", force)
|
|
fmt.Println("Auto:", auto)
|
|
if auto == false {
|
|
quiz()
|
|
}
|
|
if force == true {
|
|
err = viper.WriteConfig()
|
|
} else {
|
|
err = viper.SafeWriteConfig()
|
|
}
|
|
if err != nil {
|
|
fmt.Printf("could not write config file: %v", err)
|
|
}
|
|
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(genconfigCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// genconfigCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// genconfigCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
genconfigCmd.Flags().BoolVarP(&auto, "auto", "a", false, "generate Config file with just default values")
|
|
genconfigCmd.Flags().BoolVarP(&force, "force", "f", false, "overwrites specified config file")
|
|
}
|
|
|
|
func quiz() {
|
|
fmt.Println("Welcome to the Config generation wizard.")
|
|
stringPrompt("What address should the Application listen on (e.g. 0.0.0.0:80)", "web.address")
|
|
stringPrompt("What Database do you want to use? (e.g. SQLite, Postgres)", "db.type")
|
|
if strings.EqualFold(viper.GetString("db.type"), "sqlite") {
|
|
stringPrompt("What is the path to the Database?", "db.path")
|
|
} else {
|
|
stringPrompt("What is the name of the Database?", "db.name")
|
|
stringPrompt("What Machine is your DB hosted on?(e.g. localhost, db1.example.com)", "db.host")
|
|
stringPrompt("What User do you want to use to connect to the Database? (e.g. user)", "db.user")
|
|
stringPrompt("What is the Password for the Database user?", "db.password")
|
|
stringPrompt("What Port does the Database listen on?", "db.port")
|
|
stringPrompt("Do you want to enable SSL mode? (enable/disable)", "db.sslmode")
|
|
}
|
|
stringPrompt("What is your MinIO/S3 Access Key?", "minio.accessKeyID")
|
|
stringPrompt("What is your MinIO/S3 Access Key Secret?", "minio.AccessKeySecret")
|
|
stringPrompt("What is your MinIO/S3 Host?", "minio.hostname")
|
|
intPrompt("What Port does MinIO/S3 listen on?", "minio.port")
|
|
stringPrompt("Do you want to use TLS? (true/false)", "minio.sslmode")
|
|
stringPrompt("What is your MinIO/S3 Bucket?", "minio.bucket")
|
|
stringPrompt("What is your MINIO/S3 Location?", "minio.location")
|
|
}
|
|
|
|
func stringPrompt(prompt string, viperKey string) {
|
|
defaultValue := viper.GetString(viperKey)
|
|
var str string
|
|
r := bufio.NewReader(os.Stdin)
|
|
for {
|
|
fmt.Fprintf(os.Stdout, "%s [%s]: ", prompt, defaultValue)
|
|
str, _ = r.ReadString('\n')
|
|
if str != "" {
|
|
break
|
|
}
|
|
}
|
|
str = strings.TrimSpace(str)
|
|
if str != "" {
|
|
viper.Set(viperKey, strings.TrimSpace(str))
|
|
}
|
|
}
|
|
|
|
func intPrompt(prompt string, viperKey string) {
|
|
defaultValue := viper.GetInt(viperKey)
|
|
var str string
|
|
r := bufio.NewReader(os.Stdin)
|
|
for {
|
|
fmt.Fprintf(os.Stdout, "%s [%d]: ", prompt, defaultValue)
|
|
str, _ = r.ReadString('\n')
|
|
if str != "" {
|
|
break
|
|
}
|
|
}
|
|
str = strings.TrimSpace(str)
|
|
integer, err := strconv.Atoi(str)
|
|
if err != nil {
|
|
fmt.Printf("Not a valid Port: %v", err)
|
|
}
|
|
if integer > 0 {
|
|
viper.Set(viperKey, integer)
|
|
}
|
|
}
|