Skip to content

Commit

Permalink
Merge pull request #3659 from owncloud/add-adoc-export
Browse files Browse the repository at this point in the history
Adoc export
  • Loading branch information
dragonchaser authored May 4, 2022
2 parents 8bfb589 + 9d7c9d3 commit a3c4a9d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/extensions/_includes/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*_configvars.md
*-example.yaml
*-example.yaml
adoc/*adoc
Empty file.
104 changes: 104 additions & 0 deletions docs/helpers/adoc-generator.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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
}

type templateData struct {
ExtensionName string
Fields []ConfigField
}

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()

td := templateData{
ExtensionName: replacer.Replace(pkg),
Fields: fields,
}
if err := tpl.Execute(targetFile, td); 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
}
1 change: 1 addition & 0 deletions docs/helpers/configenvextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"environment-variable-docs-generator.go.tmpl": "output/env/environment-variable-docs-generator.go",
}
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/example-config-generator.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
if err != nil {
log.Fatalf("Marshalling yaml for pkg0 failed: %s\n", err)
}
return "# Autogenerated\n" + string(yml)
return fmt.Sprintf("# Autogenerated\n# Filename: %s-config-example.yaml\n\n%s", replacer.Replace("{{ $value }}"),string(yml))
}(),
{{- end}}
}
Expand Down
19 changes: 19 additions & 0 deletions docs/templates/ADOC.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[caption=]
.Environment variables for the {{ .ExtensionName }} extension
[width="100%",cols="~,~,~,~",options="header"]
|===
| Name
| Type
| Default Value
| Description

{{- range .Fields}}
| `{{.Name}}`
| {{.Type}}
| {{.DefaultValue}}
| {{.Description}}

{{- end }}
|===

Since Version: `+` added, `-` deprecated

0 comments on commit a3c4a9d

Please sign in to comment.