Merge pull request #5369 from kobergj/CollectGlobalEnvVars

Collect global Envvars
This commit is contained in:
kobergj
2023-01-11 09:20:17 +01:00
committed by GitHub
3 changed files with 139 additions and 31 deletions

View File

@@ -0,0 +1,5 @@
Enhancement: Collect global envvars
Compose a list of all envvars living in more than 1 service
https://github.com/owncloud/ocis/pull/5367

View File

@@ -9,11 +9,14 @@ import (
"regexp"
"strings"
"text/template"
"sort"
{{- range $key, $value := .}}
pkg{{$key}} "{{$value}}"
{{- end}})
{{- end}}
)
// ConfigField is the representation of one configuration field
type ConfigField struct {
EnvVars []string
DefaultValue string
@@ -23,6 +26,7 @@ type ConfigField struct {
DeprecationLink string
}
// DeprecationField holds information about deprecation
type DeprecationField struct {
DeprecationVersion string
DeprecationInfo string
@@ -30,6 +34,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 +51,66 @@ 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)
sort.Slice(env.Services, func(i, j int) bool {
return env.Services[i] < env.Services[j]
})
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 +118,43 @@ 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,
})
}
}
// sort
sort.Slice(tmplValues, func(i, j int) bool {
return tmplValues[i]["Name"].(string) < tmplValues[j]["Name"].(string)
})
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")
}

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

@@ -0,0 +1,33 @@
// collected through docs/helpers/adoc-generator.go.tmpl
[.landscape]
[caption=]
.Environment variables with global scope available in multiple services
[width="100%",cols="30%,25%,~,~,~",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 }}
|===