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 image 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 image'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 image name given, instead of picking one
  37 
  38 
  39 image=''
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +prit /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         exit 0
  45     ;;
  46 
  47     -n|--name)
  48         if [ $# -lt 2 ]; then
  49             printf "forgot the image name\n" >&2
  50             exit 1
  51         fi
  52         image="$2" && shift 2
  53     ;;
  54 
  55     --) shift ;;
  56 
  57     -*)
  58         printf "unsupported option '%s'\n" "$1" >&2
  59         exit 1
  60     ;;
  61 esac
  62 
  63 if [ -n "${image}" ]; then
  64     pick="$(
  65         podman images | awk -v name="${image}" '
  66             BEGIN {
  67                 name1 = tolower(name)
  68                 name2 = "docker.io/library/" name1
  69                 name3 = "localhost/" name1
  70             }
  71 
  72             { s = tolower($1) }
  73             s == name1 || s == name2 || s == name3 { print; exit }
  74         '
  75     )"
  76 
  77     if [ -z "${pick}" ]; then
  78         printf "image named '%s' not found\n" "${image}" >&2
  79         exit 1
  80     fi
  81 elif [ -z "${image}" ]; then
  82     if ! command -v fzf > /dev/null; then
  83         printf "fzf (interactive picker) not found\n" >&2
  84         exit 2
  85     fi
  86 
  87     data="$(podman images)"
  88     [ -z "${data}" ] && exit 1
  89 
  90     keys='ctrl-a:select-all,ctrl-space:toggle,F10:abort,F12:abort'
  91     picker="fzf --reverse --header-first --header-lines=1 --bind ${keys}"
  92     if [ $# -eq 0 ]; then
  93         msg='podman run -it [image]: enter picks image, escape quits'
  94     else
  95         msg="podman run -it [image] $*: enter picks image, escape quits"
  96     fi
  97 
  98     pick="$(echo "${data}" | ${picker} --header="${msg}")"
  99     [ -z "${pick}" ] && exit 1
 100 fi
 101 
 102 tagged_name="$(echo "${pick}" | awk '{ print $1 ":" $2 }')"
 103 iid="$(echo "${pick}" | awk '{ print $3 }')"
 104 
 105 msgfmt="\n\033[7m%s\033[0m shell from podman image \033[7m%s\033[0m (%s)\n\n"
 106 
 107 [ $# -eq 0 ] && printf "${msgfmt}" "entered" "${tagged_name}" "${iid}" >&2
 108 
 109 podman run -it "${iid}" "$@"
 110 res=$?
 111 
 112 [ $# -eq 0 ] && printf "${msgfmt}" "exited" "${tagged_name}" "${iid}" >&2
 113 
 114 exit "${res}"