From cd5847da1266e9f528c6fd2cbcbf4619a74abf36 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Tue, 13 Feb 2024 16:16:37 +0100 Subject: [PATCH] add env var yaml generator Signed-off-by: Christian Richter --- docs/helpers/configenvextractor.go | 12 +- docs/helpers/envvardeltas.go | 7 - docs/helpers/main.go | 6 +- .../{ => templates}/adoc-generator.go.tmpl | 0 .../templates/envar-delta-table.go.tmpl | 120 ++++++++++++++++++ ...nvironment-variable-docs-generator.go.tmpl | 0 .../example-config-generator.go.tmpl | 0 docs/helpers/{ => templates}/index.tmpl | 0 8 files changed, 129 insertions(+), 16 deletions(-) delete mode 100644 docs/helpers/envvardeltas.go rename docs/helpers/{ => templates}/adoc-generator.go.tmpl (100%) create mode 100644 docs/helpers/templates/envar-delta-table.go.tmpl rename docs/helpers/{ => templates}/environment-variable-docs-generator.go.tmpl (100%) rename docs/helpers/{ => templates}/example-config-generator.go.tmpl (100%) rename docs/helpers/{ => templates}/index.tmpl (100%) diff --git a/docs/helpers/configenvextractor.go b/docs/helpers/configenvextractor.go index 474a1fee3..a553aa72b 100644 --- a/docs/helpers/configenvextractor.go +++ b/docs/helpers/configenvextractor.go @@ -12,9 +12,10 @@ import ( ) var targets = map[string]string{ - "adoc-generator.go.tmpl": "output/adoc/adoc-generator.go", - "example-config-generator.go.tmpl": "output/exampleconfig/example-config-generator.go", - "environment-variable-docs-generator.go.tmpl": "output/env/environment-variable-docs-generator.go", + "templates/adoc-generator.go.tmpl": "output/adoc/adoc-generator.go", + "templates/example-config-generator.go.tmpl": "output/exampleconfig/example-config-generator.go", + "templates/environment-variable-docs-generator.go.tmpl": "output/env/environment-variable-docs-generator.go", + "templates/envar-delta-table.go.tmpl": "output/env/envvar-delta-table.go", } // RenderTemplates does something with templates @@ -37,7 +38,10 @@ func RenderTemplates() { runIntermediateCode(output) } fmt.Println("Cleaning up") - os.RemoveAll("output") + err = os.RemoveAll("output") + if err != nil { + fmt.Println(err) + } } func generateIntermediateCode(templatePath string, intermediateCodePath string, paths []string) { diff --git a/docs/helpers/envvardeltas.go b/docs/helpers/envvardeltas.go deleted file mode 100644 index 4dec070fc..000000000 --- a/docs/helpers/envvardeltas.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func RenderEnvVarDeltas() { - fmt.Println("RenderEnvVarDeltas") -} diff --git a/docs/helpers/main.go b/docs/helpers/main.go index 08041d764..7629a2e01 100644 --- a/docs/helpers/main.go +++ b/docs/helpers/main.go @@ -16,18 +16,15 @@ func main() { RenderGlobalVarsTemplate() case "service-index": GenerateServiceIndexMarkdowns() - case "envvar-deltas": - RenderEnvVarDeltas() case "all": RenderTemplates() GetRogueEnvs() RenderGlobalVarsTemplate() GenerateServiceIndexMarkdowns() - RenderEnvVarDeltas() case "help": fallthrough default: - fmt.Println("Usage: [templates|rogue|globals|service-index|envvar-deltas|all]") + fmt.Println("Usage: [templates|rogue|globals|service-index|all]") } } else { // Left here, even though present in the switch case, for backwards compatibility @@ -35,6 +32,5 @@ func main() { GetRogueEnvs() RenderGlobalVarsTemplate() GenerateServiceIndexMarkdowns() - RenderEnvVarDeltas() } } diff --git a/docs/helpers/adoc-generator.go.tmpl b/docs/helpers/templates/adoc-generator.go.tmpl similarity index 100% rename from docs/helpers/adoc-generator.go.tmpl rename to docs/helpers/templates/adoc-generator.go.tmpl diff --git a/docs/helpers/templates/envar-delta-table.go.tmpl b/docs/helpers/templates/envar-delta-table.go.tmpl new file mode 100644 index 000000000..c1e3bd168 --- /dev/null +++ b/docs/helpers/templates/envar-delta-table.go.tmpl @@ -0,0 +1,120 @@ +package main + +import ( + "fmt" + "gopkg.in/yaml.v2" + "html" + "reflect" + "strings" + "log" + "os" + "path/filepath" + + {{- range $key, $value :=.}} + pkg{{$key}} "{{$value}}" + {{- end}} +) + +const yamlSource = "env_vars.yaml" + +type ConfigField struct { + Name string `yaml:"name"` + DefaultValue string `yaml:"defaultValue"` + Type string `yaml:"type"` + Description string `yaml:"description"` + IntroductionVersion string `yaml:"introductionVersion"` + DeprecationVersion string `yaml:"deprecationVersion"` + RemovalVersion string `yaml:"removalVersion"` + DeprecationInfo string `yaml:"deprecationInfo"` +} + +func main() { + fmt.Println("Generating tables for env-var deltas") + var fields []ConfigField + configFields := make(map[string]ConfigField) + m := map[string]interface{}{ + {{- range $key, $value := .}} + "{{$value}}": *pkg{{$key}}.FullDefaultConfig(), + {{- end }} + } + for _, conf := range m { + fields = GetAnnotatedVariables(conf) + for _, field := range fields { + variants := strings.Split(field.Name, ";") + for _, variant := range variants { + if configFields[variant].Name == "" { + configFields[variant] = field + } else { + fmt.Println("Duplicate key: ", variant) + } + } + } + } + curdir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + fullYamlPath := filepath.Join(curdir, yamlSource) + output, err := yaml.Marshal(configFields) + if err != nil { + log.Fatalf("Could not marshall variables: %v", err) + } + err = os.WriteFile(fullYamlPath, output, 0666) + if err != nil { + log.Fatalf("could not write %s", fullYamlPath) + } + if err := os.Chdir(curdir); err != nil { + log.Fatal(err) + } +} + +func GetAnnotatedVariables(s interface{}) []ConfigField { + t := reflect.TypeOf(s) + v := reflect.ValueOf(s) + + var fields []ConfigField + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + value := v.Field(i) + + switch value.Kind() { + default: + desc := field.Tag.Get("desc") + env, ok := field.Tag.Lookup("env") + if !ok { + continue + } + introductionVersion := field.Tag.Get("introductionVersion") + deprecationVersion := field.Tag.Get("deprecationVersion") + removalVersion := field.Tag.Get("removalVersion") + deprecationInfo := field.Tag.Get("deprecationInfo") + v := fmt.Sprintf("%v", value.Interface()) + typeName := value.Type().Name() + if typeName == "" { + typeName = value.Type().String() + } + //fields = append(fields, ConfigField{Name: strings.ReplaceAll(env, ";", "
"), DefaultValue: html.EscapeString(strings.Replace(v, "|", "\\|", -1)), Description: desc, Type: typeName}) + fields = append(fields, ConfigField{ + Name: env, + DefaultValue: html.EscapeString(strings.Replace(v, "|", "\\|", -1)), + Description: desc, + Type: typeName, + IntroductionVersion: introductionVersion, + DeprecationVersion: deprecationVersion, + RemovalVersion: removalVersion, + DeprecationInfo: deprecationInfo, + }) + case reflect.Ptr: + // PolicySelectors in the Proxy are being skipped atm + // they are not configurable via env vars, if that changes + // they are probably added to the Sanitize() function + // and this should not be an issue then + if !value.IsZero() && value.Elem().CanInterface() { + fields = append(fields, GetAnnotatedVariables(value.Elem().Interface())...) + } + case reflect.Struct: + fields = append(fields, GetAnnotatedVariables(value.Interface())...) + } + } + return fields +} diff --git a/docs/helpers/environment-variable-docs-generator.go.tmpl b/docs/helpers/templates/environment-variable-docs-generator.go.tmpl similarity index 100% rename from docs/helpers/environment-variable-docs-generator.go.tmpl rename to docs/helpers/templates/environment-variable-docs-generator.go.tmpl diff --git a/docs/helpers/example-config-generator.go.tmpl b/docs/helpers/templates/example-config-generator.go.tmpl similarity index 100% rename from docs/helpers/example-config-generator.go.tmpl rename to docs/helpers/templates/example-config-generator.go.tmpl diff --git a/docs/helpers/index.tmpl b/docs/helpers/templates/index.tmpl similarity index 100% rename from docs/helpers/index.tmpl rename to docs/helpers/templates/index.tmpl