Skip to content

Instantly share code, notes, and snippets.

@cnirrad
Created March 19, 2021 00:11
Show Gist options
  • Save cnirrad/fa69722d1549246381039d188165335a to your computer and use it in GitHub Desktop.
Save cnirrad/fa69722d1549246381039d188165335a to your computer and use it in GitHub Desktop.
Babashka script to encode/decode Base64
#!/usr/bin/env bb
(require '[babashka.process :refer [process]]
'[clojure.java.io :as io]
'[clojure.tools.cli :refer [parse-opts]])
(def cli-options
[["-d" "--decode" "Decode Base64" :default false]
["-e" "--encode" "Encode Base64" :default false]])
(def options (parse-opts *command-line-args* cli-options))
(def args (:arguments options))
(def flags (:options options))
(defn decode [b64]
(let [decoder (java.util.Base64/getDecoder)
resultBytes (.decode decoder b64)]
(String. resultBytes)))
(defn encode [txt]
(let [encoder (java.util.Base64/getEncoder)
resultBytes (.encode encoder (.getBytes txt))]
(String. resultBytes)))
(defn copy-to-clipboard [txt]
(let [p (process '[clip])
stdin (io/writer (:in p))]
(binding [*out* stdin]
(println txt))
(.close stdin)))
(let [result (cond
(:decode flags) (decode (first args))
(:encode flags) (encode (first args))
:else (decode (first args)))]
(println result)
(copy-to-clipboard result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment