/
commandline.go
70 lines (56 loc) · 1.61 KB
/
commandline.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
package main
import (
"fmt"
"github.com/mewbak/gopass"
"github.com/scanbadge/api/configuration"
"github.com/scanbadge/api/models"
"github.com/scanbadge/api/utility"
)
// addUser adds a new user based on the provided stdin. The provided password will be hashed using bcrypt.
// The following details are required: username, password, email, first name, last name.
func addUser() bool {
var user models.User
fmt.Println("Adding new user to ScanBadge API...")
fmt.Println("Username:")
fmt.Scanln(&user.Username)
if user.Username == "" {
fmt.Println("Username is required")
return false
}
password, err := gopass.GetPass("Password:\n")
if err != nil {
fmt.Println(err)
return false
}
if password == "" || len(password) < 8 || len(password) > 512 {
fmt.Println("Password must be at least 8 characters long and cannot exceed 512 characters")
return false
}
user.Password = utility.HashPassword(password)
fmt.Println("Email:")
fmt.Scanln(&user.Email)
if user.Email == "" {
fmt.Println("Email is required")
return false
}
fmt.Println("First name:")
fmt.Scanln(&user.FirstName)
if user.FirstName == "" {
fmt.Println("First name is required")
return false
}
fmt.Println("Last name:")
fmt.Scanln(&user.LastName)
if user.LastName == "" {
fmt.Println("Last name is required")
return false
}
// Everything seems to be all right, attempt to insert new user to database.
err = configuration.Dbmap.Insert(&user)
if err == nil {
fmt.Println(fmt.Sprintf("Successfully added new user '%s'", user.Username))
} else {
fmt.Println("Cannot add new user due to " + err.Error())
}
return err == nil
}