summaryrefslogtreecommitdiff
path: root/conf/user.go.bak
blob: 5ceaefb77660de4e361cfed4efe0efa6f4e58904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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,
				)
			}
		},
	}
}