Bang.sh - Bash Script no Velho Oeste

Bang.sh - Bash Script no Velho Oeste

Numa cidade sem lei do Faroeste, todos possuem revólveres engatilhados pronto para atirar naqueles que escrevem códigos ilegíveis e difíceis de manter. Bang.sh é um framework escrito em Bash Script que pretende torná-lo o gatilho mais rápido do Oeste: garanta a pontaria através de testes unitários - incluindo mocking; dispare exceções como se fossem balas e as trate como damas de um Saloon; desfrute de um options parser e uma série de funções utilitárias que dão mais conforto que a maioria dos hotéis da cidade. De forma totalmente modular, desenvolva e teste seus scripts e se torne um Xerife no Velho Oeste!

4a849f1aa47e5c99f1aa156337f6c951?s=128

Gustavo Dutra

August 07, 2014
Tweet

Transcript

  1. Bang.sh Bash Script no Velho Oeste

  2. Gustavo Dutra ! ! 
 @gustavotkg @bellthoven

  3. None
  4. BANG.SH

  5. BASH 4

  6. ¯\_(π)_/¯

  7. None
  8. good practices

  9. TODO LIST

  10. None
  11. $ ./todo add “My first todo” ! $ ./todo list


    
 $ ./todo del 1
  12. Installing Bang.sh

  13. $ cd ~/Projects

  14. $ git clone bellthoven/bangsh.git

  15. $ export PATH=“$PATH:~/Projects/bangsh/bin"

  16. $ type bang bang is ~/Projects/bangsh/bin/bang

  17. bootstrapping

  18. $ bang new todo $ bang new src/todo $ bang

    new my_app
  19. -rwxr-xr-x todo ! drwxr-xr-x tasks/ ! drwxr-xr-x modules/

  20. $ cat todo #!/usr/bin/env bash
 source '~/Projects/bangsh/src/bang.sh'

  21. $ ./todo add “My first todo” ! $ ./todo list


    
 $ ./todo del 1
  22. $ ls ../bangsh/src/modules/ ! 
 module.sh opt.sh ! path.sh str.sh

    ! task.sh unittest.sh
  23. organization

  24. #!/usr/bin/env bash source '~/Projects/bangsh/src/bang.sh' ! b.module.require task ! b.task.run "$@"

    ./todo
  25. $ ./todo add “Item 1”
 =
 
 b.task.run add “Item

    1"
  26. function btask.add.run () { echo "$1" >> $HOME/.todolist } ./tasks/add.sh

  27. function btask.add.run () { echo "$1" >> $HOME/.todolist } ./tasks/add.sh

  28. function btask.add.run () { echo "$1" >> $HOME/.todolist } ./tasks/add.sh

  29. $ ./todo add “My first todo" ! ! $ cat

    ~/.todolist My first todo
  30. CONFIGS

  31. file=/Users/gustavo/.todolist ~/.todorc

  32. #!/usr/bin/env bash source '~/Projects/bangsh/src/bang.sh' ! b.module.require task b.module.require path b.module.require

    config ! todo.config.load ~/.todorc ! b.task.run "$@" ./todo
  33. function todo.config.load () { if b.path.file? "$1"; then _todo.config.load_from_file "$1"

    else _todo.config.load_default_config fi } ./modules/config.sh
  34. function todo.config.load () { if b.path.file? "$1"; then _todo.config.load_from_file "$1"

    else _todo.config.load_default_config fi } ./modules/config.sh
  35. function todo.config.load () { if b.path.file? "$1"; then _todo.config.load_from_file "$1"

    else _todo.config.load_default_config fi } ./modules/config.sh
  36. function _todo.config.load_from_file () { for line in $(cat "$1"); do

    # file=/Users/gustavo/.todolist local option="$(echo $line | cut -f1 -d=)" local value="$(echo $line | cut -f2 -d=)" ! b.set "todo.config.$option" "$value" done } ./modules/config.sh
  37. function _todo.config.load_from_file () { for line in $(cat "$1"); do

    # file=/Users/gustavo/.todolist local option="$(echo $line | cut -f1 -d=)" local value="$(echo $line | cut -f2 -d=)" ! b.set "todo.config.$option" "$value" done } ./modules/config.sh
  38. function _todo.config.load_from_file () { for line in $(cat “$1"); do

    # file=/Users/gustavo/.todolist local option="$(echo $line | cut -f1 -d=)" local value="$(echo $line | cut -f2 -d=)" ! b.set "todo.config.$option" "$value" done } ./modules/config.sh
  39. function _todo.config.load_from_file () { for line in $(cat "$1"); do

    # file=/Users/gustavo/.todolist local option="$(echo $line | cut -f1 -d=)" local value="$(echo $line | cut -f2 -d=)" ! b.set "todo.config.$option" "$value" done } ./modules/config.sh
  40. function todo.config.load () { if b.path.file? "$1"; then _todo.config.load_from_file "$1"

    else _todo.config.load_default_config fi } ./modules/config.sh
  41. function todo.config.load () { if b.path.file? "$1"; then _todo.config.load_from_file "$1"

    else _todo.config.load_default_config fi } ./modules/config.sh
  42. function _todo.config.load_default_config () { b.set "todo.config.file" "$HOME/.todolist" } ./modules/config.sh

  43. function _todo.config.load_default_config () { b.set "todo.config.file" "$HOME/.todolist" } ./modules/config.sh

  44. function btask.add.run () { local filename="$(b.get todo.config.file)" ! echo "$1"

    >> "$filename" } ./tasks/add.sh
  45. function btask.add.run () { local filename="$(b.get todo.config.file)" ! echo "$1"

    >> "$filename" } ./tasks/add.sh
  46. AGAIN

  47. ./todo • Load bang.sh • Require modules (task, path, config)

    • Read the ~/.todorc the file and set options • Run the task
  48. ./modules/config.sh • Create the todo.config.load public function • Create _todo.config.load_from_file

    and _todo.config.load_default_options private functions
  49. ./tasks/add.sh • Create the btask.add.run function • Write the todo

    item to the configured file
  50. LISTING

  51. function btask.list.run () { local filename="$(b.get 'todo.config.file')" ! cat -n

    "$filename" } ./tasks/list.sh
  52. function btask.list.run () { local filename="$(b.get 'todo.config.file')" ! cat -n

    "$filename" } ./tasks/list.sh
  53. $ ./todo list 1 My first todo 2 My second

    todo
  54. DELETING

  55. function btask.del.run () { local num=“$1” \ filepath=$(b.get 'todo.config.file') !

    sed -i '' "${num}d" "$filepath" } ./tasks/del.sh
  56. function btask.del.run () { local num=“$1” \ filepath=$(b.get 'todo.config.file') !

    sed -i '' "${num}d" "$filepath" } ./tasks/del.sh
  57. $ ./todo list 1 My first todo 2 My second

    todo
  58. $ ./todo del 1

  59. $ ./todo list 1 My second todo

  60. testing

  61. ! function b.test.load_configuration_from_file () { local filename=$(mktemp -t config_file.XXXX) echo

    "file=some/path" > "$filename" ! b.unittest.assert_equal "" \ ”$(b.get todo.config.file)” ! todo.config.load "$filename" ! b.unittest.assert_equal "some/path" \ ”$(b.get todo.config.file)" ! rm “$filename" } ./tests/modules/config_test.sh
  62. ! function b.test.load_configuration_from_file () { local filename=$(mktemp -t config_file.XXXX) echo

    "file=some/path" > "$filename" ! b.unittest.assert_equal "" \ ”$(b.get todo.config.file)” ! todo.config.load "$filename" ! b.unittest.assert_equal "some/path" \ ”$(b.get todo.config.file)" ! rm “$filename" } ./tests/modules/config_test.sh
  63. ! function b.test.load_configuration_from_file () { local filename=$(mktemp -t config_file.XXXX) echo

    "file=some/path" > "$filename" ! b.unittest.assert_equal "" \ ”$(b.get todo.config.file)” ! todo.config.load "$filename" ! b.unittest.assert_equal "some/path" \ ”$(b.get todo.config.file)" ! rm “$filename" } ./tests/modules/config_test.sh
  64. ! function b.test.load_configuration_from_file () { local filename=$(mktemp -t config_file.XXXX) echo

    "file=some/path" > "$filename" ! b.unittest.assert_equal "" \ ”$(b.get todo.config.file)” ! todo.config.load "$filename" ! b.unittest.assert_equal "some/path" \ ”$(b.get todo.config.file)" ! rm “$filename" } ./tests/modules/config_test.sh
  65. ! function b.test.load_configuration_from_file () { local filename=$(mktemp -t config_file.XXXX) echo

    "file=some/path" > "$filename" ! b.unittest.assert_equal "" \ ”$(b.get todo.config.file)” ! todo.config.load "$filename" ! b.unittest.assert_equal "some/path" \ ”$(b.get todo.config.file)" ! rm “$filename" } ./tests/modules/config_test.sh
  66. $ bang test . real 0m0.083s user 0m0.044s sys 0m0.054s

    ! Tests (Passed: 1 / Failed: 0)
  67. function b.unittest.setup () { b.set 'todo.config.file' "$(mktemp -t config_file.XXXX)" }

    ! function b.unittest.teardown () { rm $(b.get 'todo.config.file') } ! function b.test.adds_a_todo_to_the_file () { local config_file=$(b.get 'todo.config.file') ! b.unittest.assert_equal "" "$(cat $config_file)" b.task.run "add" "My todo item" b.unittest.assert_equal "My todo item" "$(cat $config_file)" } ./tests/tasks/add_test.sh
  68. function b.unittest.setup () { b.set 'todo.config.file' "$(mktemp -t config_file.XXXX)" }

    ! function b.unittest.teardown () { rm $(b.get 'todo.config.file') } ! function b.test.adds_a_todo_to_the_file () { local config_file=$(b.get 'todo.config.file') ! b.unittest.assert_equal "" "$(cat $config_file)" b.task.run "add" "My todo item" b.unittest.assert_equal "My todo item" "$(cat $config_file)" } ./tests/tasks/add_test.sh
  69. function b.unittest.setup () { b.set 'todo.config.file' "$(mktemp -t config_file.XXXX)" }

    ! function b.unittest.teardown () { rm $(b.get 'todo.config.file') } ! function b.test.adds_a_todo_to_the_file () { local config_file=$(b.get 'todo.config.file') ! b.unittest.assert_equal "" "$(cat $config_file)" b.task.run "add" "My todo item" b.unittest.assert_equal "My todo item" "$(cat $config_file)" } ./tests/tasks/add_test.sh
  70. function b.unittest.setup () { b.set 'todo.config.file' "$(mktemp -t config_file.XXXX)" }

    ! function b.unittest.teardown () { rm $(b.get 'todo.config.file') } ! function b.test.adds_a_todo_to_the_file () { local config_file=$(b.get 'todo.config.file') ! b.unittest.assert_equal "" "$(cat $config_file)" b.task.run "add" "My todo item" b.unittest.assert_equal "My todo item" "$(cat $config_file)" } ./tests/tasks/add_test.sh
  71. $ bang test .. real 0m0.083s user 0m0.044s sys 0m0.054s

    ! Tests (Passed: 2 / Failed: 0)
  72. $ ./todo add “My first todo” ! $ ./todo list


    
 $ ./todo del 1
  73. bellthoven/todo-sample

  74. arsenal • http://github.com/bellthoven/bangsh • http://bangsh.com/ • http://bangsh.com/api/

  75. moving forward

  76. Thanks

  77. • http://commons.wikimedia.org/wiki/File%3AWild_west_show.jpg • http://www.flickr.com/photos/us_mission_canada/8513394054 • http://commons.wikimedia.org/wiki/File:Cowboy_Action_Shooting.JPG • http://en.wikipedia.org/wiki/ File:Warner_Brothers_television_westerns_stars_1959.JPG •

    "Women's cowboy boots" by Pete unseth - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons - http:// commons.wikimedia.org/wiki/File:Women %27s_cowboy_boots.JPG#mediaviewer/File:Women%27s_cowboy_boots.JPG • http://commons.wikimedia.org/wiki/File:Saint-L %C3%B4_Railway_station_destroyed.jpg • http://www.flickr.com/photos/matthigh/2982689681 • http://www.flickr.com/photos/kirillsimonov/5442124915/