Last active
March 29, 2026 12:39
-
-
Save ishideo/3c55385e07cf1362dd1fac3f01b5e81c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bb | |
| (ns ip.guide.client | |
| (:require | |
| [babashka.process :refer [shell]] | |
| [cheshire.core :as json])) | |
| (def ip-guide-url "https://ip.guide") | |
| (defn fetch-ip-json [] | |
| (let [{:keys [exit out err]} | |
| (shell {:out :string} | |
| "curl" "-6" "-sL" ip-guide-url)] | |
| (when (not (zero? exit)) | |
| (binding [*out* *err*] | |
| (println "Failed to fetch IP info from" ip-guide-url ":" err)) | |
| (System/exit exit)) | |
| out)) | |
| (defn parse-ip [json-str] | |
| (-> json-str | |
| (json/parse-string true) ; keywordize keys | |
| :ip)) | |
| (defn -main [& _args] | |
| (println (parse-ip (fetch-ip-json)))) | |
| (when (= *file* (System/getProperty "babashka.file")) | |
| (apply -main *command-line-args*)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bb | |
| (require '[babashka.process :refer [shell]]) | |
| (require '[cheshire.core :as json]) | |
| (println | |
| (:ip | |
| (json/parse-string | |
| (:out (shell {:out :string} "curl" "-6" "-sL" "https://ip.guide")) | |
| true))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bb -e '(clojure.java.shell.sh "curl" "-6" "-sL" "ip.guide")' | bb --stream '(:out *input*)' | bb --stream '(cheshire.core.parse-string *input* true)' | bb --stream '(println (:ip *input*))' | |
| # bb -e '(curl/get "ip.guide" {:headers {"Accept" "application/json"}})' | bb -I '(cheshire.core.parse-string (:body (first *input*)) true)' | bb -I '(println (:ip (first *input*)))' | |
| # bb -e '(curl/get "ip.guide" {:headers {"Accept" "application/json"}})' | bb --stream '(cheshire.core.parse-string (:body *input*) true)' | bb --stream '(println (:ip *input*))' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment