-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Caddyfile snippets #1921
Caddyfile snippets #1921
Conversation
Cool, thanks! I think the name "macro" leaves a bad taste in my mouth after some bad experiences with C. Also, it feels a little weird to call a declarative unit a "macro" when it's not procedural or operative. That said, I do like the symbolic notation a little better than another keyword exception. I like the parentheses, personally. Or I still like the idea of single quotes: Are there other preferences for this syntax? And what should such a block be called? Just a 'block literal' or a 'reusable block'? Once we decide which syntax to use, I'll begin a review. :) |
Oh, I like the |
Yeah, I'd like to drop 'macro' in favor of some other syntax. Either way, I think beginners will have to learn what the block is/means, whether it's the word macro or a single-quoted word. |
How about Other possibilities: |
I like the ‘(foo) {‘ syntax. Seems more clear that it is not a normal label. |
I'd love |
Again, the problem with using a keyword (and I sometimes regret using |
Do labels ever have spaces though? If you really needed a space in it, wouldn't you do |
It depends on the server type - technically a label can have spaces, but usually you'd do that by enclosing it in quotes Multiple labels like this: |
I like |
I like the idea of using a special character such as $foo otherwise I prefer the use of I also think we should explicitly not allow I think this is an excellent PR and support its merging. |
Personally I would love to be able to use this without the import keyword so it can be used like a placeholder. This would enable me to define a log format for example
Then use this in the caddyfile where I want
Would make it a lot easier to manage large caddyfile s |
@tobya In that case, I'd prefer the single quote syntax more than parentheses: But I'm not sure I like that more than just setting and using environment variables (which you can already do): |
I must admit I was only sort of aware that I could use env variables like that in a caddyfile, however I would much rather have all my declarations in the caddyfile rather than being pulled in from outside. I'm fine with whichever syntax the majority prefer. Although this may need to be a separate feature request 😄 as I can see complications. |
I removed the |
@tobya, I kinda see where you are coming from there. I don't see why your |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The block seems to only work if declared before its use. This may be intentional and may be desired?
caddyfile/parse.go
Outdated
p.definedMacros = map[string][]Token{} | ||
} | ||
if p.definedMacros[name] != nil { | ||
p.Errf("redeclaration of previously declared macro %s", name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error doesn't seem to be triggered!
I declared two blocks directly after each other and both were parsed, but the second one applied with no error raised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops, I forgot to actually return the error. Good catch.
Yeah, macros only work if pre-declared. The alternative is to pre-scan for all macros and defer loading actual server blocks until we've collected everything. I'm not sure that would be so easy to implement though. I'm ok with this limitation for now. |
@mholt, looks like the builds are all failing to find go vet. I don't feel like that is related to my change. |
@captncraig it's fixed upstream #1929 |
I guess gometalinter changed... So what do we call these exactly? Not macros. Preset blocks? Block presets? Something like that, or something else? |
I still like
Its not a
The only place the chosen term will show up is in the docs and in the code. Macro still has my vote. |
@captncraig @mholt How about I prefer (in decreasing order): |
Considering we have |
I dont like macro. I would suggest |
Reusable SnippetsMakes a tolerably good and descriptive heading. |
Cool, 'snippet' it is then. Might be worth renaming macro to snippet in the code and then I'm happy to merge this PR (subject to a final review of course)! |
(To clarify, once the word "macro" has been replaced in the code with "snippet", I'll do a final review!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Thanks Craig. I just had one insignificant question and then it's on track for being merged.
keys := p.block.Keys | ||
// A snippet block is a single key with parens. Nothing else qualifies. | ||
if len(keys) == 1 && strings.HasPrefix(keys[0], "(") && strings.HasSuffix(keys[0], ")") { | ||
return true, strings.TrimSuffix(keys[0][1:], ")") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why TrimSuffix instead of just [1:len(keys[0])-1]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we explicitly check here that the snippet name inside the parens () does not include a .
(dot) this will avoid 1 possible route to confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is confusing about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just think it would be good to avoid
(example.com) {
log example.log "{common}"
}
as a snippet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. Well, I'm curious to see if that will actually be a problem. How about we wait and find out before we add a restriction...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand why it shouldn't be added as a restriction? It needs to be added from the beginning or not at all, otherwise you will have the potential to break installations. The only legitimate use I could see would be if someone wished to create a snippet
(snippet for all subdomains of example.com) {
tls off
}
I wonder how likely that is. I would suggest it is easier to remove the restriction if people feel it gets in the way rather than add it in at a later point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, well, I see what you mean... (I think this change requires that the name in parens is only one word, and I assume your example is just an example in that sense) - what do you think @captncraig ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
1. What does this change do, exactly?
Allows macros of the form:
or
We had some discussion about particular syntax, so I implemented both. I don't really care which anymore at this point. Just need to choose one.
Can be used in any server with
import foo
, it just imports the saved macro rather than read a file.2. Please link to the relevant issues.
https://caddy.community/t/caddyfile-macros-or-inline-includes/2852/8
3. Which documentation changes (if any) need to be made because of this PR?
Needs to be documented in caddyfile docs
4. Checklist