Skip to content

Commit c22c8a8

Browse files
committed
- Add copy command for copying packages
1 parent 071d76f commit c22c8a8

18 files changed

+467
-128
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Package Commands:
127127
get Install a package (default)
128128
undo Uninstall a package
129129
snatch Install a package from a remote repo
130+
copy Copy a package between local repositories
130131
info Show information about a package
131132
list Show packages in one or all repositories
132133
search Search in package names and info files
@@ -142,7 +143,7 @@ Options:
142143
143144
Environment Variables:
144145
RUSH_CONFIG
145-
Location of the rush config file
146+
Location of the rush config file.
146147
Default: ~/rush.ini
147148
148149
```

rush

Lines changed: 186 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ rush_usage() {
3737
echo " get Install a package (default)"
3838
echo " undo Uninstall a package"
3939
echo " snatch Install a package from a remote repo"
40+
echo " copy Copy a package between local repositories"
4041
echo " info Show information about a package"
4142
echo " list Show packages in one or all repositories"
4243
echo " search Search in package names and info files"
@@ -530,6 +531,56 @@ rush_snatch_usage() {
530531
fi
531532
}
532533

534+
# :command.usage
535+
rush_copy_usage() {
536+
if [[ -n $long_usage ]]; then
537+
printf "rush copy - Copy a package between local repositories\n"
538+
echo
539+
else
540+
printf "rush copy - Copy a package between local repositories\n"
541+
echo
542+
fi
543+
544+
printf "Usage:\n"
545+
printf " rush copy SOURCE_PACKAGE [TARGET_PACKAGE] [options]\n"
546+
printf " rush copy --help | -h\n"
547+
echo
548+
549+
if [[ -n $long_usage ]]; then
550+
printf "Options:\n"
551+
# :command.usage_fixed_flags
552+
echo " --help, -h"
553+
printf " Show this help\n"
554+
echo
555+
# :command.usage_flags
556+
# :flag.usage
557+
echo " --force, -f"
558+
printf " Copy the package even if it already exists in the target repository.\n"
559+
echo
560+
# :command.usage_args
561+
printf "Arguments:\n"
562+
563+
# :argument.usage
564+
echo " SOURCE_PACKAGE"
565+
printf " Source package name.\n This can either be the package name without the repository name (in\n this case, the default repository will be used) or in the form of\n 'repo:package'.\n"
566+
echo
567+
568+
# :argument.usage
569+
echo " TARGET_PACKAGE"
570+
printf " Target package name.\n This can either be the package name without the repository name (in\n this case, the default repository will be used) or in the form of\n 'repo:package'.\n If left empty, the package will be copied with the same name to the\n default repository.\n"
571+
echo
572+
573+
# :command.usage_examples
574+
printf "Examples:\n"
575+
576+
printf " rush copy james:python\n"
577+
printf " rush copy james:python bobby\n"
578+
printf " rush copy james:python bobby:python3 --force\n"
579+
echo
580+
581+
fi
582+
}
583+
533584
# :command.usage
534585
rush_info_usage() {
535586
if [[ -n $long_usage ]]; then
@@ -1244,6 +1295,59 @@ rush_snatch_command() {
12441295
rush get "snatched:$package"
12451296
}
12461297

1298+
# :command.function
1299+
rush_copy_command() {
1300+
# :src/copy_command.sh
1301+
source_package=${args[source_package]}
1302+
target_package=${args[target_package]}
1303+
force=${args[--force]}
1304+
source_repo=default
1305+
target_repo=default
1306+
1307+
if [[ $source_package =~ (.*):(.*) ]]; then
1308+
source_repo=${BASH_REMATCH[1]}
1309+
source_package=${BASH_REMATCH[2]}
1310+
fi
1311+
1312+
if [[ -z "$target_package" ]]; then
1313+
target_package="$source_package"
1314+
elif [[ $target_package =~ (.*):(.*) ]]; then
1315+
target_repo=${BASH_REMATCH[1]}
1316+
target_package=${BASH_REMATCH[2]}
1317+
fi
1318+
1319+
source_repo_path=$(config_get "$source_repo")
1320+
target_repo_path=$(config_get "$target_repo")
1321+
source_package_path="$source_repo_path/$source_package"
1322+
target_package_path="$target_repo_path/$target_package"
1323+
1324+
if [[ $source_repo == "default" ]]; then
1325+
source_display_name="$source_package"
1326+
else
1327+
source_display_name="$source_repo:$source_package"
1328+
fi
1329+
1330+
if [[ $target_repo == "default" ]]; then
1331+
target_display_name="$target_package"
1332+
else
1333+
target_display_name="$target_repo:$target_package"
1334+
fi
1335+
1336+
[[ $source_repo_path ]] || abort "source repo not found: $source_repo"
1337+
[[ $target_repo_path ]] || abort "target repo not found: $target_repo"
1338+
[[ -d $source_package_path ]] || abort "source package not found: $source_repo:$source_package"
1339+
if [[ -d $target_package_path ]]; then
1340+
if [[ $force ]]; then
1341+
rm -rf "$target_package_path"
1342+
else
1343+
abort "target package already exists: $target_repo:$target_package\nrun again with --force to copy anyway"
1344+
fi
1345+
fi
1346+
1347+
say "copy" "$source_display_name to $target_display_name"
1348+
cp -R "$source_package_path" "$target_package_path"
1349+
}
1350+
12471351
# :command.function
12481352
rush_info_command() {
12491353
# :src/info_command.sh
@@ -1262,7 +1366,7 @@ rush_info_command() {
12621366

12631367
# Verify we have everything we need
12641368
[[ $repo_path ]] || abort "repo not found: $repo"
1265-
[[ -d $package_path ]] || abort "package not found: $repo/$package"
1369+
[[ -d $package_path ]] || abort "package not found: $repo:$package"
12661370
[[ -f $infofile ]] || abort "infofile not found: $infofile"
12671371

12681372
cat "$infofile"
@@ -1435,7 +1539,7 @@ rush_show_command() {
14351539

14361540
# Verify we have everything we need
14371541
[[ $repo_path ]] || abort "repo not found: $repo"
1438-
[[ -d $package_path ]] || abort "package not found: $repo/$package"
1542+
[[ -d $package_path ]] || abort "package not found: $repo:$package"
14391543

14401544
# Show the package data
14411545
if [[ $file ]]; then
@@ -1548,6 +1652,13 @@ parse_requirements() {
15481652
shift $#
15491653
;;
15501654

1655+
copy )
1656+
action="copy"
1657+
shift
1658+
rush_copy_parse_requirements "$@"
1659+
shift $#
1660+
;;
1661+
15511662
info | i )
15521663
action="info"
15531664
shift
@@ -2250,6 +2361,71 @@ rush_snatch_parse_requirements() {
22502361
# :command.whitelist_filter
22512362
}
22522363

2364+
# :command.parse_requirements
2365+
rush_copy_parse_requirements() {
2366+
# :command.fixed_flag_filter
2367+
case "$1" in
2368+
--version | -v )
2369+
version_command
2370+
exit
2371+
;;
2372+
2373+
--help | -h )
2374+
long_usage=yes
2375+
rush_copy_usage
2376+
exit 1
2377+
;;
2378+
2379+
esac
2380+
# :command.environment_variables_filter
2381+
# :command.dependencies_filter
2382+
# :command.command_filter
2383+
action="copy"
2384+
# :command.required_args_filter
2385+
if [[ $1 && $1 != -* ]]; then
2386+
args[source_package]=$1
2387+
shift
2388+
else
2389+
printf "missing required argument: SOURCE_PACKAGE\nusage: rush copy SOURCE_PACKAGE [TARGET_PACKAGE] [options]\n"
2390+
exit 1
2391+
fi
2392+
# :command.required_flags_filter
2393+
# :command.parse_requirements_while
2394+
while [[ $# -gt 0 ]]; do
2395+
key="$1"
2396+
case "$key" in
2397+
# :flag.case
2398+
--force | -f )
2399+
args[--force]=1
2400+
shift
2401+
;;
2402+
2403+
2404+
-* )
2405+
printf "invalid option: %s\n" "$key"
2406+
exit 1
2407+
;;
2408+
2409+
* )
2410+
# :command.parse_requirements_case
2411+
if [[ ! ${args[source_package]} ]]; then
2412+
args[source_package]=$1
2413+
shift
2414+
elif [[ ! ${args[target_package]} ]]; then
2415+
args[target_package]=$1
2416+
shift
2417+
else
2418+
printf "invalid argument: %s\n" "$key"
2419+
exit 1
2420+
fi
2421+
;;
2422+
2423+
esac
2424+
done
2425+
# :command.default_assignments
2426+
# :command.whitelist_filter
2427+
}
2428+
22532429
# :command.parse_requirements
22542430
rush_info_parse_requirements() {
22552431
# :command.fixed_flag_filter
@@ -2630,6 +2806,14 @@ run() {
26302806
rush_snatch_command
26312807
fi
26322808

2809+
elif [[ $action == "copy" ]]; then
2810+
if [[ ${args[--help]} ]]; then
2811+
long_usage=yes
2812+
rush_copy_usage
2813+
else
2814+
rush_copy_command
2815+
fi
2816+
26332817
elif [[ $action == "info" ]]; then
26342818
if [[ ${args[--help]} ]]; then
26352819
long_usage=yes

src/bashly.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,37 @@ commands:
220220
- rush snatch james python
221221
- rush snatch james/other-rush-repo python
222222

223+
- name: copy
224+
help: Copy a package between local repositories
225+
226+
args:
227+
- name: source_package
228+
required: true
229+
help: |-
230+
Source package name.
231+
This can either be the package name without the repository name (in
232+
this case, the default repository will be used) or in the form of
233+
'repo:package'.
234+
235+
- name: target_package
236+
help: |-
237+
Target package name.
238+
This can either be the package name without the repository name (in
239+
this case, the default repository will be used) or in the form of
240+
'repo:package'.
241+
If left empty, the package will be copied with the same name to the
242+
default repository.
243+
244+
flags:
245+
- long: --force
246+
short: -f
247+
help: Copy the package even if it already exists in the target repository.
248+
249+
examples:
250+
- rush copy james:python
251+
- rush copy james:python bobby
252+
- rush copy james:python bobby:python3 --force
253+
223254
- name: info
224255
short: i
225256
help: |-

src/copy_command.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
source_package=${args[source_package]}
2+
target_package=${args[target_package]}
3+
force=${args[--force]}
4+
source_repo=default
5+
target_repo=default
6+
7+
if [[ $source_package =~ (.*):(.*) ]]; then
8+
source_repo=${BASH_REMATCH[1]}
9+
source_package=${BASH_REMATCH[2]}
10+
fi
11+
12+
if [[ -z "$target_package" ]]; then
13+
target_package="$source_package"
14+
elif [[ $target_package =~ (.*):(.*) ]]; then
15+
target_repo=${BASH_REMATCH[1]}
16+
target_package=${BASH_REMATCH[2]}
17+
fi
18+
19+
source_repo_path=$(config_get "$source_repo")
20+
target_repo_path=$(config_get "$target_repo")
21+
source_package_path="$source_repo_path/$source_package"
22+
target_package_path="$target_repo_path/$target_package"
23+
24+
if [[ $source_repo == "default" ]]; then
25+
source_display_name="$source_package"
26+
else
27+
source_display_name="$source_repo:$source_package"
28+
fi
29+
30+
if [[ $target_repo == "default" ]]; then
31+
target_display_name="$target_package"
32+
else
33+
target_display_name="$target_repo:$target_package"
34+
fi
35+
36+
[[ $source_repo_path ]] || abort "source repo not found: $source_repo"
37+
[[ $target_repo_path ]] || abort "target repo not found: $target_repo"
38+
[[ -d $source_package_path ]] || abort "source package not found: $source_repo:$source_package"
39+
if [[ -d $target_package_path ]]; then
40+
if [[ $force ]]; then
41+
rm -rf "$target_package_path"
42+
else
43+
abort "target package already exists: $target_repo:$target_package\nrun again with --force to copy anyway"
44+
fi
45+
fi
46+
47+
say "copy" "$source_display_name to $target_display_name"
48+
cp -R "$source_package_path" "$target_package_path"

src/info_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ infofile="$package_path/info"
1313

1414
# Verify we have everything we need
1515
[[ $repo_path ]] || abort "repo not found: $repo"
16-
[[ -d $package_path ]] || abort "package not found: $repo/$package"
16+
[[ -d $package_path ]] || abort "package not found: $repo:$package"
1717
[[ -f $infofile ]] || abort "infofile not found: $infofile"
1818

1919
cat "$infofile"

src/show_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ file_path="$package_path/$file"
1414

1515
# Verify we have everything we need
1616
[[ $repo_path ]] || abort "repo not found: $repo"
17-
[[ -d $package_path ]] || abort "package not found: $repo/$package"
17+
[[ -d $package_path ]] || abort "package not found: $repo:$package"
1818

1919
# Show the package data
2020
if [[ $file ]]; then

test/approvals/rush

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Package Commands:
2222
get Install a package (default)
2323
undo Uninstall a package
2424
snatch Install a package from a remote repo
25+
copy Copy a package between local repositories
2526
info Show information about a package
2627
list Show packages in one or all repositories
2728
search Search in package names and info files

test/approvals/rush_copy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
missing required argument: SOURCE_PACKAGE
2+
usage: rush copy SOURCE_PACKAGE [TARGET_PACKAGE] [options]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
info
2+
main
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/root/rush-repos/target:
2+
download1
3+
download2
4+
hello
5+
6+
/root/rush-repos/target/download1:
7+
info
8+
main
9+
undo
10+
11+
/root/rush-repos/target/download2:
12+
info
13+
main
14+
undo
15+
16+
/root/rush-repos/target/hello:
17+
info
18+
main

0 commit comments

Comments
 (0)