ledgerutil 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/zsh -f
  2. if [[ $# -eq 0 ]] ; then
  3. echo "ledgerutil [ name | list | new name | edit name ]"
  4. exit
  5. fi
  6. function list-utilities () {
  7. for utility in ~/.ledger/utilities/*.(sh|pl) ;
  8. do
  9. case $utility in
  10. *.sh)
  11. echo " - $(basename -- ${utility} .sh)"
  12. ;;
  13. *.pl)
  14. echo " - $(basename -- ${utility} .pl)"
  15. esac
  16. done
  17. }
  18. function new-utility () {
  19. name=$1
  20. shift
  21. cat <<EOF >~/.ledger/utilities/${name}.sh
  22. #!/bin/sh
  23. # Ledger Utility ${name}
  24. EOF
  25. edit-utility ${name}
  26. }
  27. function edit-utility () {
  28. name=$1
  29. shift
  30. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.sh
  31. }
  32. function run-utility () {
  33. name=$1
  34. shift
  35. if [[ -e ~/.ledger/utilities/${name}.sh ]] ; then
  36. ~/.ledger/utilities/${name}.sh $*
  37. else
  38. if [[ -e ~/.ledger/utilities/${name}.pl ]] ; then
  39. ~/.ledger/utilities/${name}.pl $*
  40. else
  41. echo Utility ${name} not defined.
  42. fi
  43. fi
  44. }
  45. name=$1
  46. shift
  47. case ${name} in
  48. list)
  49. list-utilities
  50. ;;
  51. new)
  52. new-utility $*
  53. ;;
  54. edit)
  55. edit-utility $*
  56. ;;
  57. *)
  58. run-utility $name $*
  59. esac