-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
69 lines (54 loc) · 1.11 KB
/
common.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
package dev
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)
type DevAPIError struct {
msg string
code int
}
func (d *DevAPIError) Error() string {
return fmt.Sprintf("%s: %d", d.msg, d.code)
}
func assertError(err error) bool {
t := fmt.Sprintf("%T", err)
return t == "*dev.DevAPIError"
}
func extractDevError(resp *http.Response) error {
var v map[string]interface{}
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
return &DevAPIError{
msg: v["error"].(string),
code: int(v["status"].(float64)),
}
}
func parseUTCDate(t string) (time.Time, error) {
layout := "2006-01-02T15:04:05Z"
parsed, err := time.Parse(layout, t)
if err != nil {
return time.Time{}, err
}
return parsed, nil
}
func parseMarkdownFile(path string) (string, error) {
if !(strings.HasSuffix(path, ".md") || strings.HasSuffix(path, ".markdown")) {
return "", errors.New("file must be a markdown file")
}
byt, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(byt), nil
}