forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
64 lines (59 loc) · 1.61 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/go-openapi/spec"
"k8s.io/kube-openapi/pkg/common"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
)
// Generate OpenAPI spec definitions for Workflow Resource
func main() {
if len(os.Args) <= 1 {
log.Fatal("Supply a version")
}
version := os.Args[1]
oAPIDefs := wfv1.GetOpenAPIDefinitions(func(name string) spec.Ref {
return spec.MustCreateRef("#/definitions/" + common.EscapeJsonPointer(swaggify(name)))
})
defs := spec.Definitions{}
for defName, val := range oAPIDefs {
defs[swaggify(defName)] = val.Schema
}
swagger := spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Definitions: defs,
Paths: &spec.Paths{Paths: map[string]spec.PathItem{}},
Info: &spec.Info{
InfoProps: spec.InfoProps{
Title: "Argo",
Version: version,
},
},
},
}
jsonBytes, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(string(jsonBytes))
}
// swaggify converts the github package
// e.g.:
// github.com/argoproj/argo/pkg/apis/workflow/v1alpha1.Workflow
// to:
// io.argoproj.workflow.v1alpha1.Workflow
func swaggify(name string) string {
name = strings.Replace(name, "github.com/argoproj/argo/pkg/apis", "argoproj.io", -1)
parts := strings.Split(name, "/")
hostParts := strings.Split(parts[0], ".")
// reverses something like k8s.io to io.k8s
for i, j := 0, len(hostParts)-1; i < j; i, j = i+1, j-1 {
hostParts[i], hostParts[j] = hostParts[j], hostParts[i]
}
parts[0] = strings.Join(hostParts, ".")
return strings.Join(parts, ".")
}