Improve handling of the environment#5
Open
ankon wants to merge 7 commits into
Open
Conversation
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.
|
This looks like a useful addition . Is this likely to be merged though ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 simplestrconv.Unquoteleaves these, and putting the value into quotes then produces problems when processing the template. Solution is to let Sprintf's%qformat handle the value and quote it properly.The other commits are cleanups and drive-by improvements.