sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/hack/gen-prow-documented/main.go (about) 1 /* 2 Copyright 2022 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 "os" 23 "path" 24 "path/filepath" 25 26 "github.com/sirupsen/logrus" 27 "sigs.k8s.io/prow/pkg/config" 28 "sigs.k8s.io/prow/pkg/genyaml" 29 "sigs.k8s.io/prow/pkg/plugins" 30 ) 31 32 const ( 33 // Pretending that it runs from root of the repo 34 defaultRootDir = "." 35 ) 36 37 var genConfigs = []genConfig{ 38 { 39 in: []string{ 40 "pkg/config/*.go", 41 "pkg/apis/prowjobs/v1/*.go", 42 }, 43 format: &config.ProwConfig{}, 44 out: "pkg/config/prow-config-documented.yaml", 45 }, 46 { 47 in: []string{ 48 "pkg/plugins/*.go", 49 }, 50 format: &plugins.Configuration{}, 51 out: "pkg/plugins/plugin-config-documented.yaml", 52 }, 53 } 54 55 type genConfig struct { 56 in []string 57 format interface{} 58 out string 59 } 60 61 func (g *genConfig) gen(rootDir string) error { 62 var inputFiles []string 63 for _, goGlob := range g.in { 64 ifs, err := filepath.Glob(path.Join(rootDir, goGlob)) 65 if err != nil { 66 return fmt.Errorf("filepath glob: %w", err) 67 } 68 inputFiles = append(inputFiles, ifs...) 69 } 70 71 commentMap, err := genyaml.NewCommentMap(nil, inputFiles...) 72 if err != nil { 73 return fmt.Errorf("failed to construct commentMap: %w", err) 74 } 75 actualYaml, err := commentMap.GenYaml(genyaml.PopulateStruct(g.format)) 76 if err != nil { 77 return fmt.Errorf("genyaml errored: %w", err) 78 } 79 if err := os.WriteFile(path.Join(rootDir, g.out), []byte(actualYaml), 0644); err != nil { 80 return fmt.Errorf("failed to write fixture: %w", err) 81 } 82 return nil 83 } 84 85 func main() { 86 rootDir := flag.String("root-dir", defaultRootDir, "Repo root dir.") 87 flag.Parse() 88 89 for _, g := range genConfigs { 90 if err := g.gen(*rootDir); err != nil { 91 logrus.WithError(err).WithField("fixture", g.out).Error("Failed generating.") 92 os.Exit(1) 93 } 94 } 95 }