ledgerutil 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/zsh -f
  2. if [[ $# -eq 0 ]] ; then
  3. echo "ledgerutil [ name | list | new name [ perl | shell | python ] | edit name ]"
  4. exit
  5. fi
  6. function list-utilities () {
  7. for utility in ~/.ledger/utilities/*.(sh|pl|py) ;
  8. do
  9. case $utility in
  10. *.sh)
  11. echo " - $(basename -- ${utility} .sh)"
  12. ;;
  13. *.pl)
  14. echo " - $(basename -- ${utility} .pl)"
  15. ;;
  16. *.py)
  17. echo " - $(basename -- ${utility} .py)"
  18. esac
  19. done
  20. }
  21. function new-utility () {
  22. if [[ $# -lt 2 ]] ; then
  23. echo "ledgerutil new name ( perl / shell / python )"
  24. exit 1
  25. fi
  26. name=$1
  27. shift
  28. type=$1
  29. case $type in
  30. perl)
  31. cat <<EOF > ~/.ledger/utilities/${name}.pl
  32. #!/bin/perl
  33. use strict;
  34. use warnings;
  35. # Ledger Utility ${name}
  36. EOF
  37. ;;
  38. python)
  39. cat <<EOF > ~/.ledger/utilities/${name}.py
  40. #!/bin/python
  41. import ledger
  42. # Ledger Utility ${name}
  43. EOF
  44. ;;
  45. *)
  46. cat <<EOF > ~/.ledger/utilities/${name}.sh
  47. #!/bin/sh
  48. # Ledger Utility ${name}
  49. EOF
  50. esac
  51. edit-utility ${name}
  52. }
  53. function edit-utility () {
  54. if [[ $# -lt 1 ]] ; then
  55. echo "ledgerutil edit name"
  56. exit 1
  57. fi
  58. name=$1
  59. shift
  60. if [[ -e ~/.ledger/utilities/${name}.sh ]] ; then
  61. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.sh
  62. elif [[ -e ~/.ledger/utilities/${name}.pl ]] ; then
  63. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.pl
  64. elif [[ -e ~/.ledger/utilities/${name}.py ]] ; then
  65. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.py
  66. fi
  67. }
  68. function run-utility () {
  69. name=$1
  70. shift
  71. if [[ -e ~/.ledger/utilities/${name}.sh ]] ; then
  72. ~/.ledger/utilities/${name}.sh $*
  73. elif [[ -e ~/.ledger/utilities/${name}.pl ]] ; then
  74. ~/.ledger/utilities/${name}.pl $*
  75. elif [[ -e ~/.ledger/utilities/${name}.py ]] ; then
  76. ~/.ledger/utilities/${name}.py $*
  77. else
  78. echo Utility ${name} not defined.
  79. fi
  80. }
  81. name=$1
  82. shift
  83. case ${name} in
  84. list)
  85. list-utilities
  86. ;;
  87. new)
  88. new-utility $*
  89. ;;
  90. edit)
  91. edit-utility $*
  92. ;;
  93. *)
  94. run-utility $name $*
  95. esac