golang-dy-back/tools/update_password.go
2025-10-28 16:59:41 +08:00

41 lines
891 B
Go

package main
import (
"fmt"
"os"
"golang.org/x/crypto/bcrypt"
"go-dy/internal/config"
dbpkg "go-dy/internal/db"
)
func main() {
name := os.Getenv("USER_NAME")
pw := os.Getenv("NEW_PASSWORD")
if name == "" || pw == "" {
fmt.Println("missing USER_NAME or NEW_PASSWORD")
os.Exit(1)
}
cfg := config.Load()
db, err := dbpkg.Get(cfg)
if err != nil {
fmt.Println("db error:", err)
os.Exit(1)
}
hash, err := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
if err != nil {
fmt.Println("hash error:", err)
os.Exit(1)
}
res, err := db.Exec("UPDATE users SET password_hash=? WHERE name=?", string(hash), name)
if err != nil {
fmt.Println("update error:", err)
os.Exit(1)
}
n, _ := res.RowsAffected()
fmt.Printf("updated %d rows\n", n)
}