shell dot on steroids https://pagure.io/shellfu

ffoo.3.sh 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. # ~/bin/example
  3. . <(ffoom init)
  4. ##
  5. ## pretty.sh - make output pretty and pretty useful
  6. ##
  7. ffoo import pretty
  8. # three levels should be enough for everyone
  9. #
  10. think "about saying something..." # thinks loud if FFOO_VERBOSE
  11. warn "this is just an example" # obvious :)
  12. true || die "true was false!" # Perlism :)
  13. # several ways of debugging
  14. #
  15. x=1; y=2; z=3
  16. debug -v x y z # debug:example.sh:x='1' ...
  17. echo "hello" | debug_pipe mypipe # debug|mypipe|: hello
  18. warn -f should_be_empty # warns with name+content
  19. # unless the file is empty
  20. myfun() {
  21. num=42
  22. debug -v num # reveals context!
  23. } # vvv
  24. myfun # debug:myfun: num='42'
  25. # but you can throw more at yourself
  26. #
  27. debug -c ps aux # when more is more
  28. debug -f /etc/hosts
  29. # output can be whatever you think is pretty:
  30. #
  31. FFOO_PRETTY=color
  32. FFOO_PRETTY=html
  33. #...
  34. FFOO_PRETTY=your_own_format
  35. ##
  36. ## config.sh -- config file grepping
  37. ##
  38. ffoo import config
  39. # a sample INI file
  40. #
  41. mkdir $HOME/.demo
  42. cat >$HOME/.demo/foo.ini <<EOF
  43. # a sample INI file
  44. [foo]
  45. multiline = also possible
  46. multiline = like this :)
  47. [foo.bar]
  48. # spaces after equal sign are trimmed by default
  49. baz = qux
  50. baz2 = public qux
  51. [foo.script]
  52. # but can be preserved using -S|--strict mode
  53. s1 =#!/usr/bin/python
  54. s1 =def main():
  55. s1 = print "will work!"
  56. EOF
  57. # read as you need
  58. #
  59. FFOO_CONFIG_PATH=$HOME/.demo # just tell us the path
  60. cfgrep -s foo.bar -k baz foo.ini # prints "qux"
  61. cfgrep -p foo.bar.baz foo.ini # ...short for above
  62. cfgrep -p foo.bar.baz # ...even shorter (foo hints foo.ini)
  63. # store even indented text
  64. #
  65. cfgrep -S -p foo.script python >s1.py # the -S will preserve spaces on RHS
  66. python s1.py # so this will work
  67. # merge INIs on runtime
  68. #
  69. mkdir $HOME/.demo/private
  70. cat >$HOME/.demo/private/foo.ini <<EOF
  71. [foo.bar]
  72. baz2 = secret qux
  73. EOF
  74. # this works similar to PATH
  75. #
  76. FFOO_CONFIG_PATH=$HOME/.demo-private:$HOME/.demo
  77. cfgrep -p foo.bar.baz2 # the private file wins -- "secret qux!"
  78. cfgrep -j -p foo.bar.baz2 # but -j causes them to be joined:
  79. # secret qux
  80. # public qux
  81. #
  82. # recon.sh -- common tasks to "look around"
  83. #
  84. ffoo import recon
  85. # wait for better times
  86. #
  87. wait_until -e ! test -f badfile # -e means use eval (make "!" work)
  88. think "the badfile is away..."
  89. bad_file_away() { # but you can avoid eval
  90. ! test -f badfile
  91. }
  92. wait_until bad_file_away
  93. # smart-match PIDs
  94. #
  95. sleep 100 &
  96. ./myscript-1.py
  97. ./myscript-2.py
  98. sleep 100 &
  99. for pid in $(pids_matching "^myscript-" "^sleep$");
  100. do
  101. kill $pid
  102. kill -9 $pid
  103. done
  104. # age querying
  105. #
  106. yum -y install something-to-usr
  107. sleep 10
  108. age_of_tree /usr # should be ~10
  109. # readiness checks, great food for wait_until
  110. #
  111. wait_until listening_on 8888 # locally!
  112. wait_until reachable_by_ping my_host
  113. wait_until reachable_by_tcp 25 my_host
  114. wait_until reachable_by_ssh my_host
  115. cat all_hosts | filter_hosts # will list those that ponged
  116. cat all_hosts | filter_hosts -P -s # ...no ping but SSH
  117. cat all_hosts | filter_hosts -t 25 # ...ping and TCP 25
  118. # cat but for URIs!
  119. #
  120. wait_until listening_on 8888
  121. cat_uri http://localhost:8888 | grep OK
  122. ##
  123. ## ... and more: types.sh, exits.sh, tmp.sh
  124. ##
  125. ffoo import testing
  126. register_artifact future.log # does not need to exist yet
  127. myprog > future.log
  128. collect_artifacts # will preserve tree from /
  129. # and produce folder called e.g.
  130. # artifacts-20140915-141502