showuser: display info about a single user

This commit is contained in:
boyska 2018-09-29 21:48:54 +02:00
parent a4d7e6640b
commit 5daf507569
2 changed files with 74 additions and 5 deletions

View file

@ -28,6 +28,7 @@ func init() {
"setup": cmdSetup, "setup": cmdSetup,
"adduser": cmdAddUser, "adduser": cmdAddUser,
"listusers": cmdListUsers, "listusers": cmdListUsers,
"showuser": cmdShowUser,
} }
} }
@ -103,7 +104,7 @@ func cmdAddUser(args []string) error {
func composeListUserOutput(user paneldb.User, usernameFlag, emailFlag, dateCreatedFlag bool) { func composeListUserOutput(user paneldb.User, usernameFlag, emailFlag, dateCreatedFlag bool) {
var row string var row string
if usernameFlag { if usernameFlag {
row += user.UserName row += user.Name
if emailFlag || dateCreatedFlag { if emailFlag || dateCreatedFlag {
row += "\t" row += "\t"
} }
@ -136,12 +137,12 @@ func cmdListUsers(args []string) error {
// Printing the header // Printing the header
if !*headerFlag { if !*headerFlag {
header := paneldb.User{ header := paneldb.User{
UserName: "username", Name: "username",
Email: "email", Email: "email",
DateCreated: "date_created", DateCreated: "date_created",
} }
separator := paneldb.User{ separator := paneldb.User{
UserName: "--------", Name: "--------",
Email: "-----", Email: "-----",
DateCreated: "------------", DateCreated: "------------",
} }
@ -155,10 +156,39 @@ func cmdListUsers(args []string) error {
return nil return nil
} }
func cmdShowUser(args []string) error {
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
byEmail := flagset.String("email", "", "Search by email")
byName := flagset.String("username", "", "Search by username")
flagset.Parse(args[1:])
if *byEmail != "" && *byName != "" {
fmt.Fprintln(os.Stderr, "-email and -username conflict")
os.Exit(2)
}
if *byEmail == "" && *byName == "" {
fmt.Fprintln(os.Stderr, "at least one of -email and -username must be specified")
os.Exit(2)
}
db := getDB()
var user paneldb.User
var err error
switch {
case *byName != "":
user, err = db.GetUserByName(*byName)
case *byEmail != "":
user, err = db.GetUserByEmail(*byEmail)
}
if err != nil {
return err
}
fmt.Println(user)
return nil
}
func usage(w io.Writer) { func usage(w io.Writer) {
fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0]) fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0])
fmt.Fprintln(w, "\nSubcommands:") fmt.Fprintln(w, "\nSubcommands:")
for cmdName, _ := range cmdMap { for cmdName := range cmdMap {
fmt.Fprintln(w, " -", cmdName) fmt.Fprintln(w, " -", cmdName)
} }
} }

View file

@ -8,19 +8,26 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
// An User represents a user of the application.
type User struct { type User struct {
ID string `sql:"uid"` ID string `sql:"uid"`
UserName string `sql:"username"` Name string `sql:"username"`
Email string `sql:"email"` Email string `sql:"email"`
DateCreated string `sql:"date_created"` DateCreated string `sql:"date_created"`
PasswordHash string `sql:"password"` PasswordHash string `sql:"password"`
} }
func (u User) String() string {
return fmt.Sprintf("{User: %s <%s>}", u.Name, u.Email)
}
// DB represents the application database. It provides high-level methods to get and manipulate objects.
type DB struct { type DB struct {
PgDB *pg.DB PgDB *pg.DB
Schema string Schema string
} }
// Setup will create schemas and tables as appropriate
func (db *DB) Setup() error { func (db *DB) Setup() error {
_, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`) _, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`)
if err != nil { if err != nil {
@ -42,6 +49,7 @@ func (db *DB) Setup() error {
return nil return nil
} }
// UserAdd will add a user to the database
func (db *DB) UserAdd(username, email, pwd string) error { func (db *DB) UserAdd(username, email, pwd string) error {
stmt, err := db.PgDB.Prepare("INSERT INTO users.users (username, email, password) VALUES ($1, $2, $3)") stmt, err := db.PgDB.Prepare("INSERT INTO users.users (username, email, password) VALUES ($1, $2, $3)")
if err != nil { if err != nil {
@ -58,6 +66,7 @@ func (db *DB) UserAdd(username, email, pwd string) error {
return nil return nil
} }
// GetUsers returns an array of Users
func (db *DB) GetUsers() ([]User, error) { func (db *DB) GetUsers() ([]User, error) {
var users []User var users []User
_, err := db.PgDB.Query(&users, "SELECT * FROM users.users") _, err := db.PgDB.Query(&users, "SELECT * FROM users.users")
@ -66,3 +75,33 @@ func (db *DB) GetUsers() ([]User, error) {
} }
return users, err return users, err
} }
// GetUserByName Search for exact username
func (db *DB) GetUserByName(username string) (User, error) {
var user User
_, err := db.PgDB.QueryOne(&user, `SELECT * FROM users.users WHERE username = ?`, username)
if err != nil {
return user, errors.Wrap(err, "User fetch failed")
}
return user, nil
}
// GetUserByEmail searches for exact email
func (db *DB) GetUserByEmail(email string) (User, error) {
var user User
_, err := db.PgDB.QueryOne(&user, "SELECT * FROM users.users WHERE email = ?", email)
if err != nil {
return user, errors.Wrap(err, "User fetch failed")
}
return user, nil
}
// GetUserByID search for exact uid
func (db *DB) GetUserByID(uid string) (User, error) {
var user User
_, err := db.PgDB.QueryOne(&user, "SELECT * FROM users.users WHERE uid=$1", uid)
if err != nil {
return user, errors.Wrap(err, "User fetch failed")
}
return user, nil
}