-
Notifications
You must be signed in to change notification settings - Fork 7
/
message.go
109 lines (98 loc) · 2.88 KB
/
message.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
package slack
import (
"encoding/json"
"os"
"strings"
"github.com/pkg/errors"
)
var (
// ErrFileNotFound indicates that an artifact was not found.
ErrFileNotFound = errors.New("file not found")
// ErrNotParsable indicates that an artifact could not be parsed against the
// artifact specification.
ErrNotParsable = errors.New("message not parsable")
// ErrUnknownFields indicates that an artifact contains an unknown field.
ErrUnknownFields = errors.New("message contains unknown fields")
MsgColorGreen = "#73BF69"
MsgColorYellow = "#FADE2A"
MsgColorRed = "#F2495C"
)
type Message struct {
Color string `json:"color,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"titleLink,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Service string `json:"service,omitempty"`
}
func Get(path string) (Message, error) {
m, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return Message{}, ErrFileNotFound
}
return Message{}, err
}
defer m.Close()
var message Message
decoder := json.NewDecoder(m)
decoder.DisallowUnknownFields()
err = decoder.Decode(&message)
if err != nil {
_, ok := err.(*json.SyntaxError)
if ok {
return Message{}, ErrNotParsable
}
// there is no other way to detect this error type unfortunately
// https://github.com/golang/go/blob/277609f844ed9254d25e975f7cf202d042beecc6/src/encoding/json/decode.go#L739
if strings.HasPrefix(err.Error(), "json: unknown field") {
return Message{}, errors.WithMessagef(ErrUnknownFields, "%v", err)
}
return Message{}, err
}
return message, nil
}
func Persist(path string, message Message) error {
s, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return errors.WithMessage(err, "open file")
}
defer s.Close()
err = s.Truncate(0)
if err != nil {
return errors.WithMessagef(err, "truncate file '%s'", path)
}
_, err = s.Seek(0, 0)
if err != nil {
return errors.WithMessagef(err, "reset seek on '%s'", path)
}
encode := json.NewEncoder(s)
encode.SetIndent("", " ")
err = encode.Encode(message)
if err != nil {
return errors.WithMessage(err, "encode spec to json")
}
return nil
}
// UpdateMessage updates the message in the file located at path by applying f
// on the contents.
//
// The stored Slack build message is updated accordingly.
func (c *Client) UpdateMessage(path string, f func(Message) Message) error {
m, err := Get(path)
if err != nil {
return errors.WithMessagef(err, "read artifact '%s'", path)
}
m = f(m)
m.Channel, m.Timestamp, err = c.UpdateSlackBuildStatus(m.Channel, m.Title, m.TitleLink, m.Text, m.Color, m.Timestamp)
if err != nil {
return err
}
// Persist back to the file
err = Persist(path, m)
if err != nil {
return errors.WithMessagef(err, "persist artifact to '%s'", path)
}
return nil
}