mirror of https://codeberg.org/glenneth/stash.git
Refactored symlink creation and added logging
This commit is contained in:
parent
578fda3432
commit
5950762aec
|
|
@ -141,22 +141,47 @@
|
||||||
(let ((stat (lstat path)))
|
(let ((stat (lstat path)))
|
||||||
(eqv? (stat:type stat) 'symlink)))
|
(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)
|
(define (create-symlink source target)
|
||||||
"Create a symlink for the source file or directory at the target location."
|
"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 (build-target-path source target))) ;; Use the helper function
|
||||||
(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)
|
(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
|
(catch 'system-error
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(symlink source target-symlink-path)
|
(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)))
|
(display (color-message (format #f "Symlink created: ~a -> ~a\n" target-symlink-path source) green-text)))
|
||||||
(lambda args
|
(lambda args
|
||||||
|
(log-action "Error creating symlink")
|
||||||
(display (color-message "Error creating symlink.\n" red-text)))))))
|
(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)
|
(define (handle-conflicts source target)
|
||||||
;; Display the conflict message in yellow (warning).
|
;; 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))
|
(display (color-message (format #f "Conflict detected! A symbolic link already exists at ~a.\n" target) yellow-text))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue