diff --git a/.config/bash/module/Container.sh b/.config/bash/module/Container.sh new file mode 100644 index 0000000..a3f2b64 --- /dev/null +++ b/.config/bash/module/Container.sh @@ -0,0 +1,76 @@ +# Attach/create container box in current directory with specified name. +# Uses `main` name by default. +# Usage: ca [NAME] +function ca() { + local name="${1}" + + # Set default name. + [[ "${name}" = "" ]] && name="main" + + # Append box prefix. + name="box-${name}" + + # Create container. + docker run \ + --privileged \ + -d -it \ + -h "${name}" --name "${name}" \ + --workdir /data \ + -v ${PWD}:/data \ + -v ${HOME}:/root:ro \ + debian:latest bash -c bash &> /dev/null + + # Attempt to start container. + docker start "${name}" &> /dev/null + + # Attach to running container. + docker attach "${name}" +} + +# Remove container box with specified name. +# By default uses `main` name. +# Usage: ck [NAME] +function ck() { + local name="${1}" + + # Set default name. + [[ "${name}" = "" ]] && name="main" + + # Append box prefix. + name="box-${name}" + + # Kill container. + docker kill "${name}" &> /dev/null + docker rm "${name}" &> /dev/null +} + +# Remove all container boxes. +function cka() { + local IFS=$'\n' + local boxes=$(_get_boxes) + + for box in ${boxes[@]}; do + ck "${box}" + done +} + +# List all container boxes. +function cl() { + _get_boxes +} + +# Print all boxes. +function _get_boxes() { + local IFS=$'\n' + local boxes=$(docker ps -a | grep "box-" | sed -e "s/.*box-//") + + [[ "${boxes[@]}" != "" ]] && echo "${boxes[@]}" || true +} + +# Autocomplete with boxes. +function _comp_get_boxes() { + local IFS=$'\n' + _autocomplete_first $(_get_boxes) +} + +complete -F _comp_get_boxes ca ck diff --git a/.config/bash/module/Toolbx.sh b/.config/bash/module/Toolbx.sh deleted file mode 100644 index e67626f..0000000 --- a/.config/bash/module/Toolbx.sh +++ /dev/null @@ -1,80 +0,0 @@ -# Attach/create toolbx container with specified name. -# By default uses `main` name. -# Usage: tba [NAME] -function tba() { - local name="${1}" - - # set default name. - if [[ "${name}" = "" ]]; then - name="main" - fi - - # start container or run command inside it if specified. - if [[ "${2}" = "" ]]; then - # set hostname. - toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}" - - # try to enter or create if failed. - toolbox enter "${name}" || { - # create container. - toolbox create "${name}" - - # initialize container. - toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}" - # toolbox --container "$name" run sudo rm /root/.bashrc - # toolbox --container "$name" run sudo ln -s $HOME/.linux /root/.linux - # toolbox --container "$name" run sudo ln -s /root/.linux/bash/bashrc.sh /root/.bashrc - - # enter container, finally. - toolbox enter "${name}" - } - else - # set hostname. - toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}" - - # run command inside container. - toolbox --container "${name}" run ${@:2} - fi -} - -# Remove toolbx container with specified name. -# By default uses `main` name. -# Usage: tbk [NAME] -function tbk() { - local name="${1}" - - # set default name. - if [[ "${name}" = "" ]]; then - name="main" - fi - - # stop and remove podman container. - podman stop "${name}" > /dev/null && podman rm "${name}" > /dev/null -} - -# Install rpm-fusion repository into container with specified name. -# By default uses `main` name. -# Usage: tb_rpmfusion [NAME] -function tb_rpmfusion() { - local name="${1}" - - # set default name. - if [[ "${name}" = "" ]]; then - name="main" - fi - - # run dnf inside container. - toolbox --container "${name}" run sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm -} - -# List all available containers. -function tbl() { - toolbox list -c -} - -# Autocomplete with available containers. -function _tb_containers() { - _autocomplete_first "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')" -} - -complete -F _tb_containers tba tbk tb_rpmfusion