Refactored symlink creation and added logging

This commit is contained in:
GLENN THOMPSON 2024-09-22 08:01:21 +03:00
parent 578fda3432
commit 5950762aec
1 changed files with 31 additions and 6 deletions

View File

@ -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))