summaryrefslogtreecommitdiff
path: root/cmd/admin-cli/main.go
blob: 91439c426f6c2dadc752cc7c9308f3523e1b09a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"auth-proxy/database"
	"auth-proxy/logging"
	"auth-proxy/models"
	"auth-proxy/utils"
	"flag"
	"fmt"
	"os"

	"github.com/joho/godotenv"
)

func main() {
	// Initialize logging
	logging.InitLoggers()

	// Load .env file
	if err := godotenv.Load(); err != nil {
		logging.AppLog.Info("No .env file found, using environment variables")
	}

	username := flag.String("username", "", "Username for the new admin user")
	password := flag.String("password", "", "Password for the new admin user")
	flag.Parse()

	if *username == "" || *password == "" {
		logging.AppLog.Error("Both --username and --password flags are required")
		os.Exit(1)
	}

	// Validate password policy
	if ok, message := utils.ValidatePassword(*password); !ok {
		logging.AppLog.Error("Password does not meet the policy", "reason", message)
		os.Exit(1)
	}

	// Initialize DB connection
	database.InitDB()
	defer database.CloseDB()

	// Check if user already exists
	existingUser, err := models.GetUserByUsername(*username)
	if err != nil {
		logging.AppLog.Error("Error checking for existing user", "error", err)
		os.Exit(1)
	}
	if existingUser != nil {
		logging.AppLog.Error(fmt.Sprintf("User '%s' already exists.", *username))
		os.Exit(1)
	}

	// Create admin user
	_, err = models.CreateUser(*username, *password, "admin")
	if err != nil {
		logging.AppLog.Error("Failed to create admin user", "error", err)
		os.Exit(1)
	}

	fmt.Printf("Admin user '%s' created successfully.\n", *username)
}