-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
145 lines (122 loc) · 4.41 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
143
144
145
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// contains the logic for the generator command the aim of this is to process
// the swagger definition that's obtained from Elastic Cloud and make a few
// changes in order for the SDK to be fully usable by Golang
//
// It will go over the swagger specification and set all of the boolean types
// to nullable using the vendor extension "x-nullable", this will cause any
// bool type to be converted to *bool in the Cloud SDK. This is required in
// order to fully use the V1 API.
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/go-openapi/spec"
"github.com/elastic/cloud-sdk-go/cmd/generator/cloudspec"
)
var (
sourceFile, destinationFile string
version, defFile string
defaultSourceFile = "apidocs.json"
defaultDestinationFile = "swagger.json"
errFileMustNotBeEmpty = errors.New("file must not be empty")
errFailedUnmarshalingSpec = errors.New("failed unmarshaling spec")
)
const tpl = `package {{.Package}}
// Version contains the ECE API version compatibility for the current
// auto-generated client and models. This needs to be updated every time a new
// "api/apidocs.json" file added and the client and models are re-generated.
// Even though the Bugfix version is specified, the general support statement is
// on the minor version, i.e: 2.4.2 means that all the 2.4 branch is supported,
// expecting some potentially unfixed bugs when ECE version is the same feature
// version bug higher bugfix version.
const Version = "{{.Version}}"
`
const (
_ = iota
codeCannotOpenFile
codeFileMustNotBeEmpty
codeFailedUnmarshalingSpec
codeFailedGeneratingVersionFile
codeFailedCreatingDestinationFile
codeFailedEncodingSpec
)
// Template is the template to generate the version file
type Template struct {
Package, Version string
}
func main() {
flag.StringVar(&sourceFile, "source", defaultSourceFile, "source file to parse")
flag.StringVar(&destinationFile, "destination", defaultDestinationFile, "destination file to save the result")
flag.StringVar(&defFile, "def-file", "", "destination file where to save the definition result")
flag.StringVar(&version, "version", "", "ECE version")
flag.Parse()
b, err := os.ReadFile(sourceFile)
exitOnError(err, codeCannotOpenFile)
if len(b) == 0 {
exitOnError(errFileMustNotBeEmpty, codeFileMustNotBeEmpty)
}
var cloudSpec *spec.Swagger
if err := json.Unmarshal(b, &cloudSpec); err != nil {
exitOnError(errFailedUnmarshalingSpec, codeFailedUnmarshalingSpec)
}
cloudSpec.Info.Version, cloudSpec.Info.Title = "v1", "rest"
commandFile, err := os.Create(defFile)
if err != nil {
println(err.Error())
commandFile = os.Stdout
}
defer commandFile.Close()
cloudspec.GetCommands(cloudSpec, commandFile, version)
exitOnError(
genVersionFile(filepath.Join("pkg", "api", "version.go"), version),
codeFailedGeneratingVersionFile,
)
// Modifies the spec mainly to make some properties of the swagger spec
// nullable or ommitable when they're empty to make it fully compatible
// for Go programatic consumption.
cloudspec.Modify(cloudSpec)
f, err := os.Create(destinationFile)
exitOnError(err, codeFailedCreatingDestinationFile)
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
exitOnError(enc.Encode(cloudSpec), codeFailedEncodingSpec)
}
func exitOnError(err error, code int) {
if err != nil {
fmt.Println(err)
os.Exit(code)
}
}
func genVersionFile(file, version string) error {
f, err := os.Create(file)
if err != nil {
return err
}
t, err := template.New("version").Parse(tpl)
if err != nil {
return err
}
return t.Execute(f, Template{Package: "api", Version: version})
}