From ca300c966e2875c66a25011f78c71a9aabad2c52 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Tue, 10 Jan 2023 12:40:09 +0100 Subject: [PATCH] collect global envvars Signed-off-by: jkoberg --- docs/helpers/adoc-generator.go.tmpl | 120 +++++++++++++++++++++------- docs/templates/ADOC_global.tmpl | 30 +++++++ 2 files changed, 120 insertions(+), 30 deletions(-) create mode 100644 docs/templates/ADOC_global.tmpl diff --git a/docs/helpers/adoc-generator.go.tmpl b/docs/helpers/adoc-generator.go.tmpl index 0ed23f86e..a66300f1f 100644 --- a/docs/helpers/adoc-generator.go.tmpl +++ b/docs/helpers/adoc-generator.go.tmpl @@ -14,6 +14,7 @@ import ( pkg{{$key}} "{{$value}}" {{- end}}) +// ConfigField is the representation of one configuration field type ConfigField struct { EnvVars []string DefaultValue string @@ -23,6 +24,7 @@ type ConfigField struct { DeprecationLink string } +// DeprecationField holds information about deprecation type DeprecationField struct { DeprecationVersion string DeprecationInfo string @@ -30,6 +32,15 @@ type DeprecationField struct { RemovalVersion string } +// EnvVar holds information about one envvar +type EnvVar struct { + Name string + DefaultValue string + Type string + Description string + Services []string +} + type templateData struct { ExtensionName string Fields []ConfigField @@ -38,44 +49,63 @@ type templateData struct { } func main() { -fmt.Println("Generating adoc documentation for environment variables:") -content, err := os.ReadFile("../../docs/templates/ADOC.tmpl") -if err != nil { - log.Fatal(err) -} -replacer := strings.NewReplacer( - "github.com/owncloud/ocis/v2/services/", "", - "/pkg/config/defaults", "", - ) -var fields []ConfigField -var deprecations []DeprecationField -var targetFile *os.File -tpl := template.Must(template.New("").Parse(string(content))) + fmt.Println("Generating adoc documentation for environment variables:") + content, err := os.ReadFile("../../docs/templates/ADOC.tmpl") + if err != nil { + log.Fatal(err) + } + replacer := strings.NewReplacer( + "github.com/owncloud/ocis/v2/services/", "", + "/pkg/config/defaults", "", + ) + var fields []ConfigField + var deprecations []DeprecationField + var targetFile *os.File + tpl := template.Must(template.New("").Parse(string(content))) -m := map[string]interface{}{ -{{- range $key, $value := .}} - "{{$value}}": *pkg{{$key}}.FullDefaultConfig(), -{{- end }} -} - - targetFolder := "../../docs/services/_includes/adoc/" - for pkg, conf := range m { - fields, deprecations = GetAnnotatedVariables(conf) - var hasDeprecations bool - if len(deprecations) > 0 { - hasDeprecations = true + m := map[string]interface{}{ + {{- range $key, $value := .}} + "{{$value}}": *pkg{{$key}}.FullDefaultConfig(), + {{- end }} } - if len(fields) > 0 || len(deprecations) > 0 { - fmt.Printf("... %s\n", pkg) - targetFile, err = os.Create(filepath.Join(targetFolder, replacer.Replace(pkg) + "_configvars.adoc")) + targetFolder := "../../docs/services/_includes/adoc/" + all := make(map[string]EnvVar) + for pkg, conf := range m { + service := replacer.Replace(pkg) + fields, deprecations = GetAnnotatedVariables(conf) + var hasDeprecations bool + if len(deprecations) > 0 { + hasDeprecations = true + } + + for _, f := range fields { + for _, e := range f.EnvVars { + if env, ok := all[e]; ok { + env.Services = append(env.Services, service) + all[e] = env + } else { + all[e] = EnvVar{ + Name: e, + Description: f.Description, + Type: f.Type, + DefaultValue: f.DefaultValue, + Services: []string{service}, + } + } + } + } + + if len(fields) > 0 || len(deprecations) > 0 { + fmt.Printf("... %s\n", pkg) + targetFile, err = os.Create(filepath.Join(targetFolder, service + "_configvars.adoc")) if err != nil { log.Fatalf("Failed to create target file: %s", err) } defer targetFile.Close() td := templateData{ - ExtensionName: replacer.Replace(pkg), + ExtensionName: service, Fields: fields, Deprecations: deprecations, HasDeprecations: hasDeprecations , @@ -83,8 +113,38 @@ m := map[string]interface{}{ if err := tpl.Execute(targetFile, td); err != nil { log.Fatalf("Failed to execute template: %s", err) } + } } - } + + // render global env vars + tmplValues := make([]map[string]interface{}, 0) + for _, env := range all { + if len(env.Services) > 1 { + tmplValues = append(tmplValues, map[string]interface{}{ + "Name": env.Name, + "Services": env.Services, + "Description": env.Description, + "DefaultValue": env.DefaultValue, + "Type": env.Type, + }) + } + } + + glc, err := os.ReadFile("../../docs/templates/ADOC_global.tmpl") + if err != nil { + log.Fatal(err) + } + + gltpl := template.Must(template.New("").Parse(string(glc))) + glfile, err := os.Create(filepath.Join(targetFolder, "global_configvars.adoc")) + if err != nil { + log.Fatalf("Failed to create target file: %s", err) + } + + if err := gltpl.Execute(glfile, tmplValues); err != nil { + log.Printf("Failed to execute template: %s", err) + } + fmt.Println("done") } diff --git a/docs/templates/ADOC_global.tmpl b/docs/templates/ADOC_global.tmpl new file mode 100644 index 000000000..aaaae12e0 --- /dev/null +++ b/docs/templates/ADOC_global.tmpl @@ -0,0 +1,30 @@ +[caption=] +.Environment variables with global scope available in multiple services +[width="100%",cols="~,~,~,~,~",options="header"] +|=== +| Name +| Services +| Type +| Default Value +| Description + +{{ range . }} + +a| `{{ .Name }}` + +a| [subs=-attributes] +{{- range .Services}} +xref:{s-path}/{{ . }}.adoc[{{ . }}] + +{{- end }} + +a| [subs=-attributes] +++{{ .Type }} ++ + +a| [subs=-attributes] +++{{ .DefaultValue }} ++ + +a| [subs=-attributes] +++{{ .Description }} ++ + +{{- end }} +|===