| 1 | /* |
| 2 | Copyright 2018 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "io" |
| 21 | "log" |
| 22 | "os" |
| 23 | |
| 24 | openapi_v2 "github.com/google/gnostic-models/openapiv2" |
| 25 | yaml "sigs.k8s.io/yaml/goyaml.v2" |
| 26 | |
| 27 | "k8s.io/kube-openapi/pkg/schemaconv" |
| 28 | "k8s.io/kube-openapi/pkg/util/proto" |
| 29 | ) |
| 30 | |
| 31 | func main() { |
| 32 | if len(os.Args) != 1 { |
| 33 | log.Fatal("this program takes input on stdin and writes output to stdout.") |
| 34 | } |
| 35 | |
| 36 | input, err := io.ReadAll(os.Stdin) |
| 37 | if err != nil { |
| 38 | log.Fatalf("error reading stdin: %v", err) |
| 39 | } |
| 40 | |
| 41 | document, err := openapi_v2.ParseDocument(input) |
| 42 | if err != nil { |
| 43 | log.Fatalf("error interpreting stdin: %v", err) |
| 44 | } |
| 45 | |
| 46 | models, err := proto.NewOpenAPIData(document) |
| 47 | if err != nil { |
| 48 | log.Fatalf("error interpreting models: %v", err) |
| 49 | } |
| 50 | |
| 51 | newSchema, err := schemaconv.ToSchema(models) |
| 52 | if err != nil { |
| 53 | log.Fatalf("error converting schema format: %v", err) |
| 54 | } |
| 55 | |
| 56 | if err := yaml.NewEncoder(os.Stdout).Encode(newSchema); err != nil { |
| 57 | log.Fatalf("error writing new schema: %v", err) |
| 58 | } |
| 59 | |
| 60 | } |