/
registry.go
165 lines (136 loc) · 3.96 KB
/
registry.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package api
import (
"encoding/json"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/shipyard/shipyard"
)
func (a *Api) registries(w http.ResponseWriter, r *http.Request) {
registries, err := a.manager.Registries()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(registries); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *Api) addRegistry(w http.ResponseWriter, r *http.Request) {
var registry *shipyard.Registry
if err := json.NewDecoder(r.Body).Decode(®istry); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := a.manager.AddRegistry(registry); err != nil {
log.Errorf("error saving registry: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Infof("added registry: name=%s", registry.Name)
w.WriteHeader(http.StatusNoContent)
}
func (a *Api) registry(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
vars := mux.Vars(r)
name := vars["name"]
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(registry); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *Api) removeRegistry(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := a.manager.RemoveRegistry(registry); err != nil {
log.Errorf("error deleting registry: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *Api) repositories(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
vars := mux.Vars(r)
name := vars["name"]
if name != "" {
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
repos, err := registry.Repositories()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(repos); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (a *Api) repository(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
vars := mux.Vars(r)
name := vars["name"]
repoName := vars["repo"]
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
repo, err := registry.Repository(repoName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(repo); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *Api) deleteRepository(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
repoName := vars["repo"]
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := registry.DeleteRepository(repoName); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (a *Api) inspectRepository(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
repoName := vars["repo"]
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
repo, err := registry.Repository(repoName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(repo); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}