-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from lestrrat/issue-79
Attempt to reorganize the conversion to protobuf
- Loading branch information
Showing
58 changed files
with
6,294 additions
and
4,888 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/NYTimes/openapi2proto" | ||
"github.com/NYTimes/openapi2proto/compiler" | ||
"github.com/NYTimes/openapi2proto/protobuf" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func main() { | ||
if err := _main(); err != nil { | ||
fmt.Fprintf(os.Stderr, "error: %s", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func _main() error { | ||
specPath := flag.String("spec", "../../spec.yaml", "location of the swagger spec file") | ||
annotate := flag.Bool("options", false, "include (google.api.http) options for grpc-gateway") | ||
annotate := flag.Bool("annotate", false, "include (google.api.http) options for grpc-gateway") | ||
outfile := flag.String("out", "", "the file to output the result to. Defaults to stdout if not set") | ||
indent := flag.Int("indent", 4, "number of spaces used for indentation") | ||
flag.Parse() | ||
|
||
api, err := openapi2proto.LoadDefinition(*specPath) | ||
if err != nil { | ||
log.Fatal("unable to load spec: ", err) | ||
} | ||
|
||
out, err := openapi2proto.GenerateProto(api, *annotate) | ||
if err != nil { | ||
log.Fatal("unable to generate protobuf: ", err) | ||
} | ||
|
||
var writer io.Writer | ||
writer = os.Stdout | ||
|
||
var dst io.Writer = os.Stdout | ||
if *outfile != "" { | ||
f, err := os.Create(*outfile) | ||
if err != nil { | ||
log.Fatalf("Can't open output file (%v): %v", outfile, err) | ||
return errors.Wrapf(err, `failed to open output file (%v)`, outfile) | ||
} | ||
defer f.Close() | ||
writer = f | ||
dst = f | ||
} | ||
|
||
var options []openapi2proto.Option | ||
var encoderOptions []protobuf.Option | ||
var compilerOptions []compiler.Option | ||
|
||
compilerOptions = append(compilerOptions, compiler.WithAnnotation(*annotate)) | ||
|
||
if *indent > 0 { | ||
var indentStr bytes.Buffer | ||
for i := 0; i < *indent; i++ { | ||
indentStr.WriteByte(' ') | ||
} | ||
encoderOptions = append(encoderOptions, protobuf.WithIndent(indentStr.String())) | ||
} | ||
|
||
if len(compilerOptions) > 0 { | ||
options = append(options, openapi2proto.WithCompilerOptions(compilerOptions...)) | ||
} | ||
|
||
if len(encoderOptions) > 0 { | ||
options = append(options, openapi2proto.WithEncoderOptions(encoderOptions...)) | ||
} | ||
|
||
_, err = writer.Write(out) | ||
if err != nil { | ||
log.Fatal("unable to write output to stdout: ", err) | ||
if err := openapi2proto.Transpile(dst, *specPath, options...); err != nil { | ||
return errors.Wrap(err, `failed to transpile`) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.