Skip to content

Commit

Permalink
!feat: add outdir/outfile option & add test.sh for record test case…
Browse files Browse the repository at this point in the history
…s' (#4)
  • Loading branch information
yuler committed Sep 19, 2022
1 parent 518bc2a commit 5f7e5eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
34 changes: 24 additions & 10 deletions gh-download
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ Options:
-v, --version output version
--verbose enable debug
-b, --branch branch
-o, --output <path> output path, default is \`./\`
--outdir <path> outdir path. Default is download current location and reserved repo dir path
--outfile <path> only support download single file
Examples:
gh download yuler/gh-download README.md gh-download
gh download cli/cli .github
gh download yuler/actions ci/nodejs.yml --output ./.github/workflows
gh download yuler/actions ci/nodejs.yml --outdir .github/workflows
EOF
exit
}
Expand All @@ -36,17 +37,19 @@ version() {
exit
}

# TODO: output dir/file name
download_file() {
local file=$1
local dest
local dest=$2

echo "File \`$file\` downloading..."

if [[ -z $output ]]; then
if [[ -z $dest ]]; then
dest=$file
else
dest="$output/$file"
fi
if [[ -n $outdir ]]; then
echo "$outdir"
echo "$file"
dest="$outdir/$(basename "$file")"
fi
curl --location --create-dirs --oauth2-bearer "$token" "https://raw.githubusercontent.com/$repo/$branch/$1" -o "$dest"
}
Expand All @@ -59,7 +62,6 @@ download_folder() {
for file in $files; do
download_file "$file"
done

}

# Main
Expand All @@ -85,8 +87,13 @@ while [[ $# -gt 0 ]]; do
shift
shift
;;
-o | --output)
output="$2"
--outdir)
outdir="$2"
shift
shift
;;
--outfile)
outfile="$2"
shift
shift
;;
Expand Down Expand Up @@ -140,6 +147,13 @@ fi
echo "Repo: $repo"
echo "Branch: $branch"

# Download single file
if [[ $# == 1 && $outfile ]]; then
download_file "$1" "$outfile"
exit
fi

# Iterate arguments
while (("$#")); do
path=$1

Expand Down
7 changes: 7 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

# test `--outdir`
../gh-download yuler/actions nodejs --outdir .github/workflows

# test `--outfile`
../gh-download yuler/actions nodejs/ci.yml --outfile .github/workflows/nodejs.yml

0 comments on commit 5f7e5eb

Please sign in to comment.