Files
chorus/cmd/chorus-admin-bootstrap/main.go
T

56 lines
1.4 KiB
Go

// chorus-admin-bootstrap creates the first administrator from external inputs.
package main
import (
"context"
"database/sql"
"errors"
"log"
"os"
"time"
"git.ilapage.cn/OPC/chorus/internal/adminbootstrap"
_ "github.com/go-sql-driver/mysql"
)
func main() {
result, err := run(os.LookupEnv, os.Args[1:])
if err != nil {
log.Printf("chorus admin bootstrap failed: %v", err)
os.Exit(1)
}
switch result {
case adminbootstrap.Created:
log.Print("chorus administrator account created")
case adminbootstrap.Unchanged:
log.Print("chorus administrator account already exists; password was not changed")
case adminbootstrap.PasswordReset:
log.Print("chorus administrator password reset completed")
default:
log.Print("chorus administrator bootstrap completed")
}
}
func run(lookup func(string) (string, bool), args []string) (adminbootstrap.Result, error) {
if len(args) != 0 {
return 0, errors.New("chorus-admin-bootstrap accepts no command-line arguments")
}
cfg, err := adminbootstrap.LoadConfig(lookup)
if err != nil {
return 0, err
}
db, err := sql.Open("mysql", cfg.DSN)
if err != nil {
return 0, adminbootstrap.ErrDatabaseUnavailable
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
result, err := adminbootstrap.Bootstrap(ctx, db, cfg)
if err != nil {
return 0, err
}
return result, nil
}