-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
postmortem.go
199 lines (160 loc) · 4.03 KB
/
postmortem.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package postmortems
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"time"
"github.com/gernest/front"
"github.com/goccy/go-yaml"
guuid "github.com/google/uuid"
"github.com/icco/gutil/logging"
)
// Postmortem is a structural representation of a postmortem summary and its
// metadata.
type Postmortem struct {
UUID string `yaml:"uuid"`
URL string `yaml:"url"`
StartTime time.Time `yaml:"start_time,omitempty"`
EndTime time.Time `yaml:"end_time,omitempty"`
Categories []string `yaml:"categories"`
Company string `yaml:"company"`
Product string `yaml:"product"`
Description string `yaml:"-"`
}
var (
// Categories is a whitelist of valid categories that a postmortem can have.
Categories = []string{
"automation",
"cascading-failure",
"cloud",
"config-change",
"postmortem",
"hardware",
"security",
"time",
"undescriptive",
}
// Service defines the service this runs in on GCP.
Service = "postmortems"
// GCPProject defines the project this code runs in and should log to.
GCPProject = "icco-cloud"
log = logging.Must(logging.NewLogger(Service))
)
// Parse turns an io stream into a Postmortem type.
func Parse(f io.Reader) (*Postmortem, error) {
p := &Postmortem{}
m := front.NewMatter()
m.Handle("---", front.YAMLHandler)
fm, body, err := m.Parse(f)
if err != nil {
return nil, err
}
if uuid, ok := fm["uuid"].(string); ok {
p.UUID = uuid
}
if startTime, ok := fm["start_time"].(time.Time); ok {
p.StartTime = startTime
}
if endTime, ok := fm["end_time"].(time.Time); ok {
p.EndTime = endTime
}
if url, ok := fm["url"].(string); ok {
p.URL = url
}
if company, ok := fm["company"].(string); ok {
p.Company = company
}
if product, ok := fm["product"].(string); ok {
p.Product = product
}
if cats, ok := fm["categories"].([]interface{}); ok {
for _, c := range cats {
if cat, ok := c.(string); ok {
p.Categories = append(p.Categories, cat)
}
}
}
p.Description = body
return p, nil
}
// GenerateJSON outputs all content in JSON for parsing by our website.
func GenerateJSON(d string) error {
baseDir := "./output"
err := os.MkdirAll(baseDir, os.ModePerm)
if err != nil {
return err
}
fp := filepath.Join(baseDir, "categories.json")
j, err := json.Marshal(Categories)
if err != nil {
return err
}
err = ioutil.WriteFile(fp, j, 0644)
if err != nil {
return err
}
return filepath.Walk(d, func(path string, info os.FileInfo, err error) error {
// Failed to open path
if err != nil {
return err
}
if !info.IsDir() {
f, err := os.Open(path)
if err != nil {
return err
}
fName := filepath.Base(path)
extName := filepath.Ext(path)
id := fName[:len(fName)-len(extName)]
p, err := Parse(f)
if err != nil {
return err
}
fp := filepath.Join(baseDir, fmt.Sprintf("%s.json", id))
j, err := json.Marshal(p)
if err != nil {
return err
}
err = ioutil.WriteFile(fp, j, 0644)
if err != nil {
return err
}
}
return nil
})
}
// ToYaml transforms a postmortem into yaml for the frontmatter.
func ToYaml(pm *Postmortem) (string, error) {
bytes, err := yaml.Marshal(pm)
return string(bytes), err
}
// New generates a new postmortem with a fresh uuid.
func New() *Postmortem {
id := guuid.New()
return &Postmortem{UUID: id.String()}
}
// Save takes the in-memory representation of the postmortem file and stores it
// in a file.
func (pm *Postmortem) Save(dir string) error {
var data bytes.Buffer
fm := template.New("PostmortemTemplate")
fm = fm.Funcs(template.FuncMap{"yaml": ToYaml})
fm, err := fm.Parse(bodyTmpl)
if err != nil {
return err
}
if err := fm.Execute(&data, pm); err != nil {
return fmt.Errorf("error executing template: %w", err)
}
// Write postmortem data from memory to file.
if err := ioutil.WriteFile(filepath.Join(dir, pm.UUID+".md"), data.Bytes(), 0644); err != nil {
return fmt.Errorf("error writing file: %w", err)
}
log.Debugw("saved pm", "pm", pm, "data", data.String())
return nil
}