From 1b5f7bf768ff90c633687478fa2126ef3c66613e Mon Sep 17 00:00:00 2001 From: 0xbbuddha Date: Tue, 14 Oct 2025 18:56:09 +0200 Subject: [PATCH] feat: add copy command to transfer files between local and environments --- arch-shell | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/arch-shell b/arch-shell index 3ef05f1..bc13b21 100755 --- a/arch-shell +++ b/arch-shell @@ -127,6 +127,7 @@ usage() { echo " clean Clean an environment" echo " snapshot Manage environment snapshots" echo " cache Interact with shared cache" + echo " copy Copy files between local and environment" exit 1 } @@ -203,6 +204,26 @@ usage_cache() { exit 1 } +usage_copy() { + echo "Usage: $0 copy " + echo "" + echo "Copy files between local system and environment." + echo "" + echo "Syntax:" + echo " : File or directory in environment" + echo " File or directory on local system" + echo "" + echo "Options:" + echo " -r, --recursive Copy directories recursively" + echo "" + echo "Examples:" + echo " $0 copy my-env:/etc/hosts ./hosts Copy from env to local" + echo " $0 copy ./script.sh my-env:/root/script.sh Copy from local to env" + echo " $0 copy -r ./mydir my-env:/root/mydir Copy directory to env" + echo " $0 copy -r my-env:/var/log ./logs Copy directory from env" + exit 1 +} + init_shared_cache() { debug_log "Initializing shared cache in $SHARED_CACHE" mkdir -p "$SHARED_CACHE/pkg" @@ -940,6 +961,149 @@ delete_snapshot() { fi } +copy_file() { + local recursive=false + local src="" + local dest="" + + while [[ $# -gt 0 ]]; do + case $1 in + -r|--recursive) + recursive=true + shift + ;; + *) + if [ -z "$src" ]; then + src="$1" + elif [ -z "$dest" ]; then + dest="$1" + else + echo -e "${RED}[!] Too many arguments${NC}" + usage_copy + fi + shift + ;; + esac + done + + if [ -z "$src" ] || [ -z "$dest" ]; then + echo -e "${RED}[!] Source and destination required${NC}" + usage_copy + fi + + local src_env="" + local src_path="" + local dest_env="" + local dest_path="" + + if [[ "$src" =~ ^([^:]+):(.+)$ ]]; then + src_env="${BASH_REMATCH[1]}" + src_path="${BASH_REMATCH[2]}" + else + src_path="$src" + fi + + if [[ "$dest" =~ ^([^:]+):(.+)$ ]]; then + dest_env="${BASH_REMATCH[1]}" + dest_path="${BASH_REMATCH[2]}" + else + dest_path="$dest" + fi + + if [ -n "$src_env" ] && [ -n "$dest_env" ]; then + echo -e "${RED}[!] Cannot copy directly between two environments${NC}" + echo -e "${YELLOW}[?] Copy from env to local first, then from local to env${NC}" + exit 1 + fi + + if [ -z "$src_env" ] && [ -z "$dest_env" ]; then + echo -e "${RED}[!] At least one path must reference an environment (env:path)${NC}" + usage_copy + fi + + local envname="" + local envdir="" + + if [ -n "$src_env" ]; then + envname="$src_env" + envdir="${ARCHSHELL_ENVS}/${envname}" + + if [ ! -d "$envdir" ]; then + echo -e "${RED}[!] Environment not found: ${envname}${NC}" + exit 3 + fi + + local full_src_path="${envdir}${src_path}" + + if [ ! -e "$full_src_path" ]; then + echo -e "${RED}[!] Source file not found in environment: ${src_path}${NC}" + exit 1 + fi + + if [ -d "$full_src_path" ] && [ "$recursive" = false ]; then + echo -e "${RED}[!] Source is a directory. Use -r or --recursive${NC}" + exit 1 + fi + + echo -e "${BLUE}[*] Copying from environment ${envname}:${src_path} to ${dest_path}...${NC}" + + if [ "$recursive" = true ]; then + debug_run "sudo cp -r '$full_src_path' '$dest_path'" + else + debug_run "sudo cp '$full_src_path' '$dest_path'" + fi + + if [ $? -eq 0 ]; then + debug_run "sudo chown -R $USER: '$dest_path'" + echo -e "${GREEN}[*] Copy completed successfully${NC}" + update_last_used "$envname" + else + echo -e "${RED}[!] Copy failed${NC}" + exit 1 + fi + + elif [ -n "$dest_env" ]; then + envname="$dest_env" + envdir="${ARCHSHELL_ENVS}/${envname}" + + if [ ! -d "$envdir" ]; then + echo -e "${RED}[!] Environment not found: ${envname}${NC}" + exit 3 + fi + + if [ ! -e "$src_path" ]; then + echo -e "${RED}[!] Source file not found: ${src_path}${NC}" + exit 1 + fi + + if [ -d "$src_path" ] && [ "$recursive" = false ]; then + echo -e "${RED}[!] Source is a directory. Use -r or --recursive${NC}" + exit 1 + fi + + local full_dest_path="${envdir}${dest_path}" + local dest_parent_dir=$(dirname "$full_dest_path") + + echo -e "${BLUE}[*] Copying from ${src_path} to environment ${envname}:${dest_path}...${NC}" + + debug_run "sudo mkdir -p '$dest_parent_dir'" + + if [ "$recursive" = true ]; then + debug_run "sudo cp -r '$src_path' '$full_dest_path'" + else + debug_run "sudo cp '$src_path' '$full_dest_path'" + fi + + if [ $? -eq 0 ]; then + echo -e "${GREEN}[*] Copy completed successfully${NC}" + update_last_used "$envname" + else + echo -e "${RED}[!] Copy failed${NC}" + exit 1 + fi + fi +} + while [ $# -gt 0 ]; do case "$1" in --debug) @@ -1307,6 +1471,13 @@ case "$1" in ;; esac ;; + copy) + if [ $# -lt 3 ]; then + usage_copy + fi + shift 1 + copy_file "$@" + ;; *) usage ;;