53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package app
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// ---------------- CLI ----------------
|
|
|
|
func cliInit(args []string) error {
|
|
fs := flag.NewFlagSet("init", flag.ExitOnError)
|
|
configFile := fs.String("config", defaultServerConfigPath(), "server config file")
|
|
if err := fs.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
cfg := ServerConfig{}
|
|
cfg.Addr = promptDefault("Listen address", ":8080")
|
|
cfg.PublicURL = strings.TrimRight(promptDefault("Public URL", defaultServerURL), "/")
|
|
cfg.Storage = promptDefault("Storage directory", defaultStorage)
|
|
cfg.MySQLDSN = promptDefault("MySQL DSN", "root:pass@tcp(127.0.0.1:3306)/gitocean?parseTime=true")
|
|
|
|
db, err := openMySQLAndCreateDatabaseIfMissing(cfg.MySQLDSN)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
if err := migrate(db); err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(cfg.Storage, "repos"), 0755); err != nil {
|
|
return err
|
|
}
|
|
if strings.EqualFold(promptDefault("Create first admin user?", "n"), "y") {
|
|
email := strings.ToLower(prompt("Email: "))
|
|
username := strings.ToLower(prompt("Username: "))
|
|
password := prompt("Password: ")
|
|
if err := createUserDirect(db, email, username, password, true); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Created admin user %s\n", username)
|
|
}
|
|
if err := saveServerConfig(*configFile, cfg); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Initialized Gitocean at %s\n\n", *configFile)
|
|
fmt.Println("Start the server with:")
|
|
fmt.Printf(" gitocean server --config %s\n", *configFile)
|
|
return nil
|
|
}
|