Select Git revision
1_create_table.up.sql
accounts.go 10.28 KiB
package repl
import (
"errors"
"fmt"
"strings"
"git.lyda.ie/kevin/bulletin/ask"
"git.lyda.ie/kevin/bulletin/dclish"
"git.lyda.ie/kevin/bulletin/key"
"git.lyda.ie/kevin/bulletin/storage"
"git.lyda.ie/kevin/bulletin/this"
"git.lyda.ie/kevin/bulletin/users"
)
// ActionUser handles the USER command - it prints out help for all the
// USER subcommands. This is new to the Go version of BULLETIN.
func ActionUser(cmd *dclish.Command) error {
fmt.Println(cmd.Description)
return nil
}
// ActionUserAdd handles the `USER ADD` command. This is used to add a
// new user. This is new to the Go version of BULLETIN.
func ActionUserAdd(cmd *dclish.Command) error {
ctx := storage.Context()
login := strings.ToUpper(cmd.Args[0])
u, err := users.ValidExistingLogin(this.Q, login)
if err != nil {
fmt.Printf("ERROR: %s.\n", err)
return nil
}
if u.Login != "" {
fmt.Println("ERROR: User already exists.")
return nil
}
u, err = this.Q.AddUser(ctx, storage.AddUserParams{
Login: login,
Name: cmd.Args[1],
})
if err != nil {
fmt.Printf("ERROR: %s.\n", err)
return nil
}
if u.Login == "" {
fmt.Println("ERROR: Failed to make user; unknown reason.")
return nil
}
return nil
}
// ActionUserList handles the `USER LIST` command. This lists all the
// users. For now this is limited to only the admin users.
//
// This is new to the Go version of BULLETIN.
func ActionUserList(_ *dclish.Command) error {
if this.User.Admin == 0 {
fmt.Println("ERROR: You are not an admin.")
return nil
}
ctx := storage.Context()
userlist, err := this.Q.ListUsers(ctx)
if err != nil {
fmt.Printf("ERROR: Failed to list users (%s).\n", err)
return nil
}
for _, u := range userlist {
fmt.Printf("%s\n", u)
}
return nil