-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (89 loc) · 2.08 KB
/
main.go
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
arg := os.Args[1:]
if len(arg) < 1 {
help()
return
}
dbPath := "click.db"
if len(arg) == 2 {
dbPath = arg[1]
}
switch arg[0] {
case "createuser":
createUser(dbPath)
case "help":
help()
case "init":
initdb(dbPath)
case "run":
run(dbPath)
default:
fmt.Println("Invalid argument!")
help()
}
}
func createUser(dbPath string) {
DB := GetDB()
DB.Open(dbPath)
defer DB.Close()
user, pass := credentials()
CreateUser(user, pass)
if AuthenticateUser(user, pass) {
fmt.Println("User authenticated.")
} else {
fmt.Println("User not authenticated.")
}
}
func help() {
fmt.Println("createuser\tCreates a user")
fmt.Println("init \tInitializes the database")
fmt.Println("run \tRuns the server")
}
func initdb(dbPath string) {
DB := GetDB()
DB.Open(dbPath)
defer DB.Close()
DB.LogEntry("Nymble", BucketClick, 0)
DB.LogEntry("Nymble", BucketCount, 0)
DB.LogEntry("KTHB", BucketClick, 0)
DB.LogEntry("KTHB", BucketCount, 0)
DB.LogEntry("KTHB Main Hall", BucketClick, 0)
DB.LogEntry("KTHB Main Hall", BucketCount, 0)
DB.LogEntry("KTH Entré", BucketClick, 0)
DB.LogEntry("KTH Entré", BucketCount, 0)
DB.PrintToday("Nymble", BucketClick)
DB.PrintToday("Nymble", BucketCount)
DB.PrintToday("KTHB", BucketClick)
DB.PrintToday("KTHB", BucketCount)
DB.PrintToday("KTHB Main Hall", BucketClick)
DB.PrintToday("KTHB Main Hall", BucketCount)
DB.PrintToday("KTH Entré", BucketClick)
DB.PrintToday("KTH Entré", BucketCount)
}
func run(dbPath string) {
fmt.Println("Starting click server")
RunServer(dbPath)
}
func credentials() (string, string) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Username: ")
username, _ := reader.ReadString('\n')
fmt.Print("Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
fmt.Println("Error reading password", err)
return "", ""
}
fmt.Print("\n")
password := string(bytePassword)
return strings.TrimSpace(username), strings.TrimSpace(password)
}