diff --git a/docs/extensions/_includes/.gitignore b/docs/extensions/_includes/.gitignore index 3b7434f1055..02c62065646 100644 --- a/docs/extensions/_includes/.gitignore +++ b/docs/extensions/_includes/.gitignore @@ -1,2 +1,3 @@ *_configvars.md -*-example.yaml \ No newline at end of file +*-example.yaml +adoc/*adoc \ No newline at end of file diff --git a/docs/extensions/_includes/adoc/.gitkeep b/docs/extensions/_includes/adoc/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/docs/helpers/adoc-generator.go.tmpl b/docs/helpers/adoc-generator.go.tmpl new file mode 100644 index 00000000000..b2e0868d9e8 --- /dev/null +++ b/docs/helpers/adoc-generator.go.tmpl @@ -0,0 +1,94 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "reflect" + "strings" + "text/template" + + {{- range $key, $value := .}} + pkg{{$key}} "{{$value}}" + {{- end}}) + +type ConfigField struct { + Name string + DefaultValue string + Type string + Description string + VersionInfo string +} + +func main() { +fmt.Println("Generating adoc documentation for environment variables:") +content, err := ioutil.ReadFile("../../docs/templates/ADOC.tmpl") +if err != nil { + log.Fatal(err) +} +replacer := strings.NewReplacer( + "github.com/owncloud/ocis/extensions/", "", + "/pkg/config/defaults", "", + ) +var fields []ConfigField +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/extensions/_includes/adoc/" + for pkg, conf := range m { + fields = GetAnnotatedVariables(conf) + if len(fields) > 0 { + fmt.Printf("... %s\n", pkg) + targetFile, err = os.Create(filepath.Join(targetFolder, replacer.Replace(pkg) + "_configvars.adoc")) + if err != nil { + log.Fatalf("Failed to create target file: %s", err) + } + defer targetFile.Close() + if err := tpl.Execute(targetFile, fields); err != nil { + log.Fatalf("Failed to execute template: %s", err) + } + } + } + fmt.Println("done") +} + +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 + } + v := fmt.Sprintf("%v", value.Interface()) + fields = append(fields, ConfigField{Name: strings.ReplaceAll(env, ";", "\n"), DefaultValue: v, Description: desc, Type: value.Type().Name()}) + 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/configenvextractor.go b/docs/helpers/configenvextractor.go index d7afd529553..6b49f9dc1ab 100644 --- a/docs/helpers/configenvextractor.go +++ b/docs/helpers/configenvextractor.go @@ -13,6 +13,7 @@ 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", "extractor.go.tmpl": "output/env/runner.go", } diff --git a/docs/templates/ADOC.tmpl b/docs/templates/ADOC.tmpl new file mode 100644 index 00000000000..512248b6b20 --- /dev/null +++ b/docs/templates/ADOC.tmpl @@ -0,0 +1,19 @@ +[caption=] +.Environment variables for the frontend extension +[width="100%",cols="~,~,~,~",options="header"] +|=== +| Name +| Type +| Default Value +| Description + +{{- range .}} +| `{{.Name}}` +| {{.Type}} +| {{.DefaultValue}} +| {{.Description}} + +{{- end }} +|=== + +Since Version: `+` added, `-` deprecated