Update configextractor to support multiple templates

This commit is contained in:
Christian Richter
2022-03-09 17:03:20 +01:00
parent 9734b000b3
commit 77d9e77e91
2 changed files with 36 additions and 20 deletions

View File

@@ -11,9 +11,13 @@ import (
"text/template"
)
var targets = map[string]string{
"extractor.go.tmpl": "output/runner.go",
}
func main() {
fmt.Println("Getting relevant packages")
paths, err := filepath.Glob("../../*/pkg/config/defaultconfig.go")
paths, err := filepath.Glob("../../*/pkg/config/defaults/defaultconfig.go")
if err != nil {
log.Fatal(err)
}
@@ -24,27 +28,38 @@ func main() {
for i := range paths {
paths[i] = replacer.Replace(paths[i])
}
content, err := ioutil.ReadFile("extractor.go.tmpl")
if err != nil {
log.Fatal(err)
for template, output := range targets {
GenerateIntermediateCode(template, output, paths)
RunIntermediateCode(output)
}
fmt.Println("Generating intermediate go code")
tpl := template.Must(template.New("").Parse(string(content)))
os.Mkdir("output", 0700)
runner, err := os.Create("output/runner.go")
if err != nil {
log.Fatal(err)
}
tpl.Execute(runner, paths)
fmt.Println("Running intermediate go code")
os.Chdir("output")
os.Setenv("OCIS_BASE_DATA_PATH", "~/.ocis")
out, err := exec.Command("go", "run", "runner.go").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
fmt.Println("Cleaning up")
os.Chdir("../")
os.RemoveAll("output")
}
func GenerateIntermediateCode(templatePath string, intermediateCodePath string, paths []string) {
content, err := ioutil.ReadFile(templatePath)
if err != nil {
log.Fatal(err)
}
fmt.Println("Generating intermediate go code for " + intermediateCodePath + " using template " + templatePath)
tpl := template.Must(template.New("").Parse(string(content)))
os.Mkdir("output", 0700)
runner, err := os.Create(intermediateCodePath)
if err != nil {
log.Fatal(err)
}
tpl.Execute(runner, paths)
}
func RunIntermediateCode(intermediateCodePath string) {
fmt.Println("Running intermediate go code for " + intermediateCodePath)
os.Chdir("output")
os.Setenv("OCIS_BASE_DATA_PATH", "~/.ocis")
out, err := exec.Command("go", "run", "../"+intermediateCodePath).Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
}

View File

@@ -46,6 +46,7 @@ m := map[string]interface{}{
fields = GetAnnotatedVariables(conf)
if len(fields) > 0 {
fmt.Printf("... %s\n", pkg)
os.MkdirAll(filepath.Join(targetFolder, replacer.Replace(pkg)), 0700)
targetFile, err = os.Create(filepath.Join(targetFolder, replacer.Replace(pkg) + "_configvars.md"))
if err != nil {
log.Fatalf("Failed to create target file: %s", err)