WIP insert helper

trunk
vilmibm 2023-03-06 18:18:24 +00:00
parent c4c02533e7
commit 55eb4b7010
1 changed files with 51 additions and 7 deletions

View File

@ -2,6 +2,8 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
@ -10,22 +12,64 @@ import (
"git.tilde.town/tildetown/town/towndb" "git.tilde.town/tildetown/town/towndb"
) )
// TODO this command adds a new user to /town/var/town.db // this command adds a new user to /town/var/town.db. it's meant to be invoked
// it's meant to be invoked by the welcome binary upon successfully // by the welcome binary upon successfully creating a new user account
// creating a new user account
func _main(db *sql.DB) error { func _main(tdb *sql.DB, userData towndb.TownUser) error {
return nil if userData.Username == "" {
return errors.New("no username")
}
if len(userData.Emails) == 0 {
return errors.New("no email set")
}
for _, e := range userData.Emails {
if e == "" {
return errors.New("blank email in email list")
}
}
if userData.IsAdmin {
return errors.New("please stop")
}
if userData.State != towndb.StateActive {
return errors.New("bad state")
}
return userData.Insert(tdb)
}
func parseInput(stdin *os.File) (u towndb.TownUser, err error) {
var n int
input := make([]byte, 3000) // arbitrary
if n, err = stdin.Read(input); err != nil {
return
}
if n == 0 {
err = errors.New("nothing passed on input")
return
}
err = json.Unmarshal(input[0:n], &u)
return
} }
func main() { func main() {
towndb, err := towndb.ConnectDB() tdb, err := towndb.ConnectDB()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
if err = _main(towndb); err != nil {
userData, err := parseInput(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(2) os.Exit(2)
} }
if err = _main(tdb, userData); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(3)
}
} }