From dd972e9ba7d945a8981aa748a353cd01d5da0a4d Mon Sep 17 00:00:00 2001 From: Blallo Date: Sat, 29 Sep 2018 12:18:29 +0200 Subject: [PATCH] Added listusers subcommand --- cmd/panelcli/cli.go | 62 ++++++++++++++++++++++++++++++++++++++++++--- db/db.go | 16 ++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/cmd/panelcli/cli.go b/cmd/panelcli/cli.go index f3ff280..df8fd10 100644 --- a/cmd/panelcli/cli.go +++ b/cmd/panelcli/cli.go @@ -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:") diff --git a/db/db.go b/db/db.go index 5e0d57b..fc24c4d 100644 --- a/db/db.go +++ b/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 +}