summaryrefslogtreecommitdiff
path: root/conf/user.go.bak
diff options
context:
space:
mode:
Diffstat (limited to 'conf/user.go.bak')
-rw-r--r--conf/user.go.bak127
1 files changed, 127 insertions, 0 deletions
diff --git a/conf/user.go.bak b/conf/user.go.bak
new file mode 100644
index 0000000..5ceaefb
--- /dev/null
+++ b/conf/user.go.bak
@@ -0,0 +1,127 @@
+package main
+
+import (
+ "os"
+ "fmt"
+ "log"
+ "strings"
+ "bufio"
+ "syscall"
+ "golang.org/x/term"
+ db "sanine.net/git/phlox/db"
+)
+
+
+func GetUsername(prompt string) (string, error) {
+ reader := bufio.NewReader(os.Stdin)
+
+ fmt.Print(prompt)
+ username, err := reader.ReadString('\n')
+ if err != nil {
+ return "", err
+ }
+
+ return strings.TrimSpace(username), nil
+}
+
+
+func GetPassword(prompt string) (string, error) {
+ fmt.Print(prompt)
+ bytes, err := term.ReadPassword(int(syscall.Stdin))
+ if err != nil {
+ return "", err
+ }
+
+ password := strings.TrimSpace(string(bytes))
+ return password, nil
+}
+
+
+func ConfirmPassword() string {
+ password, err := GetPassword("Enter Password: ")
+ if err != nil { log.Fatal(err) }
+ fmt.Println()
+ password1, err := GetPassword("Confirm Password: ")
+ if err != nil { log.Fatal(err) }
+ fmt.Println()
+ if password != password1 { log.Fatal("passwords do not match!") }
+ return password
+}
+
+
+func GetUser() db.User {
+ OpenDb()
+ username, err := GetUsername("Username: ")
+ if err != nil { log.Fatal(err) }
+ user, err := p.GetByUsername(username)
+ if err != nil { log.Fatal(err) }
+ return user
+}
+
+
+func CreateUser() *Command {
+ return &Command{
+ Flag: "create-user",
+ Usage: "create a new user",
+ Invoke: func() {
+ OpenDb()
+
+ username, err := GetUsername("Enter Username: ")
+ if err != nil { log.Fatal(err) }
+ password := ConfirmPassword()
+
+ _, err = p.CreateUser(username, password)
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("created user %v\n", username)
+ },
+ }
+}
+
+
+func DeleteUser() *Command {
+ return &Command{
+ Flag: "delete-user",
+ Usage: "delete an existing user",
+ Invoke: func() {
+ user := GetUser()
+ err := p.DeleteUser(user)
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("deleted user %v\n", user.Name)
+ },
+ }
+}
+
+
+func UpdatePassword() *Command {
+ return &Command{
+ Flag: "update-password",
+ Usage: "update a user's password",
+ Invoke: func() {
+ user := GetUser()
+ password := ConfirmPassword()
+ err := p.SetPassword(user, password)
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("updated password for user %v\n", user.Name)
+ },
+ }
+}
+
+
+func ListUsers() *Command {
+ return &Command{
+ Flag: "list-users",
+ Usage: "list all users",
+ Invoke: func() {
+ OpenDb()
+ users, err := p.AllUsers()
+ if err != nil { log.Fatal(err) }
+ fmt.Println("id\t\tname")
+ fmt.Println("========================================")
+ for _, user := range users {
+ fmt.Printf(
+ "%v\t\t%v\n", user.Id, user.Name,
+ )
+ }
+ },
+ }
+}