This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
127 lines (107 loc) · 2.63 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/dave/jennifer/jen"
)
type validation struct {
LinkContentType []string `json:"linkContentType"`
}
type field struct {
Name string `json:"id"`
Type string `json:"type"`
LinkType string `json:"linkType"`
Localized bool `json:"localized"`
Required bool `json:"required"`
Disabled bool `json:"disabled"`
Items struct {
Type string `json:"type"`
LinkType string `json:"linkType"`
Validations []validation `json:"validations"`
}
Validations []validation `json:"validations"`
}
type contentfulModel struct {
Description string `json:"description"`
Name string `json:"name"`
DisplayField string `json:"displayField"`
Fields []field `json:"fields"`
Sys struct {
ID string
}
}
func (m contentfulModel) CapitalizedName() string {
return strings.ToUpper(m.Name[0:1]) + m.Name[1:]
}
func (m contentfulModel) DowncasedName() string {
return strings.ToLower(m.Name[0:1]) + m.Name[1:]
}
type contentModelResponse struct {
Total int
Skip int
Limit int
Items []contentfulModel `json:"items"`
}
var (
models []contentfulModel
certs string
)
// contentful api endpoints
const cdaEndpoint = "cdn.contentful.com"
const cmaEndpoint = "api.contentful.com"
const cpaEndpoint = "preview.contentful.com"
func init() {
var url = fmt.Sprintf("https://%s/spaces/%s/content_types?access_token=%s", cmaEndpoint, os.Getenv("CONTENTFUL_SPACE_ID"), os.Getenv("CONTENTFUL_AUTH_TOKEN"))
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
var data contentModelResponse
bs, _ := ioutil.ReadAll(resp.Body)
if err := json.Unmarshal(bs, &data); err != nil {
log.Fatal(err.Error())
}
if err := resp.Body.Close(); err != nil {
log.Fatal(err.Error())
}
models = data.Items
for i := range models {
models[i].Name = strings.Replace(models[i].Name, " ", "", -1)
}
certs, err = fetchCerts()
if err != nil {
log.Fatal(err)
}
}
func main() {
var pkg string
var output string
flag.StringVar(&pkg, "pkg", "contentful", "package name")
flag.StringVar(&output, "o", "contentful.go", "output file")
flag.Parse()
f := jen.NewFile(pkg)
generateDateType(f)
generateAssetType(f)
generateResponseTypes(f)
generateIteratorCacheType(f)
for _, model := range models {
generateModelType(f, model)
}
generateIteratorUtils(f)
generateContentClient(f)
generateManagementClient(f)
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
log.Fatal(err)
}
if err := f.Render(file); err != nil {
log.Fatal(err)
}
file.Close()
}