Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom format for GetDescription #48

Open
eloo opened this issue Jun 5, 2020 · 2 comments · May be fixed by #56
Open

Custom format for GetDescription #48

eloo opened this issue Jun 5, 2020 · 2 comments · May be fixed by #56
Labels
discussion Running discussion proposal

Comments

@eloo
Copy link

eloo commented Jun 5, 2020

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

@ilyakaznacheev ilyakaznacheev added the discussion Running discussion label Jun 14, 2020
@ilyakaznacheev
Copy link
Owner

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.

@eloo
Copy link
Author

eloo commented Sep 30, 2020

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

@eloo eloo linked a pull request Oct 2, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Running discussion proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants