github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/testgrid/config/main.go (about) 1 /* 2 Copyright 2016 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 "flag" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "strings" 25 26 "k8s.io/test-infra/testgrid/config/yaml2proto" 27 ) 28 29 var ( 30 yamlPaths = flag.String("yaml", "", "comma-separated list of input YAML files") 31 printText = flag.Bool("print-text", false, "print generated proto in text format to stdout") 32 outputPath = flag.String("output", "", "output path to save generated protobuf data") 33 ) 34 35 func errExit(format string, a ...interface{}) { 36 fmt.Fprintf(os.Stderr, format, a...) 37 os.Exit(1) 38 } 39 40 func main() { 41 flag.Parse() 42 43 yamlFiles := strings.Split(*yamlPaths, ",") 44 if len(yamlFiles) == 0 || yamlFiles[0] == "" { 45 errExit("Must specify one or more YAML files with --yaml\n") 46 } 47 if !*printText && *outputPath == "" { 48 errExit("Must set --print-text or --output\n") 49 } 50 if *printText && *outputPath != "" { 51 errExit("Cannot set both --print-text and --output\n") 52 } 53 54 var c yaml2proto.Config 55 for _, file := range yamlFiles { 56 b, err := ioutil.ReadFile(file) 57 if err != nil { 58 errExit("IO Error : Cannot Read File %s : %v\n", file, err) 59 } 60 if err = c.Update(b); err != nil { 61 errExit("Error parsing file %s : %v\n", file, err) 62 } 63 } 64 65 if *printText { 66 if err := c.MarshalText(os.Stdout); err != nil { 67 errExit("err printing proto: %v", err) 68 } 69 } else { 70 b, err := c.MarshalBytes() 71 if err != nil { 72 errExit("err encoding proto: %v", err) 73 } 74 if err = ioutil.WriteFile(*outputPath, b, 0644); err != nil { 75 errExit("IO Error : Cannot Write File %v\n", outputPath) 76 } 77 } 78 }