File: prit.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # prit [options...] [command...] [args...]
  27 #
  28 # Podman Run Interactive Teletype starts the podman container given and puts
  29 # you into its running teletype shell, using the command `podman run -it`.
  30 #
  31 # When no command is given, the default is to run a container's live shell.
  32 #
  33 # The extra options are, available both in single and double-dash versions
  34 #
  35 #   -h, -help    show this help message
  36 #   -n, -name    run using the container name given, instead of picking one
  37 
  38 
  39 name=''
  40 container=''
  41 
  42 case "$1" in
  43     -h|--h|-help|--help)
  44         awk '/^# +prit /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  45         exit 0
  46     ;;
  47 
  48     -n|--name)
  49         if [ $# -lt 2 ]; then
  50             printf "forgot the container name\n" >&2
  51             exit 1
  52         fi
  53         name="$1"
  54         container="$1"
  55         shift
  56     ;;
  57 esac
  58 
  59 if [ -z "${container}" ] && command -v fzf > /dev/null; then
  60     data="$(podman images)"
  61     picker='fzf --reverse --header-first --header-lines=1'
  62     if [ $# -eq 0 ]; then
  63         msg='podman run -it [container]: enter picks container, escape quits'
  64     else
  65         msg="podman run -it [container] $*: enter picks container, escape quits"
  66     fi
  67     pick="$(echo "${data}" | ${picker} --header="${msg}")"
  68     container="$(echo "${pick}" | awk '{ print $3 }')"
  69     name="$(echo "${pick}" | awk '{ print $1 }')"
  70 
  71     if [ -z "${container}" ]; then
  72         exit 1
  73     fi
  74 fi
  75 
  76 if [ -z "${container}" ]; then
  77     awk '/^# +prit /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  78     exit 1
  79 fi
  80 
  81 if [ $# -eq 0 ]; then
  82     printf "\n\e[7mentered\e[0m shell from podman container \e[7m%s\e[0m\n\n" \
  83         "${name}" >&2
  84 fi
  85 
  86 podman run -it "${container}" "$@"
  87 res=$?
  88 
  89 if [ $# -eq 0 ]; then
  90     printf "\n\e[7mexited\e[0m shell from podman container \e[7m%s\e[0m\n\n" \
  91         "${name}" >&2
  92 fi
  93 
  94 exit "${res}"