From 5d56abbee92dec1ad295aec0d08746454ff7e5cf Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 28 Sep 2023 20:58:55 +0300 Subject: [PATCH] Add the output option to save into a file. --- src/cipher_analytical_machine/cli.clj | 19 +++++++++++++++++-- src/cipher_analytical_machine/file.clj | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cipher_analytical_machine/cli.clj b/src/cipher_analytical_machine/cli.clj index 83d09d6..a201c9e 100644 --- a/src/cipher_analytical_machine/cli.clj +++ b/src/cipher_analytical_machine/cli.clj @@ -28,6 +28,7 @@ ["-l" "--language CODE" "The string will be used to set a default symbols for a cipher." :validate [#(contains? language-options %) "Must be a code from the list: en, uk!!!"]] ["-C" "--cracking" "Cracking the encrypted message."] + ["-O" "--output-file OUTPUT-FILE" "Save the program output to a file."] ["-h" "--help"]]) (defn usage [options-summary] @@ -129,6 +130,18 @@ (set-symbols) (load-and-set-option :message))) +(defn save-output + "Save the output to a file" + [output options] + (let [file-path (:output-file options)] + (file/write-file file-path output))) + +(defn show-and-save-output + "Print and save the output to a file if needed" + [output options] + (save-output output options) + (println output)) + (defn cracking-actions [options arguments action-type] (let [message (:message options) @@ -154,11 +167,13 @@ (defn actions [options arguments action-type] (let [options (load-all-options options)] - (println + (-> (cond (contains? #{:encrypt :decrypt} action-type) (crypt-actions options arguments action-type) (= action-type :cracking) - (cracking-actions options arguments action-type))))) + (cracking-actions options arguments action-type)) + + (show-and-save-output options)))) diff --git a/src/cipher_analytical_machine/file.clj b/src/cipher_analytical_machine/file.clj index b53ab52..bc58876 100644 --- a/src/cipher_analytical_machine/file.clj +++ b/src/cipher_analytical_machine/file.clj @@ -16,3 +16,6 @@ (defn read-file [file-path] (slurp file-path)) +(defn write-file [file-path content] + (spit file-path content)) +