1/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package main
18
19import (
20	"log"
21	"os"
22
23	"github.com/googleapis/gnostic/compiler"
24	openapi_v2 "github.com/googleapis/gnostic/openapiv2"
25	yaml "gopkg.in/yaml.v2"
26
27	"k8s.io/kube-openapi/pkg/schemaconv"
28	"k8s.io/kube-openapi/pkg/util/proto"
29)
30
31func main() {
32	if len(os.Args) != 1 {
33		log.Fatal("this program takes input on stdin and writes output to stdout.")
34	}
35
36	var info yaml.MapSlice
37	if err := yaml.NewDecoder(os.Stdin).Decode(&info); err != nil {
38		log.Fatalf("error decoding stdin: %v", err)
39	}
40
41	document, err := openapi_v2.NewDocument(info, compiler.NewContext("$root", nil))
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}
61