-
Notifications
You must be signed in to change notification settings - Fork 2
/
users.go
120 lines (91 loc) · 2.73 KB
/
users.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"errors"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
)
var usersCommand *kingpin.CmdClause
var addUserCommand *kingpin.CmdClause
var userName *string
var userKeyFile *string
var removeUserCommand *kingpin.CmdClause
var listUsersCommand *kingpin.CmdClause
func setupUsersCommand(app *kingpin.Application) {
usersCommand = app.Command("users", "Manage users")
userName = usersCommand.Flag("name", "Name of user").Short('u').Default(configDefaults[keyUsername]).String()
userKeyFile = usersCommand.Flag("key-file", "Filename of user's public key").Short('k').Default(configDefaults[keyPublicKeyFile]).String()
addUserCommand = usersCommand.Command("list", "List users")
addUserCommand = usersCommand.Command("add", "Add a user")
removeUserCommand = usersCommand.Command("remove", "Remove a user")
}
func handleUsersCommand(commands []string) error {
if len(commands) < 2 {
return errors.New("No subcommand found for users command")
}
switch commands[1] {
case "list":
return handleListUsersCommand(commands)
case "add":
return handleAddUserCommand(commands)
case "remove":
return handleRemoveUserCommand(commands)
default:
return errors.New(fmt.Sprintf("Users subcommand not supported : %s", commands[1]))
}
}
func handleListUsersCommand(commands []string) error {
mealieCryptFile, err := readFile(filename, true)
if err != nil {
return err
}
for username, _ := range mealieCryptFile.Users {
fmt.Printf("%s\n", username)
}
return nil
}
func checkUsername() error {
return checkParam(*userName, "^.+$", "Username must not be empty")
}
func handleAddUserCommand(commands []string) error {
var user MealieCryptUser
mealieCryptFile, err := readFile(filename, true)
if err != nil {
return err
}
err = checkUsername()
if err != nil {
return err
}
keyValue, err := readPublicKey(userKeyFile)
if err != nil {
return err
}
_, found := mealieCryptFile.Users[*userName]
if found {
return errors.New(fmt.Sprintf("User already exists : %s", *userName))
}
user.PublicKey = keyValue
user.Comment = *comment
mealieCryptFile.Users[*userName] = user
return writeFile(filename, false, mealieCryptFile)
}
func handleRemoveUserCommand(commands []string) error {
mealieCryptFile, err := readFile(filename, true)
if err != nil {
return err
}
err = checkUsername()
if err != nil {
return err
}
//Remove user from users
delete(mealieCryptFile.Users, *userName)
//Remove user from groups
for groupName, _ := range mealieCryptFile.Groups {
delete(mealieCryptFile.Groups[groupName].Keys, *userName)
if len(mealieCryptFile.Groups[groupName].Keys) == 0 {
return errors.New(fmt.Sprintf("Cannot remove last user from group : %s", groupName))
}
}
return writeFile(filename, false, mealieCryptFile)
}