Skip to content

Commit

Permalink
lesson #32
Browse files Browse the repository at this point in the history
  • Loading branch information
astynax committed Mar 5, 2024
1 parent 1ea8506 commit 93eb418
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
80 changes: 80 additions & 0 deletions otus-32/README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#+TITLE: Скрипты на Clojure, Babashka runtime

#+begin_quote
Познакомимся с проектом babashka для написания скриптов на Clojure;
Научимся создавать command line interface для своих скриптов,
автоматизировать рутинные задачи с использованием babashka tasks.
#+end_quote

* [[https://babashka.org/][Babashka]]

Диалект Clojure, имеющий [[https://github.com/babashka/babashka#differences-with-clojure][некоторые отличия]] от оригинала.

** Основы

#+BEGIN_SRC shell :results verbatim

echo "Hello" | bb -e '(str *input* ", World!")'

bb '(+ 1 2 3)'

#+END_SRC

#+BEGIN_SRC shell :results verbatim

echo "{:a 42 :b 100}" | \
bb -IO -e '(let [[{:keys [a b]} & _] *input*] {:result (+ a b)})'

#+END_SRC

** Вызов внешних команд

#+BEGIN_SRC clojure

(println (:out (shell/sh "echo" "123")))

#+END_SRC

#+BEGIN_SRC clojure

(prn (shell/sh "echo" "123"))

#+END_SRC

#+BEGIN_SRC clojure :backend babashka

(println '((1 2 3) (4 5 6)))

#+END_SRC

** CLI args ([[https://github.com/babashka/cli][babashka.cli]])

#+BEGIN_SRC clojure

(require '[babashka.cli :as cli])

(cli/parse-args ["--output" "foo.txt" "--" "bar"])

(cli/parse-opts ["--port=8080"] {:coerce {:port :int}})

(cli/parse-args ["--verbose"])

#+END_SRC

** Задачи и [[file:bb.edn][bb.edn]]

Список задач:

#+BEGIN_SRC shell :results verbatim

bb tasks

#+END_SRC

Выполнение задачи:

#+BEGIN_SRC shell

bb run grep-me

#+END_SRC
12 changes: 12 additions & 0 deletions otus-32/bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{:tasks
{hi
{:doc "Greets you"
:task (shell "echo 123")}

hihi
(do (run 'hi)
(run 'hi))

grep-me
(-> (shell {:out :string} "echo -n '123\n456\n")
(shell "grep 5"))}}
7 changes: 7 additions & 0 deletions otus-32/script.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bb -I

(defn sum-a-and-b []
(let [[{:keys [a b]} & _] *input*]
{:result (+ a b)}))

(prn (sum-a-and-b))

0 comments on commit 93eb418

Please sign in to comment.