-
Notifications
You must be signed in to change notification settings - Fork 1
/
serve.go
108 lines (95 loc) · 2.63 KB
/
serve.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/binxio/cru/ref"
"gopkg.in/src-d/go-git.v4/plumbing"
)
type ContainerReferenceUpdateRequest struct {
CommitMessage string `json:"commit-message"`
ImageReferences []string `json:"image-references"`
MatchingTag bool `json:"matching-tag,omitEmpty"`
}
type ContainerReferenceUpdateResponse struct {
GitURL string `json:"git-url,omitempty"`
Files []string `json:"files,omitempty"`
Hash string `json:"commit-sha,omitempty"`
}
func (c Cru) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var hash plumbing.Hash
var request ContainerReferenceUpdateRequest
var response ContainerReferenceUpdateResponse
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
c.CommitMsg = request.CommitMessage
if c.CommitMsg == "" {
http.Error(w, "commit message is empty", http.StatusBadRequest)
return
}
c.imageRefs = make(ref.ContainerImageReferences, 0)
for _, r := range request.ImageReferences {
r, err := ref.NewContainerImageReference(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
c.imageRefs = append(c.imageRefs, *r)
}
if len(c.imageRefs) == 0 {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err = c.ConnectToRepository(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
c.MatchingTag = request.MatchingTag
if err = c.Walk(Update); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(c.updatedFiles) > 0 {
log.Printf("INFO: updated a total of %d files", len(c.updatedFiles))
if hash, err = c.Commit(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = c.Push(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response = ContainerReferenceUpdateResponse{c.Url, c.updatedFiles, hash.String()}
} else {
log.Println("INFO: no files were updated by cru")
response = ContainerReferenceUpdateResponse{GitURL: c.Url}
}
if body, err := json.Marshal(response); err == nil {
w.Header().Set("Content-Type", "application/json")
w.Write(body)
return
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (c *Cru) ListenAndServe() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
if c.Port == "" {
c.Port = os.Getenv("PORT")
}
if c.Port == "" {
c.Port = "8080"
}
log.Printf("Listening on port %s", c.Port)
if err := http.ListenAndServe(":"+c.Port, c); err != nil {
log.Fatal(err)
}
}