Skip to content

Instantly share code, notes, and snippets.

@hlecuanda
Last active October 28, 2019 01:06
Show Gist options
  • Save hlecuanda/78a6a39877c9753230a11c2d8832f4b6 to your computer and use it in GitHub Desktop.
Save hlecuanda/78a6a39877c9753230a11c2d8832f4b6 to your computer and use it in GitHub Desktop.
prezto-container control zsh function
#!/usr/bin/env zsh
local opts withvals
zmodload zsh/zutil || { <<< 'Requires zsh/zutil module to be available'; false; return }
[ -z $commands[docker] ] && { <<< 'Requires Docker'; false; return }
zparseopts -D -E -M -a opts -A withvals - \
h=hlp -help=h \
i: -image=i \
N: -name=N \
n -dry-run=n \
p -persistant=p \
r -run=r \
-zdotdir: \
-zpreztodir:
if (( $#hlp == 1 )) ; then
<<-USAGE
${0}: create an ephemeral prezto container using docker
usage:
${0} [options] -- [addtl-args]
options:
-h, --help : print this message
-p, --persistant : make a persistant container
-N NAME, --name=NAME : set container name to NAME (default: prezto)
-n, --dry-run : see what command would be run without doing so
--noninteractive : do not attach to container
-r 'CMD' --run 'CMD' : run 'CMD' on the container (quote CMD)
-i IMG, --image=IMG : create container from image IMG (default hlecuanda/prezto-dev)
--zdotdir=PATH : use dotfiles from local PATH
--zpreztodir : override default prezto to local PATH
addtl-args:
any additional arguments after the -- argument guard
will be passed as docker options before the image name
example:
${0} -n myruncoms --zdotdir=$HOME
creates an ephemeral container named myruncoms using
dotfiles found in ${HOME}
${0} -n illbeback -p
creates a persistant container named illbeback if such
container exists, then startit and attach to it
${0} -n ivebeenback -r 'apk add python'
spins up the a container named ivebeenback, and runs
the comand 'apk add python'. the container stops when
done.
USAGE
fi
local addtlargs="$argv[@]"
local dockerpull=""
local dockercmd='docker run'
local dryrun=""
local image="hlecuanda/prezto-dev"
local interactive="-it"
local name="prezto"
local persistant="--rm"
local zdotdir=""
local zpreztodir=""
for opt in ${(k)withvals}
case $opt in
-i)
image="$withvals[-i]" ;;
-n)
dryrun="print --" ;;
-noninteractive)
interactive="" ;;
-N)
name="$withvals[-n]" ;;
-p)
persistant="" ;;
-zdotdir)
zdotdir="-v ${(qq)withvals[-zdotdir]}:/home/prezto/zdotdir -e 'ZDOTDIR=/home/prezto/zdotdir " ;;
-zpreztodir)
zpreztodir="-v ${(qq)withvals[-zpreztodir]}:/home/prezto/zpreztodir -e 'ZPREZTODIR=/home/prezto/zpreztodir " ;;
esac
docker images \
| grep prezto-dev \
&>>! /dev/null || local dockerpull="docker pull $image && "
dockercmd="$dryrun $dockerpull docker run "
dockercmd="$dockercmd $persistant $interactive "
dockercmd="$dockercmd -h $name --name $name "
dockercmd="$dockercmd $zdotdir $zpreztodir "
dockercmd="$dockercmd ${(z)addtlargs} $image"
cmd=$(echo $dockercmd | tr -s \ )
${(z)cmd}
# vim: set ft=zsh sw=2 tw=0 fdm=manual et :
@hlecuanda
Copy link
Author

  1. download raw somewhere on your $fpath (contains some leading tabs needed for here-doc formatting)
  2. autoload with autoload prezto-container
  3. enjoy.com

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment