main.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import Foundation
  15. func printDocument(document:Openapi_V2_Document,
  16. name:String,
  17. version:String) -> String {
  18. var code = CodePrinter()
  19. code.print("READING \(name) (\(version))\n")
  20. code.print("Swagger: \(document.swagger)\n")
  21. code.print("Host: \(document.host)\n")
  22. code.print("BasePath: \(document.basePath)\n")
  23. if document.hasInfo {
  24. code.print("Info:\n")
  25. code.indent()
  26. if document.info.title != "" {
  27. code.print("Title: \(document.info.title)\n")
  28. }
  29. if document.info.description_p != "" {
  30. code.print("Description: \(document.info.description_p)\n")
  31. }
  32. if document.info.version != "" {
  33. code.print("Version: \(document.info.version)\n")
  34. }
  35. code.outdent()
  36. }
  37. code.print("Paths:\n")
  38. code.indent()
  39. for pair in document.paths.path {
  40. let v = pair.value
  41. if v.hasGet {
  42. code.print("GET \(pair.name)\n")
  43. }
  44. if v.hasPost {
  45. code.print("POST \(pair.name)\n")
  46. }
  47. }
  48. code.outdent()
  49. return code.content
  50. }
  51. func main() throws {
  52. var response = Openapi_Plugin_V1_Response()
  53. let rawRequest = try Stdin.readall()
  54. let request = try Openapi_Plugin_V1_Request(serializedData: rawRequest)
  55. let wrapper = request.wrapper
  56. let document = try Openapi_V2_Document(serializedData:wrapper.value)
  57. let report = printDocument(document:document, name:wrapper.name, version:wrapper.version)
  58. if let reportData = report.data(using:.utf8) {
  59. var file = Openapi_Plugin_V1_File()
  60. file.name = "report.txt"
  61. file.data = reportData
  62. response.files.append(file)
  63. }
  64. let serializedResponse = try response.serializedData()
  65. Stdout.write(bytes: serializedResponse)
  66. }
  67. try main()