Output example yaml

This commit is contained in:
Christian Richter
2022-03-10 14:06:51 +01:00
parent 77d9e77e91
commit 59eaf7c1b8
4 changed files with 57 additions and 11 deletions
+5 -5
View File
@@ -6,13 +6,15 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"text/template"
)
var targets = map[string]string{
"extractor.go.tmpl": "output/runner.go",
"example-config-generator.go.tmpl": "output/exampleconfig/example-config-generator.go",
"extractor.go.tmpl": "output/env/runner.go",
}
func main() {
@@ -34,7 +36,6 @@ func main() {
RunIntermediateCode(output)
}
fmt.Println("Cleaning up")
os.Chdir("../")
os.RemoveAll("output")
}
@@ -45,7 +46,7 @@ func GenerateIntermediateCode(templatePath string, intermediateCodePath string,
}
fmt.Println("Generating intermediate go code for " + intermediateCodePath + " using template " + templatePath)
tpl := template.Must(template.New("").Parse(string(content)))
os.Mkdir("output", 0700)
os.MkdirAll(path.Dir(intermediateCodePath), 0700)
runner, err := os.Create(intermediateCodePath)
if err != nil {
log.Fatal(err)
@@ -55,9 +56,8 @@ func GenerateIntermediateCode(templatePath string, intermediateCodePath string,
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()
out, err := exec.Command("go", "run", intermediateCodePath).Output()
if err != nil {
log.Fatal(err)
}
@@ -0,0 +1,47 @@
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
{{- range $key, $value := .}}
pkg{{$key}} "{{$value}}"
{{- end}}
)
func main() {
targetFolder := "example-yaml/"
os.MkdirAll(targetFolder, 0700)
replacer := strings.NewReplacer(
"github.com/owncloud/ocis/", "",
"/pkg/config/defaults", "",
)
cfg := map[string]string{
{{- range $key, $value := .}}
replacer.Replace("{{$value}}"): func() string {
fmt.Println("Generating example YAML config for {{ $value -}}")
c := pkg{{$key}}.DefaultConfig()
pkg{{$key}}.EnsureDefaults(c)
pkg{{$key}}.Sanitize(c)
yml, err := yaml.Marshal(c)
if err != nil {
log.Fatalf("Marshalling yaml for pkg0 failed: %s\n", err)
}
return "# Autogenerated\n" + string(yml)
}(),
{{- end}}
}
for pkg, yml := range cfg {
targetYamlFile, err := os.Create(filepath.Join(targetFolder, replacer.Replace(pkg) + "-example.yaml"))
if err != nil {
log.Fatalf("Failed to create target file for : %s", err)
}
defer targetYamlFile.Close()
targetYamlFile.WriteString(yml)
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ type ConfigField struct {
func main() {
fmt.Println("Generating documentation for environment variables:")
content, err := ioutil.ReadFile("../../../docs/templates/CONFIGURATION.tmpl")
content, err := ioutil.ReadFile("../../docs/templates/CONFIGURATION.tmpl")
if err != nil {
log.Fatal(err)
}