main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // gnostic_go_sample is a sample Gnostic plugin written in Go.
  15. package main
  16. import (
  17. "io/ioutil"
  18. "os"
  19. "github.com/golang/protobuf/proto"
  20. "github.com/googleapis/gnostic/printer"
  21. openapi "github.com/googleapis/gnostic/OpenAPIv2"
  22. plugins "github.com/googleapis/gnostic/plugins"
  23. )
  24. // generate a simple report of an OpenAPI document's contents
  25. func printDocument(code *printer.Code, document *openapi.Document) {
  26. code.Print("Swagger: %+v", document.Swagger)
  27. code.Print("Host: %+v", document.Host)
  28. code.Print("BasePath: %+v", document.BasePath)
  29. if document.Info != nil {
  30. code.Print("Info:")
  31. code.Indent()
  32. if document.Info.Title != "" {
  33. code.Print("Title: %s", document.Info.Title)
  34. }
  35. if document.Info.Description != "" {
  36. code.Print("Description: %s", document.Info.Description)
  37. }
  38. if document.Info.Version != "" {
  39. code.Print("Version: %s", document.Info.Version)
  40. }
  41. code.Outdent()
  42. }
  43. code.Print("Paths:")
  44. code.Indent()
  45. for _, pair := range document.Paths.Path {
  46. v := pair.Value
  47. if v.Get != nil {
  48. code.Print("GET %+v", pair.Name)
  49. }
  50. if v.Post != nil {
  51. code.Print("POST %+v", pair.Name)
  52. }
  53. }
  54. code.Outdent()
  55. }
  56. // record an error, then serialize and return the response
  57. func sendAndExitIfError(err error, response *plugins.Response) {
  58. if err != nil {
  59. response.Errors = append(response.Errors, err.Error())
  60. sendAndExit(response)
  61. }
  62. }
  63. // serialize and return the response
  64. func sendAndExit(response *plugins.Response) {
  65. responseBytes, _ := proto.Marshal(response)
  66. os.Stdout.Write(responseBytes)
  67. os.Exit(0)
  68. }
  69. func main() {
  70. // initialize the response
  71. response := &plugins.Response{}
  72. // read and deserialize the request
  73. data, err := ioutil.ReadAll(os.Stdin)
  74. sendAndExitIfError(err, response)
  75. request := &plugins.Request{}
  76. err = proto.Unmarshal(data, request)
  77. sendAndExitIfError(err, response)
  78. wrapper := request.Wrapper
  79. document := &openapi.Document{}
  80. err = proto.Unmarshal(wrapper.Value, document)
  81. sendAndExitIfError(err, response)
  82. // generate report
  83. code := &printer.Code{}
  84. code.Print("READING %s (%s)", wrapper.Name, wrapper.Version)
  85. printDocument(code, document)
  86. file := &plugins.File{}
  87. file.Name = "report.txt"
  88. file.Data = []byte(code.String())
  89. response.Files = append(response.Files, file)
  90. // send with success
  91. sendAndExit(response)
  92. }