forked from boyska/feedpanel
Added listusers subcommand
This commit is contained in:
parent
c92074003c
commit
dd972e9ba7
2 changed files with 75 additions and 3 deletions
|
@ -22,8 +22,9 @@ var cmdMap map[string]commandFunc
|
|||
|
||||
func init() {
|
||||
cmdMap = map[string]commandFunc{
|
||||
"setup": cmdSetup,
|
||||
"adduser": cmdAddUser,
|
||||
"setup": cmdSetup,
|
||||
"adduser": cmdAddUser,
|
||||
"listusers": cmdListUsers,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,7 +34,7 @@ func getDB() paneldb.DB {
|
|||
return db
|
||||
}
|
||||
|
||||
func cmdSetup(_args []string) error {
|
||||
func cmdSetup(args []string) error {
|
||||
db := getDB()
|
||||
err := db.Setup()
|
||||
if err != nil {
|
||||
|
@ -96,6 +97,61 @@ func cmdAddUser(args []string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func composeListUserOutput(user paneldb.User, usernameFlag, emailFlag, dateCreatedFlag bool) {
|
||||
var row string
|
||||
if usernameFlag {
|
||||
row += user.UserName
|
||||
if emailFlag || !dateCreatedFlag {
|
||||
row += "\t"
|
||||
}
|
||||
}
|
||||
if emailFlag {
|
||||
row += user.Email
|
||||
if !dateCreatedFlag {
|
||||
row += "\t"
|
||||
}
|
||||
}
|
||||
if !dateCreatedFlag {
|
||||
row += user.DateCreated
|
||||
}
|
||||
fmt.Fprintln(os.Stdout, row)
|
||||
}
|
||||
|
||||
func cmdListUsers(args []string) error {
|
||||
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||
usernameFlag := flagset.Bool("no-nick", true, "skip printing usernames")
|
||||
emailFlag := flagset.Bool("no-email", true, "skip printing emails")
|
||||
dateCreatedFlag := flagset.Bool("created", false, "print date of user creation")
|
||||
headerFlag := flagset.Bool("no-header", false, "skip printing the header")
|
||||
flagset.Parse(args[1:])
|
||||
db := getDB()
|
||||
users, err := db.UserList()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Printing the header
|
||||
if *headerFlag {
|
||||
header := paneldb.User{
|
||||
UserName: "username",
|
||||
Email: "email",
|
||||
DateCreated: "date_created",
|
||||
}
|
||||
separator := paneldb.User{
|
||||
UserName: "--------",
|
||||
Email: "-----",
|
||||
DateCreated: "------------",
|
||||
}
|
||||
composeListUserOutput(header, *usernameFlag, *emailFlag, *dateCreatedFlag)
|
||||
composeListUserOutput(separator, *usernameFlag, *emailFlag, *dateCreatedFlag)
|
||||
}
|
||||
// Printing the user list
|
||||
for _, user := range users {
|
||||
composeListUserOutput(user, *usernameFlag, *emailFlag, *dateCreatedFlag)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func usage(w io.Writer) {
|
||||
fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0])
|
||||
fmt.Fprintln(w, "\nSubcommands:")
|
||||
|
|
16
db/db.go
16
db/db.go
|
@ -8,6 +8,12 @@ import (
|
|||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
UserName string
|
||||
Email string
|
||||
DateCreated string
|
||||
}
|
||||
|
||||
type DB struct {
|
||||
PgDB *pg.DB
|
||||
Schema string
|
||||
|
@ -24,6 +30,7 @@ func (db *DB) Setup() error {
|
|||
uid UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
username varchar(64) UNIQUE NOT NULL,
|
||||
email varchar(100) UNIQUE NOT NULL,
|
||||
date_created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
password varchar(256) NOT NULL
|
||||
)`)
|
||||
if err != nil {
|
||||
|
@ -48,3 +55,12 @@ func (db *DB) UserAdd(username, email, pwd string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) UserList() ([]User, error) {
|
||||
var users []User
|
||||
_, err := db.PgDB.Query(&users, "SELECT * FROM users.users")
|
||||
if err != nil {
|
||||
return users, errors.Wrap(err, "User enumeration failed")
|
||||
}
|
||||
return users, err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue