forked from harness/gitness
/
keys.go
116 lines (95 loc) · 2.47 KB
/
keys.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
package github
import (
"fmt"
)
type Key struct {
Id int `json:"id"`
Key string `json:"key"`
Url string `json:"url"`
Title string `json:"title"`
}
type KeyResource struct {
client *Client
}
// Gets a list of the keys associated with an account.
func (r *KeyResource) List() ([]*Key, error) {
keys := []*Key{}
const path = "/user/keys"
if err := r.client.do("GET", path, nil, &keys); err != nil {
return nil, err
}
return keys, nil
}
// Gets the key associated with the specified id.
func (r *KeyResource) Find(id int) (*Key, error) {
key := Key{}
path := fmt.Sprintf("/user/keys/%v", id)
if err := r.client.do("GET", path, nil, &key); err != nil {
return nil, err
}
return &key, nil
}
// Gets the key associated with specified title.
func (r *KeyResource) FindName(title string) (*Key, error) {
keys, err := r.List()
if err != nil {
return nil, err
}
for _, key := range keys {
if key.Title == title {
return key, nil
}
}
return nil, ErrNotFound
}
// Creates a key on the specified account. You must supply a valid key
// that is unique across the Github service.
func (r *KeyResource) Create(key, title string) (*Key, error) {
in := Key{ Title: title, Key: key }
out := Key{ }
const path = "/user/keys"
if err := r.client.do("POST", path, &in, &out); err != nil {
return nil, err
}
return &out, nil
}
// Updates a key on the specified account.
func (r *KeyResource) Update(key, title string, id int) (*Key, error) {
out := Key {}
in := Key {
Title : title,
Key : key,
}
path := fmt.Sprintf("/user/keys/%v", id)
if err := r.client.do("PATCH", path, &in, &out); err != nil {
return nil, err
}
return &out, nil
}
// Creates a key on the specified account, assuming it does not already
// exist in the system
func (r *KeyResource) CreateUpdate(key, title string) (*Key, error) {
if found, err := r.FindName(title); err == nil {
// if the public keys are different we should update
if found.Key != key {
return r.Update(key, title, found.Id)
}
// otherwise we should just return the key, since there
// is nothing to update
return found, nil
}
return r.Create(key, title)
}
// Deletes the key specified by the id value.
func (r *KeyResource) Delete(id int) error {
path := fmt.Sprintf("/user/keys/%v", id)
return r.client.do("DELETE", path, nil, nil)
}
// Deletes the named key.
func (r *KeyResource) DeleteName(title string) error {
key, err := r.FindName(title)
if err != nil {
return err
}
return r.Delete(key.Id)
}