This commit is contained in:
joachimschmidt557 2019-12-07 14:07:31 +01:00
parent e162e41ee2
commit e69144a7d9

109
src
View file

@ -3,6 +3,7 @@
srcfile=$(pwd)/.srcfile
usage() {
## Print usage information
cat <<EOF
src
manages source directories
@ -10,7 +11,7 @@ manages source directories
Usage: src [COMMAND]
Commands:
update Update source directory
up Update source directory
ls List managed directories
add Add a source
rm Remove a source directory
@ -21,16 +22,16 @@ Flags:
EOF
}
add_gitrepo() {
## Add this git repo to the srcfile
echo "git" "$1" "$2" >> "$srcfile"
add_item() {
## Add this item repo to the srcfile
echo "$1" "$2" "$3" >> "$srcfile"
}
rm_entry() {
rm_item() {
## Remove this entry from the srcfile
tmp="$srcfile.tmp"
awk "\$3 == $1 { print }" "$srcfile" > "$tmp"
awk "$$3 != $1 { print }" "$srcfile" > "$tmp"
mv "$tmp" "$srcfile"
}
@ -40,7 +41,10 @@ update_gitrepo() {
git clone "$1" "$2"
updated="$updated $2"
else
git -C "$2" pull
count=$(git -C "$2" pull | wc -l)
if [ "$count" -gt 1 ] ; then
updated="$updated $2"
fi
fi
}
@ -56,31 +60,55 @@ update_tarfile() {
fi
}
parse() {
## Parse srcfile and update everything
srcfile=$(pwd)/.srcfile
update_item() {
## Updates an individual item
case "$1" in
"git")
echo "Updating: $3"
update_gitrepo "$2" "$3"
;;
"tar")
echo "Updating: $3"
update_tarfile "$2" "$3"
;;
*)
echo "Not a valid source type: $1"
errors="$errors $3"
;;
esac
}
preproc() {
## Preprocess the srcfile (remove comments etc.)
if ! [ -f "$srcfile" ] ; then
echo ".srcfile not found!"
exit 1
fi
awk '!($$0 ~ /^#/) { print }' "$srcfile" | while read -r type url path; do
case "$type" in
"git")
echo "Updating: $path"
update_gitrepo "$url" "$path"
;;
"tar")
echo "Updating: $path"
#update_tarfile "$url" "$path"
tmp="$srcfile.tmp"
awk '!($$0 ~ /^#/) { print }' "$srcfile" > "$tmp"
}
cleanup() {
## Cleanup everything (temp files)
rm "$tmp"
}
update_all() {
## Parse srcfile and update everything
preproc
while read -r type url path; do
update_item "$type" "$url" "$path"
done < "$tmp"
case "$errors" in
"")
;;
*)
echo "Not a valid source type: $type"
continue
echo "Errors occurred during: $updated"
;;
esac
done
case "$updated" in
"")
@ -90,23 +118,44 @@ parse() {
echo "Updated: $updated"
;;
esac
cleanup
}
for arg in "$@" ; do
case "$arg" in
--help)
list_all() {
## Parse srcfile and update everything
preproc
while read -r type url path; do
echo "$type" "$url" "$path"
done < "$tmp"
cleanup
}
case "$1" in
"up")
update_all
;;
"add")
add_item "$2" "$3" "$4"
;;
"rm")
rm_item "$2" "$3" "$4"
;;
"ls")
list_all
;;
"--help"|"-h")
usage
exit 1
;;
--version)
"--version"|"-v")
echo "src version 0.1.0"
exit 1
;;
*)
echo "Invalid command line args"
echo "Invalid command or flag"
exit 1
;;
esac
done
parse