Custom format for GetDescription
Hi,
i really like your simple approach for env configuration and also the possibility to easily print the env variables. But i'm not that fully happy with the multiline format for the env variables and currently its pretty hard to adjust it. So maybe it would be useful to have a possibility to pass an own format for the description lines like
"$1 [$2] - $3 (default $4)"
which would be create an output like this
TEST_ENV [string] - This is a test env variable (default "test")
Another maybe more easier approach to implement would be to add an method which returns the parsed metadata so it could be used to print using an own format.
Thanks
That's an interesting proposal, thank you.
I think the easiest thing to do would be to simply make a function that takes the formatting function as a parameter, which is where the meta-information about the structure is transmitted. But at the moment all this metainformation is implemented locally, and I don't really want to make it exportable.
On the other hand, I don't want to introduce some DSL to describe templates and make library users learn it.
Anyway, I need to think about how to do it better and I'll be glad to receive suggestions.
Hi @ilyakaznacheev I've just check how the Usage/Description is build in envconfig. There they use text tables which looks pretty nice and its also possible to customise it easily using the template engine. Maybe this could be a good solution here as well.
For example the interesting code could look like this:
const (
DefaultTableFormat = `The following environment variables can be used for configuration:
KEY TYPE DEFAULT REQUIRED DESCRIPTION
{{range .}}{{usage_key .}} {{usage_type .}} {{usage_default .}} {{usage_required .}} {{usage_description .}}
{{end}}`
)
...
functions := template.FuncMap{
"usage_key": func(v structMeta) string { return v.envList[0] },
"usage_description": func(v structMeta) string { return v.description },
"usage_type": func(v structMeta) string { return v.fieldValue.Kind().String() },
"usage_default": func(v structMeta) string { return derefString(v.defValue) },
"usage_required": func(v structMeta) string { return formatBool(v.required) },
}
...
tmpl, err := template.New("cleanenv").Funcs(functions).Parse(DefaultTableFormat)
tabs := tabwriter.NewWriter(os.Stdout, 1, 0, 4, ' ', 0)
err = tmpl.Execute(tabs, meta)
If you think this could be a solution for your library i would create a PR which brings such a functionality.
Best regards eloo