From e69144a7d9922a545cd4f521f94d4591c770344d Mon Sep 17 00:00:00 2001 From: joachimschmidt557 Date: Sat, 7 Dec 2019 14:07:31 +0100 Subject: [PATCH] wip src --- src | 135 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 92 insertions(+), 43 deletions(-) diff --git a/src b/src index 54e72f3..0235dd6 100755 --- a/src +++ b/src @@ -3,6 +3,7 @@ srcfile=$(pwd)/.srcfile usage() { + ## Print usage information cat <> "$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" - ;; - *) - echo "Not a valid source type: $type" - continue - ;; - esac - done + 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 "Errors occurred during: $updated" + ;; + esac case "$updated" in "") @@ -90,23 +118,44 @@ parse() { echo "Updated: $updated" ;; esac + + cleanup } -for arg in "$@" ; do - case "$arg" in - --help) - usage - exit 1 - ;; - --version) - echo "src version 0.1.0" - exit 1 - ;; - *) - echo "Invalid command line args" - exit 1 - ;; - esac -done +list_all() { + ## Parse srcfile and update everything + preproc -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