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

String substitution in variable expansion #4427

Merged
merged 1 commit into from
Jan 12, 2024

Conversation

tstenner
Copy link

One of the variable expansion functions missing from #4287 was string substition, so I implemented it.

Motivating example:

ARG VERSION=1.2.3
ARG URL=https://github.com/foo-corp/foo/releases/download/$VERSION/foo-$VERSION-${TARGETARCH}.tar.gz
ADD ${URL//arm64/aarch64} /tmp/foo.tar.gz
RUN tar -xf /tmp/foo.tar.gz

The two parts following the variable name (pattern and replacement) need to be parsed separately with processStopWords, otherwise variable expansion could cause a / in a variable referenced in the pattern to be interpreted as separator between patern and replacement. This means that the second / is required (i.e. ${foo/bar/} to remove bar, compared to ${foo/bar} as accepted by various shells).

On the positive side, wildcards and slashes in referenced variables are handled correctly (e.g. PAT='*/'; REPL='.../'; ${FOO/$PAT/$REPL}).

Signed-off-by: Tristan Stenner <ts@ppi.de>
Copy link
Collaborator

@dvdksn dvdksn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Docs LGTM :-)

@tstenner
Copy link
Author

tstenner commented Dec 4, 2023

Surprisingly, all CI checks passed even though I added more tests.

There's one decision left: referencing an undefined variable is not an error (as it is in bash without set -u), but personally I feel it should be.

@tonistiigi
Copy link
Member

PTAL @jsternberg

@jsternberg
Copy link
Collaborator

Sorry for taking so long to get to this. I've had it on my backburner for awhile. I'll have a review for this by the end of my current day.

Copy link
Collaborator

@jsternberg jsternberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tonistiigi tonistiigi merged commit a091126 into moby:master Jan 12, 2024
61 checks passed
@tstenner tstenner deleted the variable_substitution branch January 14, 2024 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants