Add adduser CLI command for creating users from the command line
Build and Deploy / build-and-deploy (push) Successful in 1m19s
Build and Deploy / build-and-deploy (push) Successful in 1m19s
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
"podstalk/handler"
|
"podstalk/handler"
|
||||||
"podstalk/store"
|
"podstalk/store"
|
||||||
)
|
)
|
||||||
@@ -19,6 +21,11 @@ var templateFS embed.FS
|
|||||||
var staticFS embed.FS
|
var staticFS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if len(os.Args) > 1 && os.Args[1] == "adduser" {
|
||||||
|
runAddUser()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
dbPath := os.Getenv("PODSTALK_DB")
|
dbPath := os.Getenv("PODSTALK_DB")
|
||||||
if dbPath == "" {
|
if dbPath == "" {
|
||||||
dbPath = "podstalk.db"
|
dbPath = "podstalk.db"
|
||||||
@@ -93,6 +100,38 @@ func main() {
|
|||||||
log.Fatal(http.ListenAndServe(addr, nil))
|
log.Fatal(http.ListenAndServe(addr, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runAddUser() {
|
||||||
|
if len(os.Args) != 4 {
|
||||||
|
log.Fatalf("Usage: podstalk adduser <email> <password>")
|
||||||
|
}
|
||||||
|
|
||||||
|
email := os.Args[2]
|
||||||
|
password := os.Args[3]
|
||||||
|
|
||||||
|
dbPath := os.Getenv("PODSTALK_DB")
|
||||||
|
if dbPath == "" {
|
||||||
|
dbPath = "podstalk.db"
|
||||||
|
}
|
||||||
|
|
||||||
|
st, err := store.New(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to open database: %v", err)
|
||||||
|
}
|
||||||
|
defer st.Close()
|
||||||
|
|
||||||
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to hash password: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := st.CreateUser(email, string(hash))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to create user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("User created (id=%d)", userID)
|
||||||
|
}
|
||||||
|
|
||||||
func envOrDefault(key, def string) string {
|
func envOrDefault(key, def string) string {
|
||||||
if v := os.Getenv(key); v != "" {
|
if v := os.Getenv(key); v != "" {
|
||||||
return v
|
return v
|
||||||
|
|||||||
Reference in New Issue
Block a user