Skip to content

Commit

Permalink
support custom config when upload/download
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyiyi0226 committed Jun 14, 2021
1 parent 45ff586 commit 8097427
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SPC lets you transfer files and folders to multiple computers easily.
2. Set/Modify the configurations if needed ( [the supported configurations](#configurations) )
```
spc update mypc -P=8787 uploaddir=/usr/me/Desktop
spc update mypc -P=8787 uploaddir=/home/me/Desktop
```
3. Set the choosen machine as default
Expand Down Expand Up @@ -90,12 +90,12 @@ spc support these commands:
#### `spc upload`
Upload file/folders to remote machine
Usage: spc [upload] [-r <remote-name>] <file> <file2> <...>
Usage: spc [upload] [-r <remote-name>] [-d <upload-directory>] [-c <config-string>] <file> <file2> <...>
#### `spc download`
Download file/folders from remote machine
Usage: spc download [-r <remote-name>] <file1> <file2> <...>
Usage: spc download [-r <remote-name>] [-d <download-directory>] [-c <config-string>] <file1> <file2> <...>
### Remote setting commands
Expand Down
64 changes: 63 additions & 1 deletion libexec/spc-download
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
# Summary: Download file/folders from remote machine
# Usage: spc download [-r <remote-name>] <file1> <file2> <...>
# Usage: spc download [-r <remote-name>] [-d <download-directory>] [-c <config-string>] <file1> <file2> <...>
# Example: spc download -r remote1 -d /home -c "-P=8787 -p" file1
#

set -e
Expand All @@ -17,6 +18,37 @@ get_config() {
awk -F "=" -v config="${config}" '$0~config {print $2}' "${file}"
}

apply_config() {
## create or replace config

local option="$1"
local value="$2"

if [[ ! "${SCP_OPS[*]}" =~ ${option} ]]; then
echo "Invalid option: ${option}"
exit
fi

# replace config if exists
for i in "${!configs[@]}"; do
if [[ "${configs[i]%% *}" == "${option}" ]]; then
if [[ -n "${value}" ]]; then
configs[i]="${option} ${value}"
else
configs[i]="${option}"
fi
return 0
fi
done

# append config
if [[ -n "${value}" ]]; then
configs+=("${option} ${value}")
else
configs+=("${option}")
fi
}

parse_config() {
## parse config and store in three variables: address, downloaddir, configs

Expand Down Expand Up @@ -75,6 +107,36 @@ fi

parse_config "${remote}"

if [[ "$1" =~ ^(-d|--directory)$ ]]; then
if [ -z "$2" ]; then
echo "Missing <upload-directory>"
exit
fi

# overwrite the download directory
downloaddir="$2"
shift 2
fi

if [[ "$1" =~ ^(-c|--config)$ ]]; then
if [ -z "$2" ]; then
echo "Missing <config-string>"
exit
fi

read -r -a custom_configs <<< "$2"

for config in "${custom_configs[@]}"; do
if [[ "${config}" == *=* ]]; then
apply_config "${config%%=*}" "${config##*=}"
else
apply_config "${config}"
fi
done
shift 2
fi


if [ $# -gt 1 ]; then
IFS=","
scp -r "${configs[@]}" "${address}:{$*}" "${downloaddir}"
Expand Down
63 changes: 62 additions & 1 deletion libexec/spc-upload
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
# Summary: Upload file/folders to remote machine
# Usage: spc [upload] [-r <remote-name>] <file> <file2> <...>
# Usage: spc [upload] [-r <remote-name>] [-d <upload-directory>] [-c <config-string>] <file> <file2> <...>
# Example: spc upload -r remote1 -d /home -c "-P=8787 -p" file1
#

set -e
Expand All @@ -17,6 +18,37 @@ get_config() {
awk -F "=" -v config="${config}" '$0~config {print $2}' "${file}"
}

apply_config() {
## create or replace config

local option="$1"
local value="$2"

if [[ ! "${SCP_OPS[*]}" =~ ${option} ]]; then
echo "Invalid option: ${option}"
exit
fi

# replace config if exists
for i in "${!configs[@]}"; do
if [[ "${configs[i]%% *}" == "${option}" ]]; then
if [[ -n "${value}" ]]; then
configs[i]="${option} ${value}"
else
configs[i]="${option}"
fi
return 0
fi
done

# append config
if [[ -n "${value}" ]]; then
configs+=("${option} ${value}")
else
configs+=("${option}")
fi
}

parse_config() {
## parse config and store in three variables: address, uploaddir, configs

Expand Down Expand Up @@ -75,4 +107,33 @@ fi

parse_config "${remote}"

if [[ "$1" =~ ^(-d|--directory)$ ]]; then
if [ -z "$2" ]; then
echo "Missing <upload-directory>"
exit
fi

# overwrite the upload directory
uploaddir="$2"
shift 2
fi

if [[ "$1" =~ ^(-c|--config)$ ]]; then
if [ -z "$2" ]; then
echo "Missing <config-string>"
exit
fi

read -r -a custom_configs <<< "$2"

for config in "${custom_configs[@]}"; do
if [[ "${config}" == *=* ]]; then
apply_config "${config%%=*}" "${config##*=}"
else
apply_config "${config}"
fi
done
shift 2
fi

scp -r "${configs[@]}" "$@" "${address}:${uploaddir}"

0 comments on commit 8097427

Please sign in to comment.