|
146 | 146 | (tools.util/single-text-content (string/join "\n" paths)) |
147 | 147 | (tools.util/single-text-content "No files found for given pattern" :error))))) |
148 | 148 |
|
| 149 | +(defn file-change-full-content [path original-content new-content all?] |
| 150 | + (let [original-full-content (slurp path) |
| 151 | + new-full-content (if all? |
| 152 | + (string/replace original-full-content original-content new-content) |
| 153 | + (string/replace-first original-full-content original-content new-content))] |
| 154 | + (when (string/includes? original-full-content original-content) |
| 155 | + {:original-full-content original-full-content |
| 156 | + :new-full-content new-full-content}))) |
| 157 | + |
149 | 158 | (defn ^:private edit-file [arguments {:keys [db]}] |
150 | 159 | (or (tools.util/invalid-arguments arguments (concat (path-validations db) |
151 | 160 | [["path" fs/readable? "File $path is not readable"]])) |
152 | 161 | (let [path (get arguments "path") |
153 | 162 | original-content (get arguments "original_content") |
154 | 163 | new-content (get arguments "new_content") |
155 | | - all? (boolean (get arguments "all_occurrences")) |
156 | | - content (slurp path)] |
157 | | - (if (string/includes? content original-content) |
158 | | - (let [content (if all? |
159 | | - (string/replace content original-content new-content) |
160 | | - (string/replace-first content original-content new-content))] |
161 | | - (spit path content) |
| 164 | + all? (boolean (get arguments "all_occurrences"))] |
| 165 | + (if-let [{:keys [new-full-content]} (file-change-full-content path original-content new-content all?)] |
| 166 | + (do |
| 167 | + (spit path new-full-content) |
162 | 168 | (tools.util/single-text-content (format "Successfully replaced content in %s." path))) |
163 | 169 | (tools.util/single-text-content (format "Original content not found in %s" path) :error))))) |
164 | 170 |
|
|
0 commit comments