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

Get placeholder array from index #5202

Closed
davidpanic opened this issue Nov 11, 2022 · 5 comments · Fixed by #5249
Closed

Get placeholder array from index #5202

davidpanic opened this issue Nov 11, 2022 · 5 comments · Fixed by #5249
Labels
feature ⚙️ New feature or request
Milestone

Comments

@davidpanic
Copy link

I would like to be able to get placeholder values like some programming languages allow to with "rest" arguments. Perhaps it's best explained with an example:

(myservice) {
    {args.0}.some.domain {
        reverse_proxy {args.1...} {
            ...a ton of options that I don't want to repeat
        }
    }
}

import myservice service.a http://192.168.60.1:8081 http://192.168.60.2:8081 http://192.168.60.3:8081
import myservice service.b http://192.168.60.4:8181 http://192.168.60.5:8181

As you can see there's a different amount of arguments (in this case upstream URLs) defined in the import and I want to just specify them all for the reverse proxy upstreams.

To my knowledge there is currently no way to do this, you must specify the arguments by index which doesn't work for variable length arguments.

@francislavoie
Copy link
Member

I've thought about implementing something like this before, but I couldn't think of a good way to do it.

It happens here in the codebase if you'd like to take a look:

https://github.com/caddyserver/caddy/blob/master/caddyconfig/caddyfile/parse.go#L345-L352

(And further down, it does ReplaceKnown to actually replace stuff.)

As for the syntax... I think using ... doesn't look great because we already use a dot in between. It is similar to Go syntax so it has that going for it.

Could be something like {args.1+} where the plus means everything starting at arg 1 and after it. Might be more readable that way, I dunno.

I guess one way it could be implemented is by filling the replacer with a + variant for each entry that is the current arg and all the following ones, for each index provided. That could work.

@francislavoie francislavoie added the feature ⚙️ New feature or request label Nov 11, 2022
@davidpanic
Copy link
Author

I guess one way it could be implemented is by filling the replacer with a + variant for each entry that is the current arg and all the following ones, for each index provided. That could work.

Sounds easy enough to add, I'm all for it! I can implement it and make a PR if you'd like.

@WeidiDeng
Copy link
Member

@davidpanic Can you build caddy with 5249?

@mholt mholt added this to the v2.6.3 milestone Dec 12, 2022
@mholt
Copy link
Member

mholt commented Feb 6, 2023

After looking at this more, I'm wondering if we could keep the : but instead of the ., use square brackets?

So, args[0] and args[-1] and args[:3] instead of args.0 or args.-1 or args.:3.

@francislavoie
Copy link
Member

To elaborate on that, I think we can keep the old {args.0} syntax around for now, without variadic support. We can introduce the this new {args[0]} syntax which includes variadic support.

We can mark the old syntax as deprecated with a Caddyfile adapt warning maybe. But we could defer writing the warning until a later release to not be too annoying.

@mholt mholt modified the milestones: v2.6.3, v2.6.4, v2.6.5 Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants