collect global envvars

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg
2023-01-10 12:40:09 +01:00
parent 1d51aab69f
commit ca300c966e
2 changed files with 120 additions and 30 deletions

View File

@@ -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")
}

30
docs/templates/ADOC_global.tmpl vendored Normal file
View File

@@ -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 }}
|===