mirror of https://codeberg.org/glenneth/stash.git
100 lines
4.3 KiB
Scheme
100 lines
4.3 KiB
Scheme
;;; stash.scm --- A Guile script for moving directories and creating symlinks with conflict resolution
|
|
;;;
|
|
;;; Author: Glenn Thompson <glenn@kirstol.org>
|
|
;;; Version: 0.1.0-alpha.1
|
|
;;; Created: 2024-12-03
|
|
;;; Compatibility: Guile 3.0.9
|
|
;;; Keywords: symlink, file management, conflict resolution, backup
|
|
;;;
|
|
;;; Commentary:
|
|
;;;
|
|
;;; Stash is a command-line utility written in Guile Scheme designed to facilitate the movement of directories and
|
|
;;; creation of symbolic links (symlinks) for files and directories. It allows users to specify a source directory
|
|
;;; and a target directory where the symlink should be created. The utility handles potential conflicts with existing
|
|
;;; files or directories at the target location, offering users the choice to overwrite, back up, or skip the creation of new symlinks.
|
|
;;;
|
|
;;; Main Features:
|
|
;;; - Command-line argument parsing for specifying source and target paths.
|
|
;;; - Conflict detection with interactive user resolution options (overwrite, backup, skip, or cancel).
|
|
;;; - Moving directories and creating symlinks.
|
|
;;; - Simple and interactive user interface for easy use.
|
|
;;;
|
|
;;; Usage:
|
|
;;;
|
|
;;; guile -L . stash.scm --target=<target-dir> --source=<source-dir>
|
|
;;;
|
|
;;; Replace <target-dir> with the directory where you want the symlink to be created,
|
|
;;; and <source-dir> with the path to the source directory.
|
|
;;;
|
|
;;; License:
|
|
;;;
|
|
;;; This project is licensed under the GNU General Public License v3.
|
|
;;;
|
|
|
|
;;; CODE
|
|
|
|
(use-modules (ice-9 getopt-long)
|
|
(stash help) ;; Help module
|
|
(stash colors) ;; ANSI colors
|
|
(stash log) ;; Logging module
|
|
(stash paths) ;; Path handling module
|
|
(stash conflict) ;; Conflict resolution module
|
|
(stash file-ops) ;; File and symlink operations module
|
|
(srfi srfi-1)
|
|
(srfi srfi-19))
|
|
|
|
;;; Version function
|
|
(define (display-version)
|
|
"Display the current version of the program."
|
|
(newline)
|
|
(display "Stash version 0.1.0-alpha.1\n")
|
|
(exit 0))
|
|
|
|
;;; Helper function to check for version flag and display version
|
|
(define (check-for-version args)
|
|
"Check if --version or -v is in the arguments list."
|
|
(if (or (member "--version" args)
|
|
(member "-v" args))
|
|
(display-version)))
|
|
|
|
;;; Main function to handle source and target operations
|
|
(define (handle-source-and-target source-dir target-dir)
|
|
"Move the source directory to the target and create a symlink in the parent directory."
|
|
(let ((target-source-dir (move-source-to-target source-dir target-dir))) ;; Only pass source-dir and target-dir
|
|
(create-symlink-in-parent source-dir target-source-dir))) ;; Create the symlink if no conflict
|
|
|
|
;;; Function to handle command-line arguments
|
|
(define (parse-arguments args)
|
|
"Parse command-line arguments."
|
|
(getopt-long args
|
|
'((target (value #t) (single-char #\t)) ;; Support both --target and -t
|
|
(source (value #t) (single-char #\s)) ;; Support both --source and -s
|
|
(help (value #f) (single-char #\h)) ;; Support -h for help
|
|
(version (value #f) (single-char #\v))))) ;; Support -v for version
|
|
|
|
;;; Main entry point
|
|
(define (main args)
|
|
"Main function to parse arguments and execute the program."
|
|
(setenv "GUILE_AUTO_COMPILE" "0") ;; Disable auto-compilation for performance
|
|
|
|
;; Check if --help or -h was passed
|
|
(if (or (member "--help" args) (member "-h" args))
|
|
(display-help))
|
|
|
|
;; Check if --version or -v was passed
|
|
(check-for-version args)
|
|
|
|
;;; Parse remaining arguments
|
|
(let* ((options (parse-arguments args)) ;; Parse the command-line options
|
|
(target-dir (assoc-ref options 'target)) ;; Extract --target or -t argument
|
|
(source-dir (assoc-ref options 'source))) ;; Extract --source or -s argument
|
|
(if (and target-dir source-dir) ;; Ensure both arguments are provided
|
|
(handle-source-and-target source-dir target-dir) ;; Proceed with moving the directory and creating the symlink
|
|
(begin
|
|
(display (color-message "Error: Missing required arguments.\n" red-text)) ;; Show error in red text
|
|
(display (color-message "Usage: stash.scm --target <target-dir> --source <source-dir>\n" yellow-text)) ;; Show updated usage message
|
|
(exit 1))))) ;; Exit with error status
|
|
|
|
;; Entry point for stash
|
|
(main (command-line))
|