config docs prototype

This commit is contained in:
David Christofas
2022-03-01 13:21:06 +01:00
parent b438fce511
commit bb6a018e9a
6 changed files with 106 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
package main
import (
"io/ioutil"
"log"
"os"
"text/template"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/docs"
)
func main() {
cfg := config.DefaultConfig()
fields := docs.Display(*cfg)
content, err := ioutil.ReadFile("docs/templates/CONFIGURATION.tmpl")
if err != nil {
log.Fatal(err)
}
tpl := template.Must(template.New("").Parse(string(content)))
tpl.Execute(os.Stdout, fields)
// for _, f := range fields {
// fmt.Printf("%s %s = %v\t%s\n", f.Name, f.Type, f.DefaultValue, f.Description)
// }
}

View File

@@ -25,8 +25,8 @@ type Config struct {
Repo Repo
Index Index
ServiceUser ServiceUser
HashDifficulty int `env:"ACCOUNTS_HASH_DIFFICULTY"`
DemoUsersAndGroups bool `env:"ACCOUNTS_DEMO_USERS_AND_GROUPS"`
HashDifficulty int `env:"ACCOUNTS_HASH_DIFFICULTY" desc:"The hash difficulty makes sure that validating a password takes at least a certain amount of time."`
DemoUsersAndGroups bool `env:"ACCOUNTS_DEMO_USERS_AND_GROUPS" desc:"If this flag is set the service will setup the demo users and groups."`
Context context.Context `yaml:"-"`
}

5
docs/templates/CONFIGURATION.tmpl vendored Normal file
View File

@@ -0,0 +1,5 @@
| Name | Type | Default Value | Description |
|------|------|---------------|-------------|
{{- range .}}
| {{.Name}} | {{.Type}} | {{.DefaultValue}} | {{.Description}}|
{{- end }}

38
ocis-pkg/docs/config.go Normal file
View File

@@ -0,0 +1,38 @@
package docs
import (
"fmt"
"reflect"
)
type ConfigField struct {
Name string
DefaultValue string
Type string
Description string
}
func Display(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, Display(value.Interface())...)
}
}
return fields
}

View File

@@ -0,0 +1,11 @@
package main
import (
"github.com/owncloud/ocis/ocis-pkg/docs"
"github.com/owncloud/ocis/thumbnails/pkg/config"
)
func main() {
cfg := config.DefaultConfig()
docs.Display(*cfg)
}

View File

@@ -0,0 +1,23 @@
package main
import (
"fmt"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/config/parser"
"gopkg.in/yaml.v2"
)
func main() {
cfg := config.DefaultConfig()
parser.EnsureDefaults(cfg)
parser.Sanitize(cfg)
b, err := yaml.Marshal(cfg)
if err != nil {
return
}
fmt.Println(string(b))
}