Skip to content

Commit

Permalink
Merge pull request #19 from guptasu/newFlagForOutPath
Browse files Browse the repository at this point in the history
Add flags for output file paths.
  • Loading branch information
timburks committed Dec 19, 2016
2 parents 32dabe6 + 67a0632 commit 9539760
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ and the [Google Protocol Buffer Compiler](https://github.com/google/protobuf).
1. Get this package by downloading it with `go get`.

go get github.com/googleapis/openapi-compiler/openapic
2. [Optional] Build and run the compiler generator.
This uses the OpenAPI JSON schema to generate a Protocol Buffer language file
that describes the OpenAPI specification and a Go-language file of code that
Expand All @@ -54,7 +54,7 @@ buffers. Pre-generated versions of these files are in the OpenAPIv2 directory.

cd $GOPATH/src/github.com/googleapis/openapi-compiler/generator
go build
cd ..
cd ..
./generator/generator

3. [Optional] Generate protocol buffer support code.
Expand All @@ -72,13 +72,13 @@ You can get protoc [here](https://github.com/google/protobuf).
5. Run the OpenAPI compiler. This will create a file called "petstore.pb" that contains a binary
Protocol Buffer description of a sample API.

openapic -in examples/petstore.json -pb
openapic -pb_out petstore.pb examples/petstore.json

6. You can also compile files that you specify with a URL. Here's another way to compile the previous
example. This time we're creating "petstore.text", which creates a textual representation of the
Protocol Buffer description. This is mainly for use in testing and debugging.

openapic -in https://raw.githubusercontent.com/googleapis/openapi-compiler/master/examples/petstore.json -text
openapic -text_out petstore.pb.text https://raw.githubusercontent.com/googleapis/openapi-compiler/master/examples/petstore.json

7. For a sample application, see apps/report.

Expand Down
59 changes: 38 additions & 21 deletions openapic/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,55 @@ import (
)

func main() {
var input = flag.String("in", "", "OpenAPI source file to read")
var rawInput = flag.Bool("raw", false, "Output the raw json input")
var textProtobuf = flag.Bool("text", false, "Output a text protobuf representation")
var jsonProtobuf = flag.Bool("json", false, "Output a json protobuf representation")
var binaryProtobuf = flag.Bool("pb", false, "Output a binary protobuf representation")
var textProtoFileName = flag.String("text_out", "", "Output location for writing the text proto")
var jsonProtoFileName = flag.String("json_out", "", "Output location for writing the json proto")
var binaryProtoFileName = flag.String("pb_out", "", "Output location for writing the binary proto")
var keepReferences = flag.Bool("keep-refs", false, "Disable resolution of $ref references")
var logErrors = flag.Bool("errors", false, "Log errors to a file")

flag.Parse()

if *input == "" {
flag.Usage = func() {
fmt.Printf("Usage: openapic [OPTION] OPENAPI_FILE\n")
fmt.Printf("OPENAPI_FILE is the path to the input openapi " +
"file to parse.\n")
fmt.Printf("Output is generated based on the options given:\n")
flag.PrintDefaults()
}

flag.Parse()

var input string

if len(flag.Args()) == 1 {
input = flag.Arg(0)
} else {
flag.Usage()
return
}

fmt.Printf("Compiling %s (%s)\n", *input, openapi_v2.Version())
if *textProtoFileName == "" && *jsonProtoFileName == "" && *binaryProtoFileName == "" {
fmt.Printf("Missing output directives.\n")
flag.Usage()
return
}

fmt.Printf("Compiling %s (%s)\n", input, openapi_v2.Version())

raw, err := compiler.ReadFile(*input)
raw, err := compiler.ReadFile(input)
if err != nil {
fmt.Printf("Error: No Specification\n%+v\n", err)
os.Exit(-1)
}

if *rawInput {
rawDescription := compiler.DescribeMap(raw, "")
rawFileName := strings.TrimSuffix(path.Base(*input), path.Ext(*input)) + ".raw"
rawFileName := strings.TrimSuffix(path.Base(input), path.Ext(input)) + ".raw"
ioutil.WriteFile(rawFileName, []byte(rawDescription), 0644)
}

errorFileName := strings.TrimSuffix(path.Base(*input), path.Ext(*input)) + ".errors"
errorFileName := strings.TrimSuffix(path.Base(input), path.Ext(input)) + ".errors"

document, err := openapi_v2.NewDocument(raw, compiler.NewContext("$root", nil))
if err != nil {
Expand All @@ -72,7 +91,7 @@ func main() {
}

if !*keepReferences {
_, err = document.ResolveReferences(*input)
_, err = document.ResolveReferences(input)
if err != nil {
fmt.Printf("%+v\n", err)
if *logErrors {
Expand All @@ -82,20 +101,18 @@ func main() {
}
}

if *textProtobuf {
textProtoFileName := strings.TrimSuffix(path.Base(*input), path.Ext(*input)) + ".text"
ioutil.WriteFile(textProtoFileName, []byte(proto.MarshalTextString(document)), 0644)
if *textProtoFileName != "" {
ioutil.WriteFile(*textProtoFileName, []byte(proto.MarshalTextString(document)), 0644)
fmt.Printf("Output protobuf textfile: %s\n", *textProtoFileName)
}

if *jsonProtobuf {
jsonProtoFileName := strings.TrimSuffix(path.Base(*input), path.Ext(*input)) + ".json"
if *jsonProtoFileName != "" {
jsonBytes, _ := json.Marshal(document)
ioutil.WriteFile(jsonProtoFileName, jsonBytes, 0644)
ioutil.WriteFile(*jsonProtoFileName, jsonBytes, 0644)
fmt.Printf("Output protobuf json file: %s\n", *jsonProtoFileName)
}

if *binaryProtobuf {
binaryProtoFileName := strings.TrimSuffix(path.Base(*input), path.Ext(*input)) + ".pb"
if *binaryProtoFileName != "" {
protoBytes, _ := proto.Marshal(document)
ioutil.WriteFile(binaryProtoFileName, protoBytes, 0644)
ioutil.WriteFile(*binaryProtoFileName, protoBytes, 0644)
fmt.Printf("Output protobuf binary file: %s\n", *binaryProtoFileName)
}
}
2 changes: 1 addition & 1 deletion openapic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func test_compiler(t *testing.T, input_file string, reference_file string, expec
t.FailNow()
}
// run the compiler
err = exec.Command("openapic", "-in", input_file, "-text", "-errors").Run()
err = exec.Command("openapic", "-text_out", output_file, "-errors", input_file).Run()
if err != nil && !expect_errors {
t.Logf("Compile failed: %+v", err)
t.FailNow()
Expand Down

0 comments on commit 9539760

Please sign in to comment.