-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
142 lines (127 loc) · 4.09 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/foomo/gocontentful/config"
"github.com/foomo/gocontentful/erm"
)
var VERSION = "v1.0.26"
type contentfulRc struct {
ManagementToken string `json:"managementToken"`
}
var Usage = func() {
fmt.Printf("\nSYNOPSIS\n")
fmt.Printf(" gocontentful -spaceid SpaceID -cmakey CMAKey [-contenttypes firsttype,secondtype...lasttype] path/to/target/package\n\n")
flag.PrintDefaults()
fmt.Printf("\nNotes:\n")
fmt.Println("- The last segment of the path/to/target/package will be used as package name")
fmt.Println("- The -cmakey parameter can be omitted if you logged in with the Contentful CLI")
fmt.Println()
}
func usageError(comment string) {
fmt.Println("ERROR:", comment)
Usage()
os.Exit(1)
}
func fatal(infos ...interface{}) {
fmt.Println(infos...)
os.Exit(1)
}
func getCmaKeyFromRcFile() string {
currentUser, errGetUser := user.Current()
if errGetUser != nil {
return ""
}
contentfulRcBytes, errReadFile := os.ReadFile(currentUser.HomeDir + "/.contentfulrc.json")
if errReadFile != nil {
return ""
}
var contentfulConfig contentfulRc
errUnmarshal := json.Unmarshal(contentfulRcBytes, &contentfulConfig)
if errUnmarshal != nil {
return ""
}
return contentfulConfig.ManagementToken
}
func main() {
// Get parameters from cmd line flags
flagConfigFile := flag.String("configfile", "", "Full path to configuration file")
flagSpaceID := flag.String("spaceid", "", "Contentful space ID")
flagCMAKey := flag.String("cmakey", "", "[Optional] Contentful CMA key")
flagEnvironment := flag.String("environment", "", "[Optional] Contentful space environment")
flagGenerateFromExport := flag.String("exportfile", "", "Space export file to generate the API from")
flagContentTypes := flag.String("contenttypes", "", "[Optional] Content type IDs to parse, comma separated")
flagVersion := flag.Bool("version", false, "Print version and exit")
flagHelp := flag.Bool("help", false, "Print version and exit")
flag.Parse()
if *flagVersion {
fmt.Println(VERSION)
os.Exit(0)
}
if *flagHelp {
Usage()
os.Exit(0)
}
var conf *config.Config
var err error
if *flagConfigFile != "" {
conf, err = config.LoadConfigFromYAML(*flagConfigFile)
if err != nil {
fatal(err)
}
if conf.RequireVersion != "" && conf.RequireVersion != VERSION && conf.RequireVersion != strings.Trim(VERSION, "v") {
fatal("Required version mismatch. Want: " + conf.RequireVersion + " Have: " + VERSION)
}
} else {
conf = &config.Config{
SpaceID: *flagSpaceID,
Environment: *flagEnvironment,
ExportFile: *flagGenerateFromExport,
}
if *flagContentTypes != "" {
conf.ContentTypes = strings.Split(*flagContentTypes, ",")
}
}
cmaKey := *flagCMAKey
if cmaKey == "" && *flagGenerateFromExport == "" {
cmaKey = getCmaKeyFromRcFile()
}
if conf.ExportFile == "" && (conf.SpaceID == "" || cmaKey == "") ||
conf.ExportFile != "" && (conf.SpaceID != "" || cmaKey != "") {
usageError("Please provide either a Contentful Space ID and CMA access token or an export file name")
}
var path string
if len(flag.Args()) != 1 && conf.PathTargetPackage == "" {
usageError("Missing arg path/to/target/package")
}
if conf.PathTargetPackage != "" {
path = conf.PathTargetPackage
} else {
path = flag.Arg(0)
}
packageName := filepath.Base(path)
fmt.Println("output path:", path)
fmt.Println("packageName:", packageName)
matched, err := regexp.MatchString(`[a-z].{2,}`, packageName)
if !matched || err != nil {
usageError("Please specify the package name correctly (only small caps letters)")
}
fmt.Printf("Contentful API Generator %s starting...\n\n", VERSION)
var cleanContentTypes []string
if len(conf.ContentTypes) > 0 {
for _, contentType := range conf.ContentTypes {
cleanContentTypes = append(cleanContentTypes, strings.TrimSpace(contentType))
}
}
err = erm.GenerateAPI(filepath.Dir(path), packageName, conf.SpaceID, cmaKey, conf.Environment, conf.ExportFile, cleanContentTypes, VERSION)
if err != nil {
fatal("Something went horribly wrong...", err)
}
fmt.Println("ALL DONE!")
}