clevis 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. input_commands="$cmd $@"
  29. while [ $# -gt 0 ]; do
  30. [[ "$1" =~ ^- ]] && break
  31. cmd="$cmd-$1"
  32. shift
  33. exe="$(findexe "$cmd")" && exec "$exe" "$@"
  34. done
  35. exec >&2
  36. if [ "$cmd" != "clevis" ];
  37. then
  38. echo
  39. echo "Command '$input_commands' is invalid"
  40. fi
  41. echo
  42. echo "Usage: clevis COMMAND [OPTIONS]"
  43. echo
  44. max=0
  45. for f in "$0"-*; do
  46. [ -f "$f" ] && [ -x "$f" ] || continue
  47. f="${f##*/}"
  48. [ ${#f} -gt $max ] && max=${#f}
  49. done
  50. for f in "$0"-*; do
  51. [ -f "$f" ] && [ -x "$f" ] || continue
  52. summ="$("$f" --summary 2>/dev/null)" || continue
  53. f="${f##*/}"
  54. printf " %-*s %s\n" "$max" "${f//-/ }" "$summ"
  55. done
  56. echo
  57. exit 2