forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_error.go
115 lines (93 loc) · 2.23 KB
/
gen_error.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
// +build ignore
package main
import (
"bytes"
"go/format"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"text/template"
"time"
"gopkg.in/yaml.v2"
)
type CFCode int
type HTTPCode int
type Definition struct {
CFCode `yaml:"-"`
Name string `yaml:"name"`
HTTPCode `yaml:"http_code"`
Message string `yaml:"message"`
}
func main() {
const url = "https://raw.githubusercontent.com/cloudfoundry/cloud_controller_ng/master/vendor/errors/v2.yml"
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var m map[CFCode]Definition
if err := yaml.Unmarshal(body, &m); err != nil {
log.Fatal(err)
}
var definitions []Definition
for c, d := range m {
d.CFCode = c
definitions = append(definitions, d)
}
sort.Slice(definitions, func(i, j int) bool {
return definitions[i].CFCode < definitions[j].CFCode
})
buf := &bytes.Buffer{}
if err := packageTemplate.Execute(buf, struct {
Timestamp time.Time
Definitions []Definition
}{
Timestamp: time.Now(),
Definitions: definitions,
}); err != nil {
log.Fatal(err)
}
dst, err := format.Source(buf.Bytes())
if err != nil {
log.Printf("%s", buf.Bytes())
log.Fatal(err)
}
if err := ioutil.WriteFile("cf_error.go", dst, 0600); err != nil {
log.Fatal(err)
}
}
// destutter ensures that s does not end in "Error".
func destutter(s string) string {
return strings.TrimSuffix(s, "Error")
}
var packageTemplate = template.Must(template.New("").Funcs(template.FuncMap{
"destutter": destutter,
}).Parse(`
package cfclient
// Code generated by go generate. DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
import "github.com/pkg/errors"
{{- range .Definitions }}
{{$method := printf "Is%sError" (.Name | destutter) }}
// {{ $method }} returns a boolean indicating whether
// the error is known to report the Cloud Foundry error:
// - Cloud Foundry code: {{ .CFCode }}
// - HTTP code: {{ .HTTPCode }}
// - message: {{ printf "%q" .Message }}
func Is{{ .Name | destutter }}Error(err error) bool {
cause := errors.Cause(err)
cferr, ok := cause.(CloudFoundryError)
if !ok {
return false
}
return cferr.Code == {{ .CFCode }}
}
{{- end }}
`))