Globbing files to pass to CLI command options
I've been using Helm recently as part of a new product offering, and as a way to deploy my various websites. Helm allows providing values to the templates in a Helm chart via one or more values files, which are provided via the option -f. It's often useful to break these into several files, so you can do things like cover common values, and then environment- or namespace-specific values.
However, the -f option only accepts a single value, and doesn't recognize globs.
The printf solution
The solution is to pass the glob to printf, with the format '-f %s '. This will print out an option per file.
When in a terminal, this can be done using printf within a subshell. Now how you do that depends on the shell, because the -f in our format string can be interpreted as an option/flag to printf itself depending on the shell.
For bash, we can do this:
helm upgrade -n qa . $(printf -- '-f % ' *.values.yaml)
This is because printf in bash will emit an error for any unknown option/flag it encounters; the -- tells the command when the end of all options/flags have been reached, so that it treats the remainder as arguments.
For fish, we don't need the --, as the printf implementation in fish doesn't accept arguments anyways:
helm upgrade -n qa . $(printf '-f % ' *.values.yaml)
Makefile
Of course, subshells in a Makefile require a little change as well. In a Makefile, I write the above as:
helm upgrade -n qa . $(shell printf -- '-f %' *.values.yaml)
(And since I also declare SHELL=/bin/bash at the top of my Makefile, I have to use the -- trick here.)