Skip to content

Commit 2130030

Browse files
committed
refactor: Remove old parsing function and improve naming of current one
1 parent 8c30348 commit 2130030

File tree

6 files changed

+24
-53
lines changed

6 files changed

+24
-53
lines changed

pkg/lib/commands/do-add.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ do-add() {
2020
fi
2121

2222
for repoSpec in "${pkgs[@]}"; do
23-
util.construct_clone_url "$repoSpec" "$with_ssh"
23+
util.extract_data_from_input "$repoSpec" "$with_ssh"
2424
local uri="$REPLY1"
2525
local site="$REPLY2"
2626
local package="$REPLY3"

pkg/lib/commands/do-plumbing-remove-deps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ do-plumbing-remove-deps() {
2222

2323
log.info "Removing dependencies for '$package'"
2424
for dep in "${deps[@]}"; do
25-
util.construct_clone_url "$repoSpec" "$with_ssh"
25+
util.extract_data_from_input "$repoSpec" "$with_ssh"
2626
local site="$REPY2"
2727
local package="$REPLY3"
2828
local ref="$REPLY4"

pkg/lib/commands/do-remove.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ do-remove() {
2020
do_actual_removal "$site/$package"
2121
fi
2222
else
23-
util.construct_clone_url "$repoSpec"
23+
util.extract_data_from_input "$repoSpec"
2424
local site="$REPLY2"
2525
local package="$REPLY3"
2626
local ref="$REPLY4"

pkg/lib/commands/do-upgrade.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ do-upgrade() {
4646

4747
do_actual_upgrade "$site/$package"
4848
else
49-
util.construct_clone_url "$repoSpec"
49+
util.extract_data_from_input "$repoSpec"
5050
local site="$REPLY2"
5151
local package="$REPLY3"
5252
local ref="$REPLY4"

pkg/lib/util/util.sh

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,12 @@
33
# @file util.sh
44
# @brief Utility functions for all subcommands
55

6-
# @description Given input of a particular package on the internet
7-
# parse it into its components
8-
util.parse_package_full() {
9-
local repoSpec="$1"
10-
11-
if [ -z "$repoSpec" ]; then
12-
die "Must supply a repository"
13-
fi
14-
15-
# Remove any http(s) prefixes
16-
repoSpec="${repoSpec#http?(s)://}"
17-
18-
local site user repository
19-
if [[ "$repoSpec" == */*/* ]]; then
20-
IFS='/' read -r site user repository <<< "$repoSpec"
21-
elif [[ "$repoSpec" == */* ]]; then
22-
site="github.com"
23-
IFS='/' read -r user repository <<< "$repoSpec"
24-
fi
25-
26-
if [[ "$repository" == *@* ]]; then
27-
IFS='@' read -r repository ref <<< "$repository"
28-
else
29-
ref=""
30-
fi
31-
32-
ensure.non_zero 'site' "$site"
33-
ensure.non_zero 'user' "$user"
34-
ensure.non_zero 'repository' "$repository"
35-
36-
REPLY="$site:$user:$repository:$ref"
37-
}
38-
39-
# @description Generate the final URL to clone from
6+
# @description Given some user input, this extracts
7+
# data like the site it was cloned from, the owner of
8+
# the repository, and the name of the repository
409
# @arg $1 repoSpec
4110
# @arg $2 with_ssh Whether to clone with SSH (yes/no)
42-
util.construct_clone_url() {
11+
util.extract_data_from_input() {
4312
REPLY1=
4413
REPLY2=
4514
REPLY3=
@@ -111,6 +80,7 @@ util.extract_data_from_package_dir() {
11180
REPLY1=
11281
REPLY2=
11382
REPLY3=
83+
REPLY4=
11484

11585
local dir="$1"
11686
ensure.non_zero 'dir' "$dir"
@@ -122,6 +92,7 @@ util.extract_data_from_package_dir() {
12292
REPLY1="$site"
12393
REPLY2="$user"
12494
REPLY3="$repository"
95+
REPLY4=
12596
}
12697

12798
util.readlink() {

tests/util-construct-clone-url.bats renamed to tests/util-extract-data-from-input.bats

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ load 'util/init.sh'
44

55

66
@test "fails on no arguments" {
7-
run util.construct_clone_url
7+
run util.extract_data_from_input
88

99
assert_failure
1010
assert_line -p "Must supply a repository"
1111
}
1212

1313
@test "parses with full https url" {
14-
util.construct_clone_url 'https://gitlab.com/eankeen/proj'
14+
util.extract_data_from_input 'https://gitlab.com/eankeen/proj'
1515

1616
assert [ "$REPLY1" = 'https://gitlab.com/eankeen/proj.git' ]
1717
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -20,7 +20,7 @@ load 'util/init.sh'
2020
}
2121

2222
@test "parses with full http url" {
23-
util.construct_clone_url 'http://gitlab.com/eankeen/proj'
23+
util.extract_data_from_input 'http://gitlab.com/eankeen/proj'
2424

2525
assert [ "$REPLY1" = 'http://gitlab.com/eankeen/proj.git' ]
2626
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -29,7 +29,7 @@ load 'util/init.sh'
2929
}
3030

3131
@test "parses with full https url with .git ending" {
32-
util.construct_clone_url 'https://gitlab.com/eankeen/proj'
32+
util.extract_data_from_input 'https://gitlab.com/eankeen/proj'
3333

3434
assert [ "$REPLY1" = 'https://gitlab.com/eankeen/proj.git' ]
3535
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -38,7 +38,7 @@ load 'util/init.sh'
3838
}
3939

4040
@test "parses with full http url with .git ending" {
41-
util.construct_clone_url 'http://gitlab.com/eankeen/proj.git'
41+
util.extract_data_from_input 'http://gitlab.com/eankeen/proj.git'
4242

4343
assert [ "$REPLY1" = 'http://gitlab.com/eankeen/proj.git' ]
4444
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -47,7 +47,7 @@ load 'util/init.sh'
4747
}
4848

4949
@test "parses with full ssh url" {
50-
util.construct_clone_url 'git@gitlab.com:eankeen/proj.git'
50+
util.extract_data_from_input 'git@gitlab.com:eankeen/proj.git'
5151

5252
assert [ "$REPLY1" = 'git@gitlab.com:eankeen/proj' ]
5353
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -56,7 +56,7 @@ load 'util/init.sh'
5656
}
5757

5858
@test "parses with package and domain" {
59-
util.construct_clone_url 'gitlab.com/eankeen/proj'
59+
util.extract_data_from_input 'gitlab.com/eankeen/proj'
6060

6161
assert [ "$REPLY1" = 'https://gitlab.com/eankeen/proj.git' ]
6262
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -65,7 +65,7 @@ load 'util/init.sh'
6565
}
6666

6767
@test "parses with package and domain and ref" {
68-
util.construct_clone_url 'gitlab.com/eankeen/proj@v0.1.0'
68+
util.extract_data_from_input 'gitlab.com/eankeen/proj@v0.1.0'
6969

7070
assert [ "$REPLY1" = 'https://gitlab.com/eankeen/proj.git' ]
7171
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -74,7 +74,7 @@ load 'util/init.sh'
7474
}
7575

7676
@test "parses with package and domain with ssh" {
77-
util.construct_clone_url 'gitlab.com/eankeen/proj' 'yes'
77+
util.extract_data_from_input 'gitlab.com/eankeen/proj' 'yes'
7878

7979
assert [ "$REPLY1" = 'git@gitlab.com:eankeen/proj' ]
8080
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -83,7 +83,7 @@ load 'util/init.sh'
8383
}
8484

8585
@test "parses with package and domain and ref with ssh" {
86-
util.construct_clone_url 'gitlab.com/eankeen/proj@v0.1.0' 'yes'
86+
util.extract_data_from_input 'gitlab.com/eankeen/proj@v0.1.0' 'yes'
8787

8888
assert [ "$REPLY1" = 'git@gitlab.com:eankeen/proj' ]
8989
assert [ "$REPLY2" = 'gitlab.com' ]
@@ -92,7 +92,7 @@ load 'util/init.sh'
9292
}
9393

9494
@test "parses with package" {
95-
util.construct_clone_url 'eankeen/proj'
95+
util.extract_data_from_input 'eankeen/proj'
9696

9797
assert [ "$REPLY1" = 'https://github.com/eankeen/proj.git' ]
9898
assert [ "$REPLY2" = 'github.com' ]
@@ -101,7 +101,7 @@ load 'util/init.sh'
101101
}
102102

103103
@test "parses with package and ref" {
104-
util.construct_clone_url 'eankeen/proj@v0.2.0'
104+
util.extract_data_from_input 'eankeen/proj@v0.2.0'
105105

106106
assert [ "$REPLY1" = 'https://github.com/eankeen/proj.git' ]
107107
assert [ "$REPLY2" = 'github.com' ]
@@ -110,7 +110,7 @@ load 'util/init.sh'
110110
}
111111

112112
@test "parses with package with ssh" {
113-
util.construct_clone_url 'eankeen/proj' 'yes'
113+
util.extract_data_from_input 'eankeen/proj' 'yes'
114114

115115
assert [ "$REPLY1" = 'git@github.com:eankeen/proj' ]
116116
assert [ "$REPLY2" = 'github.com' ]
@@ -119,7 +119,7 @@ load 'util/init.sh'
119119
}
120120

121121
@test "parses with package with ssh and ref" {
122-
util.construct_clone_url 'eankeen/proj@v0.2.0' 'yes'
122+
util.extract_data_from_input 'eankeen/proj@v0.2.0' 'yes'
123123

124124
assert [ "$REPLY1" = 'git@github.com:eankeen/proj' ]
125125
assert [ "$REPLY2" = 'github.com' ]

0 commit comments

Comments
 (0)