Files
pamietnik/backend/cmd/createuser/main.go
Christoph K. d0b0b4f8bd
Some checks failed
Deploy to NAS / deploy (push) Failing after 4s
Convert backend from submodule to regular directory
Remove submodule tracking; backend is now a plain directory in the repo.
Also update deploy workflow: remove --recurse-submodules.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-07 16:59:50 +02:00

56 lines
1.2 KiB
Go

// cmd/createuser creates a new user in the database.
// Usage: DATABASE_URL=... go run ./cmd/createuser <username> <password>
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5"
"github.com/jacek/pamietnik/backend/internal/auth"
)
func main() {
if len(os.Args) != 3 {
fmt.Fprintln(os.Stderr, "usage: createuser <username> <password>")
os.Exit(1)
}
username := os.Args[1]
password := os.Args[2]
if len(password) < 8 {
fmt.Fprintln(os.Stderr, "password must be at least 8 characters")
os.Exit(1)
}
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
dsn = "postgres://pamietnik:pamietnik@localhost:5432/pamietnik?sslmode=disable"
}
conn, err := pgx.Connect(context.Background(), dsn)
if err != nil {
fmt.Fprintln(os.Stderr, "db error:", err)
os.Exit(1)
}
defer conn.Close(context.Background())
hash, err := auth.HashPassword(password)
if err != nil {
fmt.Fprintln(os.Stderr, "hash error:", err)
os.Exit(1)
}
_, err = conn.Exec(context.Background(),
`INSERT INTO users (username, password_hash) VALUES ($1, $2)`,
username, hash,
)
if err != nil {
fmt.Fprintln(os.Stderr, "insert error:", err)
os.Exit(1)
}
fmt.Printf("user '%s' created\n", username)
}