-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin development helpers and verbose flag for plugins. (#152)
* Add plugin development helpers and --verbose flag for plugins.
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 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
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2020 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// gnostic-plugin-request is a development tool that captures and optionally | ||
// displays the contents of the gnostic plugin interface. | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/golang/protobuf/jsonpb" | ||
"github.com/golang/protobuf/proto" | ||
openapiv2 "github.com/googleapis/gnostic/OpenAPIv2" | ||
openapiv3 "github.com/googleapis/gnostic/OpenAPIv3" | ||
plugins "github.com/googleapis/gnostic/plugins" | ||
surface "github.com/googleapis/gnostic/surface" | ||
) | ||
|
||
func main() { | ||
env, err := plugins.NewEnvironment() | ||
env.RespondAndExitIfError(err) | ||
|
||
if env.Verbose { | ||
for _, model := range env.Request.Models { | ||
log.Printf("model %s", model.TypeUrl) | ||
switch model.TypeUrl { | ||
case "openapi.v2.Document": | ||
document := &openapiv2.Document{} | ||
err = proto.Unmarshal(model.Value, document) | ||
if err == nil { | ||
log.Printf("%+v", document) | ||
} | ||
case "openapi.v3.Document": | ||
document := &openapiv3.Document{} | ||
err = proto.Unmarshal(model.Value, document) | ||
if err == nil { | ||
log.Printf("%+v", document) | ||
} | ||
case "surface.v1.Model": | ||
document := &surface.Model{} | ||
err = proto.Unmarshal(model.Value, document) | ||
if err == nil { | ||
log.Printf("%+v", document) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// export the plugin request as JSON | ||
{ | ||
file := &plugins.File{} | ||
file.Name = "plugin-request.json" | ||
m := jsonpb.Marshaler{Indent: " "} | ||
s, err := m.MarshalToString(env.Request) | ||
file.Data = []byte(s) | ||
env.RespondAndExitIfError(err) | ||
env.Response.Files = append(env.Response.Files, file) | ||
} | ||
// export the plugin request as binary protobuf | ||
{ | ||
file := &plugins.File{} | ||
file.Name = "plugin-request.pb" | ||
file.Data, err = proto.Marshal(env.Request) | ||
env.RespondAndExitIfError(err) | ||
env.Response.Files = append(env.Response.Files, file) | ||
} | ||
env.RespondAndExit() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2020 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// gnostic-process-plugin-response is a development tool that processes | ||
// the output of a gnostic plugin in the same way that it would be handled | ||
// by gnostic itself. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"github.com/golang/protobuf/proto" | ||
plugins "github.com/googleapis/gnostic/plugins" | ||
) | ||
|
||
func exitIfError(err error) { | ||
if err != nil { | ||
log.Printf("%+v", err) | ||
os.Exit(-1) | ||
} | ||
} | ||
|
||
func main() { | ||
output := flag.String("output", "-", "Output file or directory") | ||
flag.Parse() | ||
|
||
// Read the plugin response data from stdin. | ||
pluginData, err := ioutil.ReadAll(os.Stdin) | ||
exitIfError(err) | ||
response := &plugins.Response{} | ||
err = proto.Unmarshal(pluginData, response) | ||
exitIfError(err) | ||
|
||
// Handle the response in the standard (gnostic) way. | ||
err = plugins.HandleResponse(response, *output) | ||
exitIfError(err) | ||
} |