Compare commits

..
4 Commits
Author SHA1 Message Date
KKlochko df8f580f93 Fix writing to a file only when the option is set.
continuous-integration/drone/push Build is passing
2023-10-02 22:29:22 +03:00
KKlochko 871fc3ed55 Fix checking for file existence only when the options are set. 2023-10-02 22:26:32 +03:00
KKlochko 79a498022e Add the file errors to prevent using an invalid file path.
continuous-integration/drone/push Build is passing
2023-09-28 21:00:17 +03:00
KKlochko 5d56abbee9 Add the output option to save into a file. 2023-09-28 20:59:58 +03:00
2 changed files with 29 additions and 2 deletions
+26 -2
View File
@@ -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]
@@ -68,6 +69,14 @@
(map-has-keys? options [:message, :message-file]) (map-has-keys? options [:message, :message-file])
{:exit-message (error-msg ["You can't use message and message-file options at the same time!!!"])} {:exit-message (error-msg ["You can't use message and message-file options at the same time!!!"])}
(and (contains? options :message-file)
(file/is-file-not-exists? (:message-file options)))
{:exit-message (error-msg ["Please, check the path to the message file!!!"])}
(and (contains? options :output-file)
(file/is-file-exists? (:output-file options)))
{:exit-message (error-msg ["Please, choose another file!!! Overwriting was prevented!!!"])}
(and (map-has-keys? options [:cipher, :key]) (and (map-has-keys? options [:cipher, :key])
(or (contains? options :message) (or (contains? options :message)
(contains? options :message-file)) (contains? options :message-file))
@@ -129,6 +138,19 @@
(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]
(when (contains? options :output-file)
(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 +176,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))))
+3
View File
@@ -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))