Clojure Command Line Application Tutorial

preview_player
Показать описание
This video covers how to create a password generator for the command line using Clojure.

tools used:

0:00 Intro
0:14 Demo
0:47 Project Setup
02:55 Start REPL
03:36 Create password namespace to generate password
08:00 Create Clipboard namespace
16:45 Colour output text
17:27 Create Uberjar
18:27 Install GraalVM
18:39 Compile utility using Native Image
19:00 Use command-line application
19:23 Outro + asking for subs 🙏
Рекомендации по теме
Комментарии
Автор

nice balance of scope / speed / friendliness

agumonkey
Автор

Regarding Step 1 (generating password):

Usage of reduce and loop in available-chars and generate-password seems unnecessarily complex to me. I'd probably use something simpler like

(def available-chars
(map char (range 33 123)))

(defn generate-password [length]
(str/join (take length (repeatedly #(rand-nth

(generate-password 3)
;; => "tiG"
;; => "re#"
;; => "7U)"
(generate-password 10)
;; => "8D7ydA+u]M"
;; => "D]+1mBGTHB"
;; => "&c>D>.nC_m"

TomasBrejla
Автор

I really enjoyed this :) Thanks!

Are you in SA?

jacobclarkson
Автор

At 9:18 you're using `sun.lwawt.macosx.CClipboard` as it was the actual class name that got returned to you in REPL output window. As the name suggests, that class will only be returned on Mac OS, not on other OSs. For example in linux, I'm getting 'sun.awt.X11.XClipboard`.

You probably should have used the class instead as that's the class that `Toolkit/getSystemClipboard` promises to return anyway (and `setContents` method is defined on it).

Btw there's a fun way to find the information about that specific class/method (for example the mentioned `Toolkit/getSystemClipboard`) without googling for javadocs:

(->> (clojure.reflect/type-reflect 'java.awt.Toolkit)
:members
(filter #(= (:name %) 'getSystemClipboard)))

Of course you can simply issue (clojure.reflect/type-reflect 'java.awt.Toolkit) first and then search for getSystemClipboard manually

it returns information such as..

({:name getSystemClipboard,
:return-type java.awt.datatransfer.Clipboard,
:declaring-class java.awt.Toolkit,
:parameter-types [],
:exception-types [java.awt.HeadlessException],
:flags #{:public :abstract}})

`clojure.reflect` namespace FTW!

TomasBrejla
Автор

Thanks, very well done. Why the cli hangs for a few seconds after generating the password?

PedroGabriel-jrrb
Автор

That's strange that the native executable doesn't work like instantly.. There seems to be initialization delay or something. Maybe try building native image with "--no-fallback" option. Hmmmm

eugenej.
Автор

Please, checkout babashka (clj) and nbb (cljs)

PeterLeder
welcome to shbcf.ru