From 9e4c46814c42533cedd05c51ee6f9ac194f47608 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 21 Sep 2023 20:55:43 +0300 Subject: [PATCH] Update the CLI to have separated actions and an option initializer. --- src/cipher_analytical_machine/cli.clj | 72 ++++++++++++++++++++------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/src/cipher_analytical_machine/cli.clj b/src/cipher_analytical_machine/cli.clj index 8b51deb..d98f1e9 100644 --- a/src/cipher_analytical_machine/cli.clj +++ b/src/cipher_analytical_machine/cli.clj @@ -88,29 +88,65 @@ (println exit-message) (System/exit (if status 0 1))) -(defn get-message - "Return the message" - [options arguments] - (cond - (contains? options :message) - (:message options) - - (contains? options :message-file) - (file/read-file (:message-file options)))) +(defn get-or-load-option + "Return the option value or load the value from a file." + [options arguments option] + (let [file-option (keyword (str (name option) "-file"))] + (cond + (contains? options option) + (option options) + + (contains? options file-option) + (file/read-file (file-option options))))) + +(defn set-option + "Set a option in the option map." + [options option value] + (-> options + (assoc option value))) + +(defn load-and-set-option + "Load the option and set it." + [options option] + (->> (get-or-load-option options nil option) + (set-option options option))) + +(defn load-all-options + "Load all options." + [options] + (-> options + (load-and-set-option :message))) + +(defn cracking-actions + [options arguments action-type] + (let [message (:message options) + symbols (:symbols options) + frequencies symbol-frequences/english-letter-frequences] + (cond + (= action-type :cracking) + (caesar-analyzers/get-plaintext message symbols frequencies) + ))) -(defn actions +(defn crypt-actions [options arguments action-type] - (let [message (get-message options arguments) + (let [message (:message options) key (Integer/parseInt (:key options)) symbols (:symbols options)] + (cond + (= action-type :encrypt) + (caesar/encrypt-message message key symbols) + + (= action-type :decrypt) + (caesar/decrypt-message message key symbols)))) + +(defn actions + [options arguments action-type] + (let [options (load-all-options options)] (println (cond - (= action-type :cracking) - (caesar-analyzers/get-plaintext message symbols symbol-frequences/english-letter-frequences) - - (= action-type :encrypt) - (caesar/encrypt-message message key symbols) + (contains? #{:encrypt :decrypt} action-type) + (crypt-actions options arguments action-type) - (= action-type :decrypt) - (caesar/decrypt-message message key symbols))))) + (= action-type :cracking) + (cracking-actions options arguments action-type)))))