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

135
src
View file

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