mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-07 03:50:30 -05:00
First version of the envvar docs generator
Signed-off-by: Christian Richter <crichter@owncloud.com@>
This commit is contained in:
committed by
Christian Richter
parent
bb6a018e9a
commit
4af7f630b9
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
paths, err := filepath.Glob("../../*/pkg/config/defaultconfig.go")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
replacer := strings.NewReplacer(
|
||||
"../../", "github.com/owncloud/ocis/",
|
||||
"/defaultconfig.go", "",
|
||||
)
|
||||
for i := range paths {
|
||||
paths[i] = replacer.Replace(paths[i])
|
||||
}
|
||||
content, err := ioutil.ReadFile("extractor.go.tmpl")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tpl := template.Must(template.New("").Parse(string(content)))
|
||||
os.Mkdir("output", 0700)
|
||||
runner, err := os.Create("output/runner.go")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tpl.Execute(runner, paths)
|
||||
os.Chdir("output")
|
||||
out, err := exec.Command("go", "run", "runner.go").Output()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
os.Chdir("../")
|
||||
os.RemoveAll("output")
|
||||
fmt.Println(string(out))
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
{{- range $key, $value := .}}
|
||||
pkg{{$key}} "{{$value}}"
|
||||
{{- end}})
|
||||
|
||||
type ConfigField struct {
|
||||
Name string
|
||||
DefaultValue string
|
||||
Type string
|
||||
Description string
|
||||
}
|
||||
|
||||
func main() {
|
||||
content, err := ioutil.ReadFile("../../../docs/templates/CONFIGURATION.tmpl")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
replacer := strings.NewReplacer(
|
||||
"github.com/owncloud/ocis/", "",
|
||||
"/pkg/config", "",
|
||||
)
|
||||
var fields []ConfigField
|
||||
var targetFile *os.File
|
||||
tpl := template.Must(template.New("").Parse(string(content)))
|
||||
{{ range $key, $value := .}}
|
||||
fields = GetAnnotatedVariables(*pkg{{ $key }}.DefaultConfig())
|
||||
if len(fields) > 0 {
|
||||
targetFolder := "../../../docs/extensions/" + replacer.Replace("{{ $value }}")
|
||||
os.MkdirAll(targetFolder, 0700)
|
||||
targetFile, err = os.Create(targetFolder + "/configvars.md")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tpl.Execute(targetFile, fields)
|
||||
targetFile.Close()
|
||||
}
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
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: env, DefaultValue: v, Description: desc, Type: value.Type().Name()})
|
||||
case reflect.Struct:
|
||||
fields = append(fields, GetAnnotatedVariables(value.Interface())...)
|
||||
}
|
||||
}
|
||||
return fields
|
||||
}
|
||||
Reference in New Issue
Block a user