Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 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 | |
| 15 | package openapiextension_v1 |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | |
| 22 | "github.com/golang/protobuf/proto" |
| 23 | "github.com/golang/protobuf/ptypes" |
| 24 | ) |
| 25 | |
| 26 | type documentHandler func(version string, extensionName string, document string) |
| 27 | type extensionHandler func(name string, yamlInput string) (bool, proto.Message, error) |
| 28 | |
| 29 | func forInputYamlFromOpenapic(handler documentHandler) { |
| 30 | data, err := ioutil.ReadAll(os.Stdin) |
| 31 | if err != nil { |
| 32 | fmt.Println("File error:", err.Error()) |
| 33 | os.Exit(1) |
| 34 | } |
| 35 | if len(data) == 0 { |
| 36 | fmt.Println("No input data.") |
| 37 | os.Exit(1) |
| 38 | } |
| 39 | request := &ExtensionHandlerRequest{} |
| 40 | err = proto.Unmarshal(data, request) |
| 41 | if err != nil { |
| 42 | fmt.Println("Input error:", err.Error()) |
| 43 | os.Exit(1) |
| 44 | } |
| 45 | handler(request.Wrapper.Version, request.Wrapper.ExtensionName, request.Wrapper.Yaml) |
| 46 | } |
| 47 | |
| 48 | // ProcessExtension calles the handler for a specified extension. |
| 49 | func ProcessExtension(handleExtension extensionHandler) { |
| 50 | response := &ExtensionHandlerResponse{} |
| 51 | forInputYamlFromOpenapic( |
| 52 | func(version string, extensionName string, yamlInput string) { |
| 53 | var newObject proto.Message |
| 54 | var err error |
| 55 | |
| 56 | handled, newObject, err := handleExtension(extensionName, yamlInput) |
| 57 | if !handled { |
| 58 | responseBytes, _ := proto.Marshal(response) |
| 59 | os.Stdout.Write(responseBytes) |
| 60 | os.Exit(0) |
| 61 | } |
| 62 | |
| 63 | // If we reach here, then the extension is handled |
| 64 | response.Handled = true |
| 65 | if err != nil { |
| 66 | response.Error = append(response.Error, err.Error()) |
| 67 | responseBytes, _ := proto.Marshal(response) |
| 68 | os.Stdout.Write(responseBytes) |
| 69 | os.Exit(0) |
| 70 | } |
| 71 | response.Value, err = ptypes.MarshalAny(newObject) |
| 72 | if err != nil { |
| 73 | response.Error = append(response.Error, err.Error()) |
| 74 | responseBytes, _ := proto.Marshal(response) |
| 75 | os.Stdout.Write(responseBytes) |
| 76 | os.Exit(0) |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | responseBytes, _ := proto.Marshal(response) |
| 81 | os.Stdout.Write(responseBytes) |
| 82 | } |