Last active
February 5, 2022 22:12
-
-
Save EnigmaCurry/f8136b02731dfeda2b7ee7a7632654c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
wrapper() { | |
## Run a native command if it's available, otherwise from a docker image | |
## (Note: this only works with programs that read/write to stdin/stdout. | |
## Reading and writing files on the client host system is not supported.) | |
DOCKER=${DOCKER:-docker} | |
command=$1; shift | |
WRAPPER_IMAGE=localhost/wrapper_${command} | |
if which ${command} >/dev/null 2>&1; then | |
## Runs the native command passing args and stdin, prefixed with | |
## "command" to ignore the function with the same name: | |
command ${command} "${@}" </dev/stdin | |
else | |
${DOCKER} run --rm -i ${WRAPPER_IMAGE} ${command} "${@}" </dev/stdin | |
fi | |
} | |
wrapper_build() { | |
DOCKER=${DOCKER:-docker} | |
dockerfile=$(</dev/stdin) | |
command=$1; shift | |
WRAPPER_IMAGE=localhost/wrapper_${command} | |
if ! which ${command} >/dev/null 2>&1; then | |
echo "${dockerfile}" | ${DOCKER} build -t ${WRAPPER_IMAGE} - 2>&1 >/dev/null | |
fi | |
} | |
### USAGE: | |
# source wrapper.sh | |
# jq() { | |
# wrapper_build jq <<EOF | |
# FROM alpine | |
# RUN apk add -U jq | |
# EOF | |
# wrapper jq "${@}" </dev/stdin | |
# } | |
# | |
# ## Then use jq like normal, whether its installed or not: | |
# cat passwords.json | jq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## OK, so I have some json to parse: | |
[ryan@maldek ~]$ JSON='{"username":"ryan", "password":"foo"}' | |
## But, I don't have jq installed: | |
[ryan@maldek ~]$ jq --version | |
bash: jq: command not found | |
## No problem, if I have docker/podman, I can import the wrapper functions | |
## and then define this function for jq: | |
[ryan@maldek ~]$ source docker_wrapper.sh | |
[ryan@maldek ~]$ | |
jq() { | |
wrapper_build jq <<EOF | |
FROM alpine | |
RUN apk add -U jq | |
EOF | |
wrapper jq "${@}" </dev/stdin | |
} | |
## Now I have jq available, even though its not installed locally: | |
## This is the version that is in the docker container: | |
[ryan@maldek ~]$ jq --version | |
jq-master-v3.15.0_alpha20210804-4073-gb39e1241e8 | |
## Now I can use jq to parse the json: | |
[ryan@maldek ~]$ echo ${JSON} | jq ".username" | |
"ryan" | |
## This version is a bit slow, since it builds and runs on my remote docker server. | |
## What if I install the native version of jq locally? | |
[ryan@maldek ~]$ sudo pacman -S jq | |
[sudo] password for ryan: | |
resolving dependencies... | |
looking for conflicting packages... | |
Packages (1) jq-1.6-4 | |
Total Installed Size: 0.67 MiB | |
... | |
## OK now I can still run jq the same way, but its running locally now: | |
## This is the version number for my system package of jq: | |
[ryan@maldek ~]$ jq --version | |
jq-1.6 | |
## Still works (and its faster): | |
[ryan@maldek ~]$ echo ${JSON} | jq ".username" | |
"ryan" | |
## jq is a good use case for this since it operates only on stdin/stdout. | |
## For other programs that require reading/writing files, | |
## this wouldn't work on a remote docker server, and so its not supported. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment