From 5950762aec3c0222ceb147a4d22999912d1d8d22 Mon Sep 17 00:00:00 2001 From: GLENN THOMPSON Date: Sun, 22 Sep 2024 08:01:21 +0300 Subject: [PATCH] Refactored symlink creation and added logging --- guile-stash.scm | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/guile-stash.scm b/guile-stash.scm index 27c7f9b..d1a8e61 100644 --- a/guile-stash.scm +++ b/guile-stash.scm @@ -141,22 +141,47 @@ (let ((stat (lstat path))) (eqv? (stat:type stat) 'symlink))) +(define (log-action message) + (with-output-to-file "guile-stash.log" + (lambda () (display message) (newline)) + #:append #t)) + +;; Helper function to build target symlink path +(define (build-target-path source target) + "Builds the symlink path for the source file or directory under the target directory." + (string-append target "/" (basename source))) + +;; Refactored create-symlink function (define (create-symlink source target) "Create a symlink for the source file or directory at the target location." - ;; Determine whether source is a file or directory and adjust the target path accordingly. - (let ((target-symlink-path - (if (file-is-directory? source) - (string-append target "/" (basename source)) ;; If it's a directory, append the directory name to the target - (string-append target "/" (basename source))))) ;; If it's a file, append the file name to the target + (let ((target-symlink-path (build-target-path source target))) ;; Use the helper function (if (file-exists? target-symlink-path) - (handle-conflicts source target-symlink-path) ; Call conflict handler if symlink already exists + (handle-conflicts source target-symlink-path) ;; Call conflict handler if symlink already exists (catch 'system-error (lambda () (symlink source target-symlink-path) + (log-action (format #f "Symlink created: ~a -> ~a" target-symlink-path source)) ;; Log symlink creation (display (color-message (format #f "Symlink created: ~a -> ~a\n" target-symlink-path source) green-text))) (lambda args + (log-action "Error creating symlink") (display (color-message "Error creating symlink.\n" red-text))))))) +;; (define (create-symlink source target) +;; "Create a symlink for the source file or directory at the target location." +;; ;; Determine whether source is a file or directory and adjust the target path accordingly. +;; (let ((target-symlink-path +;; (if (file-is-directory? source) +;; (string-append target "/" (basename source)) ;; If it's a directory, append the directory name to the target +;; (string-append target "/" (basename source))))) ;; If it's a file, append the file name to the target +;; (if (file-exists? target-symlink-path) +;; (handle-conflicts source target-symlink-path) ; Call conflict handler if symlink already exists +;; (catch 'system-error +;; (lambda () +;; (symlink source target-symlink-path) +;; (display (color-message (format #f "Symlink created: ~a -> ~a\n" target-symlink-path source) green-text))) +;; (lambda args +;; (display (color-message "Error creating symlink.\n" red-text))))))) + (define (handle-conflicts source target) ;; Display the conflict message in yellow (warning). (display (color-message (format #f "Conflict detected! A symbolic link already exists at ~a.\n" target) yellow-text))