-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (54 loc) · 3.81 KB
/
main.go
File metadata and controls
64 lines (54 loc) · 3.81 KB
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 (
"flag"
"fmt"
"log/slog"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
"github.com/pubgo/protoc-gen-openapi/internal/converter"
"github.com/pubgo/protoc-gen-openapi/internal/converter/options"
_ "github.com/pubgo/protoc-gen-openapi/internal/logging"
"github.com/pubgo/protoc-gen-openapi/version"
)
var conf = options.Config{
AllowGetFlag: flag.Bool("allow-get", false, "For methods that have `IdempotencyLevel=IDEMPOTENT`, this option will generate HTTP `GET` requests instead of `POST`."),
BaseFlag: flag.String("base", "", "The path to a base OpenAPI file to populate fields that this tool doesn't populate."),
ContentTypesFlag: flag.String("content-types", "json;proto", "Semicolon-separated content types to generate requests/responses"),
DebugFlag: flag.Bool("debug", false, "Emit debug logs"),
FormatFlag: flag.String("format", "yaml", "Which format to use for the OpenAPI file, defaults to `yaml`."),
IgnoreGoogleApiHttpFlag: flag.Bool("ignore-googleapi-http", false, "Ignore `google.api.http` options on methods when generating openapi specs"),
IncludeNumberEnumValuesFlag: flag.Bool("include-number-enum-values", false, "Include number enum values beside the string versions, defaults to only showing strings"),
PathFlag: flag.String("path", "", "Output filepath, defaults to per-protoFile output if not given."),
PathPrefixFlag: flag.String("path-prefix", "", "Prefixes the given string to the beginning of each HTTP path."),
ProtoFlag: flag.Bool("proto", false, "Generate requests/responses with the protobuf content type"),
ServicesFlag: flag.String("services", "", "Filter which services have OpenAPI spec generated. The default is all services. Comma-separated, uses the full path of the service \"[package name].[service name]\""),
TrimUnusedTypesFlag: flag.Bool("trim-unused-types", false, "Remove types that aren't references from any method request or response."),
WithProtoAnnotationsFlag: flag.Bool("with-proto-annotations", false, "Add protobuf type annotations to the end of descriptions so users know the protobuf type that the field converts to."),
WithProtoNamesFlag: flag.Bool("with-proto-names", false, "Use protobuf field names instead of the camelCase JSON names for property names."),
WithStreamingFlag: flag.Bool("with-streaming", false, "Generate OpenAPI for client/server/bidirectional streaming RPCs (can be messy)."),
FullyQualifiedMessageNamesFlag: flag.Bool("fully-qualified-message-names", false, "Use the full path for message types: {pkg}.{name} instead of just the name. This is helpful if you are mixing types from multiple services."),
WithServiceDescriptions: flag.Bool("with-service-descriptions", false, "set to true will cause service names and their comments to be added to the end of info.description"),
}
var showVersion = flag.Bool("version", false, "print the version and exit")
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("protoc-gen-openapi %s\n", version.FullVersion())
return
}
var flagSet = func(name, value string) error {
err := flag.CommandLine.Set(name, value)
if err != nil {
slog.Info("openapi flags set error", name, value, "err", err)
}
return nil
}
var runPlugin = func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
gen.SupportedEditionsMinimum = descriptorpb.Edition_EDITION_PROTO2
gen.SupportedEditionsMaximum = descriptorpb.Edition_EDITION_2024
return converter.ConvertV1(gen, conf)
}
protogen.Options{ParamFunc: flagSet}.Run(runPlugin)
}