/
notify_adapters.go
64 lines (54 loc) · 1.3 KB
/
notify_adapters.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
package cmd
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"os"
"time"
"github.com/Shopify/themekit/src/cmdutil"
"github.com/Shopify/themekit/src/colors"
)
type notifyAdapter interface {
notify(*cmdutil.Ctx, string)
}
func newNotifyAdapter(notifyPath string) notifyAdapter {
if notifyPath == "" {
return &noopNotify{}
} else if u, err := url.Parse(notifyPath); err == nil && u.Scheme != "" && u.Host != "" {
return &urlNotify{
url: notifyPath,
client: http.Client{
Timeout: time.Second,
},
}
}
return &fileNotify{path: notifyPath}
}
type noopNotify struct{}
func (noop *noopNotify) notify(*cmdutil.Ctx, string) {}
type urlNotify struct {
url string
client http.Client
}
func (urlNote *urlNotify) notify(ctx *cmdutil.Ctx, path string) {
body, _ := json.Marshal(map[string]interface{}{"files": []string{path}})
resp, err := urlNote.client.Post(urlNote.url, "application/json", bytes.NewBuffer(body))
if err != nil {
ctx.Log.Printf(
`[%s] Error while notifying webhook "%s": %s`,
colors.Green(ctx.Env.Name),
colors.Blue(ctx.Env.Notify),
err,
)
} else {
resp.Body.Close()
}
}
type fileNotify struct {
path string
}
func (fileNote *fileNotify) notify(*cmdutil.Ctx, string) {
os.Create(fileNote.path)
os.Chtimes(fileNote.path, time.Now(), time.Now())
}