Skip to content

Improve handling of the environment #5

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ankon
Copy link

@ankon ankon commented Aug 3, 2023

The main contribution here is in the last commit: Instead of producing invalid template content and an error that is very misleading (see also #1), it now produces a warning about the problem and skips the variable. The idea here is that not every variable in the environment might be used in the template, and the warning should be visible enough so that whoever is running caddy can fix it if needed.

The second important part 4cae952, which works around problems when environment variables contain \ characters: A simple strconv.Unquote leaves these, and putting the value into quotes then produces problems when processing the template. Solution is to let Sprintf's %q format handle the value and quote it properly.

The other commits are cleanups and drive-by improvements.

ankon added 7 commits August 3, 2023 20:37
This should make the tests pass even when the environment on the test
host is actually leading to syntax problems in the generated template.
No caller cares about the return value in the error case (and they anyways
have it already!), so reduce confusion for the reader and follow golang's
usual code patterns.
Using `strconv.Unquote` removes a level of quotes, but it doesn't handle things
like embedded `\` characters -- which happen on pretty much any sh-like shell
in the `PS1` environment. The `%q` format to Sprintf properly escapes these.
For instance this could happen when `nvs` is installed, as it adds some
functions into the environment, which appear as `BASH_FUNC_nvs%%` under bash.
@joeblew999
Copy link

This looks like a useful addition .

Is this likely to be merged though ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants