From 5daf507569c16901af967368034568c5bef41ee0 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 29 Sep 2018 21:48:54 +0200 Subject: [PATCH] showuser: display info about a single user --- cmd/panelcli/cli.go | 38 ++++++++++++++++++++++++++++++++++---- db/db.go | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/cmd/panelcli/cli.go b/cmd/panelcli/cli.go index afde88a..3b965ff 100644 --- a/cmd/panelcli/cli.go +++ b/cmd/panelcli/cli.go @@ -28,6 +28,7 @@ func init() { "setup": cmdSetup, "adduser": cmdAddUser, "listusers": cmdListUsers, + "showuser": cmdShowUser, } } @@ -103,7 +104,7 @@ func cmdAddUser(args []string) error { func composeListUserOutput(user paneldb.User, usernameFlag, emailFlag, dateCreatedFlag bool) { var row string if usernameFlag { - row += user.UserName + row += user.Name if emailFlag || dateCreatedFlag { row += "\t" } @@ -136,12 +137,12 @@ func cmdListUsers(args []string) error { // Printing the header if !*headerFlag { header := paneldb.User{ - UserName: "username", + Name: "username", Email: "email", DateCreated: "date_created", } separator := paneldb.User{ - UserName: "--------", + Name: "--------", Email: "-----", DateCreated: "------------", } @@ -155,10 +156,39 @@ func cmdListUsers(args []string) error { 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) { fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0]) fmt.Fprintln(w, "\nSubcommands:") - for cmdName, _ := range cmdMap { + for cmdName := range cmdMap { fmt.Fprintln(w, " -", cmdName) } } diff --git a/db/db.go b/db/db.go index 34c08c2..ea3e42d 100644 --- a/db/db.go +++ b/db/db.go @@ -8,19 +8,26 @@ import ( "golang.org/x/crypto/bcrypt" ) +// An User represents a user of the application. type User struct { ID string `sql:"uid"` - UserName string `sql:"username"` + Name string `sql:"username"` Email string `sql:"email"` DateCreated string `sql:"date_created"` 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 { PgDB *pg.DB Schema string } +// Setup will create schemas and tables as appropriate func (db *DB) Setup() error { _, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`) if err != nil { @@ -42,6 +49,7 @@ func (db *DB) Setup() error { return nil } +// UserAdd will add a user to the database 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)") if err != nil { @@ -58,6 +66,7 @@ func (db *DB) UserAdd(username, email, pwd string) error { return nil } +// GetUsers returns an array of Users func (db *DB) GetUsers() ([]User, error) { var users []User _, err := db.PgDB.Query(&users, "SELECT * FROM users.users") @@ -66,3 +75,33 @@ func (db *DB) GetUsers() ([]User, error) { } 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 +}