-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsshkey.go
73 lines (63 loc) · 1.31 KB
/
sshkey.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
package lobster
// database objects
type SSHKey struct {
ID int
UserID int
Name string
Key string
}
func keyListHelper(rows Rows) []*SSHKey {
defer rows.Close()
keys := make([]*SSHKey, 0)
for rows.Next() {
key := SSHKey{}
rows.Scan(&key.ID, &key.UserID, &key.Name, &key.Key)
keys = append(keys, &key)
}
return keys
}
const SSHKEY_QUERY = "SELECT id, user_id, name, val FROM sshkeys"
func keyListAll() []*SSHKey {
return keyListHelper(
db.Query(
SSHKEY_QUERY + " ORDER BY user_id, name",
),
)
}
func keyList(userID int) []*SSHKey {
return keyListHelper(
db.Query(
SSHKEY_QUERY+" WHERE user_id = ? ORDER BY name",
userID,
),
)
}
func keyGet(userID int, id int) *SSHKey {
keys := keyListHelper(
db.Query(
SSHKEY_QUERY+" WHERE id = ? AND user_id = ?",
id, userID,
),
)
if len(keys) == 1 {
return keys[0]
} else {
return nil
}
}
func keyAdd(userID int, name string, key string) (int, error) {
if name == "" {
return 0, L.Error("name_empty")
} else if key == "" {
return 0, L.Error("key_empty")
}
result := db.Exec(
"INSERT INTO sshkeys (user_id, name, val) VALUES (?, ?, ?)",
userID, name, key,
)
return result.LastInsertId(), nil
}
func keyRemove(userID int, id int) error {
db.Exec("DELETE FROM sshkeys WHERE user_id = ? AND id = ?", userID, id)
return nil
}