Add the output option to save into a file.

main
KKlochko 2 years ago
parent 4607f7f013
commit 5d56abbee9

@ -28,6 +28,7 @@
["-l" "--language CODE" "The string will be used to set a default symbols for a cipher." ["-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!!!"]] :validate [#(contains? language-options %) "Must be a code from the list: en, uk!!!"]]
["-C" "--cracking" "Cracking the encrypted message."] ["-C" "--cracking" "Cracking the encrypted message."]
["-O" "--output-file OUTPUT-FILE" "Save the program output to a file."]
["-h" "--help"]]) ["-h" "--help"]])
(defn usage [options-summary] (defn usage [options-summary]
@ -129,6 +130,18 @@
(set-symbols) (set-symbols)
(load-and-set-option :message))) (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 (defn cracking-actions
[options arguments action-type] [options arguments action-type]
(let [message (:message options) (let [message (:message options)
@ -154,11 +167,13 @@
(defn actions (defn actions
[options arguments action-type] [options arguments action-type]
(let [options (load-all-options options)] (let [options (load-all-options options)]
(println (->
(cond (cond
(contains? #{:encrypt :decrypt} action-type) (contains? #{:encrypt :decrypt} action-type)
(crypt-actions options arguments action-type) (crypt-actions options arguments action-type)
(= action-type :cracking) (= action-type :cracking)
(cracking-actions options arguments action-type))))) (cracking-actions options arguments action-type))
(show-and-save-output options))))

@ -16,3 +16,6 @@
(defn read-file [file-path] (defn read-file [file-path]
(slurp file-path)) (slurp file-path))
(defn write-file [file-path content]
(spit file-path content))

Loading…
Cancel
Save