clevis 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2017 Red Hat, Inc.
  5. # Author: Nathaniel McCallum <npmccallum@redhat.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. function findexe() {
  21. [ $# -eq 1 ] || return 1
  22. while read -r -d: path; do
  23. [ -f "$path/$1" ] && [ -x "$path/$1" ] && echo "$path/$1" && return 0
  24. done <<< "$PATH:"
  25. return 1
  26. }
  27. cmd=clevis
  28. while [ $# -gt 0 ]; do
  29. [[ "$1" =~ ^- ]] && break
  30. cmd="$cmd-$1"
  31. shift
  32. exe="$(findexe "$cmd")" && exec "$exe" "$@"
  33. done
  34. exec >&2
  35. echo
  36. echo "Command '$cmd' is invalid"
  37. echo
  38. echo "Usage: clevis COMMAND [OPTIONS]"
  39. echo
  40. max=0
  41. for f in "$0"-*; do
  42. [ -f "$f" ] && [ -x "$f" ] || continue
  43. f="${f##*/}"
  44. [ ${#f} -gt $max ] && max=${#f}
  45. done
  46. for f in "$0"-*; do
  47. [ -f "$f" ] && [ -x "$f" ] || continue
  48. summ="$("$f" --summary 2>/dev/null)" || continue
  49. f="${f##*/}"
  50. printf " %-*s %s\n" "$max" "${f//-/ }" "$summ"
  51. done
  52. echo
  53. exit 2