github.com/sanposhiho/openapi2proto@v0.0.0-20230521044535-d1080a134e37/cmd/openapi2proto/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  
    10  	"github.com/sanposhiho/openapi2proto"
    11  	"github.com/sanposhiho/openapi2proto/compiler"
    12  	"github.com/sanposhiho/openapi2proto/protobuf"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func main() {
    17  	if err := _main(); err != nil {
    18  		fmt.Fprintf(os.Stderr, "error: %s", err)
    19  		os.Exit(1)
    20  	}
    21  }
    22  
    23  func _main() error {
    24  	specPath := flag.String("spec", "../../spec.yaml", "location of the swagger spec file")
    25  	annotate := flag.Bool("annotate", false, "include (google.api.http) options for grpc-gateway. Defaults to false if not set")
    26  	outfile := flag.String("out", "", "the file to output the result to. Defaults to stdout if not set")
    27  	indent := flag.Int("indent", 4, "number of spaces used for indentation")
    28  	skipRpcs := flag.Bool("skip-rpcs", false, "skip rpc code generation. Defaults to false if not set")
    29  	skipDeprecatedRpcs := flag.Bool("skip-deprecated-rpcs", false, "skip rpc code generation for endpoints marked as deprecated. Defaults to false if not set")
    30  	namespaceEnums := flag.Bool("namespace-enums", false, "prefix enum values with the enum name to prevent namespace conflicts. Defaults to false if not set")
    31  	wrapPrimitives := flag.Bool("wrap-primitives", false, "specify primitive values using their wrapper message types instead of their scalar types. Defaults to false if not set")
    32  	addAutogeneratedComment := flag.Bool("add-autogenerated-comment", false, "add comment on top of the generated protos that those files are autogenerated and should not be modified. Defaults to false if not set")
    33  	flag.Parse()
    34  
    35  	var dst io.Writer = os.Stdout
    36  	if *outfile != "" {
    37  		f, err := os.Create(*outfile)
    38  		if err != nil {
    39  			return errors.Wrapf(err, `failed to open output file (%v)`, outfile)
    40  		}
    41  		defer f.Close()
    42  		dst = f
    43  	}
    44  
    45  	var options []openapi2proto.Option
    46  	var encoderOptions []protobuf.Option
    47  	var compilerOptions []compiler.Option
    48  
    49  	compilerOptions = append(compilerOptions, compiler.WithAnnotation(*annotate))
    50  	compilerOptions = append(compilerOptions, compiler.WithSkipRpcs(*skipRpcs))
    51  	compilerOptions = append(compilerOptions, compiler.WithSkipDeprecatedRpcs(*skipDeprecatedRpcs))
    52  	compilerOptions = append(compilerOptions, compiler.WithPrefixEnums(*namespaceEnums))
    53  	compilerOptions = append(compilerOptions, compiler.WithWrapPrimitives(*wrapPrimitives))
    54  
    55  	encoderOptions = append(encoderOptions, protobuf.WithAutogeneratedComment(*addAutogeneratedComment))
    56  
    57  	if *indent > 0 {
    58  		var indentStr bytes.Buffer
    59  		for i := 0; i < *indent; i++ {
    60  			indentStr.WriteByte(' ')
    61  		}
    62  		encoderOptions = append(encoderOptions, protobuf.WithIndent(indentStr.String()))
    63  	}
    64  
    65  	if len(compilerOptions) > 0 {
    66  		options = append(options, openapi2proto.WithCompilerOptions(compilerOptions...))
    67  	}
    68  
    69  	if len(encoderOptions) > 0 {
    70  		options = append(options, openapi2proto.WithEncoderOptions(encoderOptions...))
    71  	}
    72  
    73  	if err := openapi2proto.Transpile(dst, *specPath, options...); err != nil {
    74  		return errors.Wrap(err, `failed to transpile`)
    75  	}
    76  	return nil
    77  }