Environment Variable Substitution doesn't implement regex

So, as Drone doesn’t include a slug for commit branch, I tried to use environment substitution ${DRONE_COMMIT_BRANCH//[^a-zA-Z0-9_.-]/__}. This should ensure any branch name is mapped to an unique value, which is a valid docker image tag for example. However, it seems that the envsubst go package doesn’t implement regex parsing. This is a huge difference from shell implementations for this pattern.

See envsubst/funcs.go at 179042472c46cbed86d44ecbb11a03236efbb73a · drone/envsubst · GitHub

I can go and replace that with a regex. However, that might break some existing uses. Though, I personally see this missing feature as a bug.

That said, is someone against this better implementation and is there something I have not thought about? If not, then I can look on implementing a PR for this.

p.s. DRONE_COMMIT_BRANCH_SLUG couldn’t be a valuable addition (Gitlab CI includes CI_COMMIT_REF_SLUG).

The envsubst library is a reasonable effort to emulate posix variable substitution, which uses pattern matching, which is not the same thing as regular expressions. Note this warning from posix pattern matching documentation:

Do not confuse patterns with regular expressions because they share some symbols and do similar matching work.

Here is some relevant documentation:

For pattern matching we use the path.Match function in the Go standard library. You can see the supported pattern matching syntax at this link:

The path.Match function may not support the full posix pattern matching syntax but should support most common use cases. I suspect that in order to provide accurate emulation one would need to write a custom pattern matching library, which is not something I was interested in doing. I decided to use as much of the standard library as possible, meaning the goal was to get reasonably close to the posix spec, therefore, a perfectly compliant implementation was considered out of scope.

Yeah. My bad on the regex part. I got confused as the pattern in example is correct in both.

However, it’s still the case that the envsubst uses string replace and not path match, if I found the correct part in the code.

I tested my example in bash, zsh and ksh, so it seems to be valid modern shell pattern (doesn’t work in dash of course).