Skip to content

Instantly share code, notes, and snippets.

@aminnairi
Last active March 1, 2021 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminnairi/fd430af265167571be2c9a50c1220fb1 to your computer and use it in GitHub Desktop.
Save aminnairi/fd430af265167571be2c9a50c1220fb1 to your computer and use it in GitHub Desktop.
python.fish
#!/usr/bin/env fish
# File: $HOME/.config/fish/functions/python.fish
# Author: Amin NAIRI <https://github.com/aminnairi>
# Usage: python DOCKER_ARGUMENTS -- ENTRYPOINT_ARGUMENTS
# Example: python -- --version
# Example: python --tag 8.0.0 -- --version
# Example: python -- script.mjs
# Example: python --tag 8.0.0 -- script.mjs
# Example: python -- --experimental-json-modules module.mjs
# Example: python --publish 8080:8080 -- --experimental-json-modules server.mjs
function python
# The image name to use
set -l docker_image python
# The entrypoint to use for this image
set -l docker_entrypoint python
# A local array of arguments to proxy
set -l entrypoint_arguments
# A local array of Docker arguments to proxy
set -l docker_arguments
# A boolean to check whether the argument is assigned to Docker or the entrypoint program
set -l is_docker_argument true
# A boolean to check whether the argument is the tag modification argument
set -l is_tag_modification_argument false
# A string to keep track of the wanted tag for running this image
set -l docker_image_tag latest
# For each arguments in the provided arguments
for argument in $argv
if [ "$argument" = "--tag" ]
set is_tag_modification_argument true
continue
end
if [ "$is_tag_modification_argument" = "true" ]
set docker_image_tag "$argument"
set is_tag_modification_argument false
continue
end
# If the argument is the end of positional arguments argument
if [ "$argument" = "--" ]
# Setting the boolean to false
set is_docker_argument false
# Continueing looping for other arguments
continue
end
# If we are still in the docker argument positional state
if [ "$is_docker_argument" = "true" ]
# Appending the current argument to the list of all docker arguments
set -a docker_arguments $argument
else
# Appending the current argument to the list of all entrypoint arguments
set -a entrypoint_arguments $argument
end
end
# running entrypoint with all the arguments
docker pull "$docker_image:$docker_image_tag" && docker run --interactive --tty --rm --entrypoint "$docker_entrypoint" --volume "$PWD:/usr/local/src" --user (id -u):(id -u) --workdir /usr/local/src $docker_arguments "$docker_image:$docker_image_tag" $entrypoint_arguments
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment