showuser: display info about a single user
This commit is contained in:
parent
a4d7e6640b
commit
5daf507569
2 changed files with 74 additions and 5 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
41
db/db.go
41
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
|
||||
}
|
||||
|
|
Reference in a new issue