-
Notifications
You must be signed in to change notification settings - Fork 15
/
openapi.go
96 lines (78 loc) · 2.69 KB
/
openapi.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
package openapi3
import (
"os"
"github.com/jexia/semaphore/v2/cmd/semaphore/daemon/config"
"github.com/jexia/semaphore/v2/cmd/semaphore/daemon/providers"
print "github.com/jexia/semaphore/v2/cmd/semaphore/generate/printer"
"github.com/jexia/semaphore/v2/pkg/broker"
"github.com/jexia/semaphore/v2/pkg/broker/logger"
"github.com/jexia/semaphore/v2/pkg/functions"
"github.com/jexia/semaphore/v2/pkg/generators/openapi3"
"github.com/jexia/semaphore/v2/pkg/prettyerr"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
// Command represents the semaphore daemon command
var (
flags = new(config.Daemon)
Command = &cobra.Command{
Use: "openapi3",
Short: "Generates a openapi3 specification",
Long: `Generates a openapi3 specification.
*NOTE*: This is a experimental feature and not all features are supported yet.`,
RunE: run,
SilenceUsage: true,
}
printOptions = print.Options{
LineStart: "# ",
LineEnd: "\n",
}
includeNotReferenced bool
)
func init() {
Command.PersistentFlags().StringSliceVar(&flags.Protobuffers, "protobuffers", []string{}, "If set are all proto definitions found inside the given path passed as schema definitions, all proto definitions are also passed as imports")
Command.PersistentFlags().StringSliceVarP(&flags.Files, "file", "f", []string{"config.hcl"}, "Parses the given file as a definition file")
Command.PersistentFlags().StringVar(&flags.LogLevel, "level", "warn", "Global logging level, this value will override the defined log level inside the file definitions")
Command.PersistentFlags().Bool("include-not-referenced", false, "Include not referenced properties into the generated OpenAPI3 schema")
}
func run(cmd *cobra.Command, args []string) (err error) {
defer func() {
if err != nil {
err = prettyerr.StandardErr(err)
}
}()
options := openapi3.DefaultOption
if includeNotReferenced {
options = options | openapi3.IncludeNotReferenced
}
ctx := logger.WithLogger(broker.NewContext())
err = config.SetOptions(ctx, flags)
if err != nil {
return err
}
core, err := config.NewCore(ctx, flags)
if err != nil {
return err
}
provider, err := config.NewProviders(ctx, core, flags)
if err != nil {
return err
}
collection, err := providers.Resolve(ctx, functions.Collection{}, provider)
if err != nil {
return err
}
object, err := openapi3.Generate(collection.EndpointList, collection.FlowListInterface, options)
if err != nil {
return err
}
object.Info = openapi3.Info{
Title: "Public API",
Version: cmd.Version,
Description: "generated by Semaphore",
}
if err := print.DefaultHeader(cmd.Version).Print(os.Stdout, printOptions); err != nil {
return err
}
return yaml.NewEncoder(os.Stdout).Encode(object)
}