From 7c5a94414872e92fd7315c2163b0157dbbfdb252 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sat, 23 Nov 2019 23:59:35 +0100 Subject: [PATCH 1/4] Implement update for git repo --- src/dotinstall.sh | 62 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/dotinstall.sh b/src/dotinstall.sh index 5126c4e..8488d53 100755 --- a/src/dotinstall.sh +++ b/src/dotinstall.sh @@ -14,6 +14,7 @@ OVERWRITE_FILE=0 repository="" install=1 +update=0 usage () { @@ -310,25 +311,64 @@ define_env () ## create bin directory [ ! -d $BIN_DIRECTORY ] && mkdir -p $BIN_DIRECTORY || printf "bin exist\n" -# Test parameters : we need to know if it's a git repo or a file -[[ $1 == "uninstall" || $1 == "-u" ]] && { install=0; printf "Activate uninstall mode\n"; shift; } +# define mode : install, uninstall or update +case $1 in + "uninstall") + install=0 + printf "Activate uninstall mode\n" + shift + ;; + + "update") + update=1; + printf "Activate update mode\n" + shift + ;; +esac + [ "$*" = "" ] && die "You must specify a bootstrap file" 10 1 if [[ $* =~ ^https://.*\.git$ || $* =~ ^ssh://.*\.git$ ]] then - bin_check "git" + check_bin "git" # Check - localdir="${DOTREPO}/$(basename $* .git)" - [ -d "$localdir" ] && die "destination folder for git repository already exist" 21 0 - - private:clone_repository "$*" "$localdir" - if [ -f "${localdir}/bootstrap" ] + localrepo="${DOTREPO}/$(basename $* .git)" + if [ $update -eq 1 ] then - $0 "${localdir}/bootstrap" - exit $? + [ ! -d $localrepo ] && die "The local repository does not exist" 22 0 + + # For update we need to uninstall repo, then git pull and install + current_dir=$(pwd) + $0 uninstall ${locarepo}/bootstrap + cd $localrepo + pwd + git pull + [ $? -ne 0 ] && die "$localrepo does not seems to be a git repo" 23 0 + $0 bootstrap + cd $current_dir else - die "Can't find a \`boostrap\` file in ${repo}" 22 0 + if [ $install -eq 0 ] + then + if [ -f "$localrepo" ] + then + $0 uninstall ${localrepo}/bootstrap + printf "\nRemove $localrepo folder : " + ret=$(rm -rf $localrepo) + [ $? -eq 0 ] && printf " \e[32mdone\e[0m\n" || error "$ret" + else + die "destination folder for git repository not exist in $DOTREPO" 21 0 + fi + else + private:clone_repository "$*" "$localrepo" + if [ -f "${localrepo}/bootstrap" ] + then + $0 "${localrepo}/bootstrap" + exit $? + else + die "Can't find a \`boostrap\` file in ${repo}" 22 0 + fi + fi fi else [ ! -f "$*" ] && die "$* does not exist" 10 1 From 5cce7f0d3bc0617cbee084c1bf85e2826a2cf51f Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 24 Nov 2019 00:16:56 +0100 Subject: [PATCH 2/4] Remove trailing slashed from path variables --- src/dotinstall.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dotinstall.sh b/src/dotinstall.sh index 8488d53..c7a3b1a 100755 --- a/src/dotinstall.sh +++ b/src/dotinstall.sh @@ -2,10 +2,10 @@ # Personnal Dotfiles install script -BIN_DIRECTORY="${HOME}/.local/bin/" -LIB_DIRECTORY="${HOME}/.local/lib/" +BIN_DIRECTORY="${HOME}/.local/bin" +LIB_DIRECTORY="${HOME}/.local/lib" SYD_DIRECTORY="${HOME}/.config/systemd/user" -DOTREPO="${HOME}/.config/dotrepo/" +DOTREPO="${HOME}/.config/dotrepo" ENV_FILE="${HOME}/.config/environment" # DEFAULT VALUES @@ -326,7 +326,7 @@ case $1 in ;; esac -[ "$*" = "" ] && die "You must specify a bootstrap file" 10 1 +[ "$*" = "" ] && die "You must specify a bootstrap file or repo" 10 1 if [[ $* =~ ^https://.*\.git$ || $* =~ ^ssh://.*\.git$ ]] then From dc95d17211c116d61416a0a72aae54703f55558c Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 24 Nov 2019 02:34:09 +0100 Subject: [PATCH 3/4] Rewrite main code --- src/dotinstall.sh | 101 +++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/src/dotinstall.sh b/src/dotinstall.sh index c7a3b1a..84cbde0 100755 --- a/src/dotinstall.sh +++ b/src/dotinstall.sh @@ -5,6 +5,8 @@ BIN_DIRECTORY="${HOME}/.local/bin" LIB_DIRECTORY="${HOME}/.local/lib" SYD_DIRECTORY="${HOME}/.config/systemd/user" +CACHE_DIR="${HOME}/.cache/dotinstall" + DOTREPO="${HOME}/.config/dotrepo" ENV_FILE="${HOME}/.config/environment" @@ -148,7 +150,8 @@ private:create_symblink () private:clone_repository () { # Clone a git repository and test if it has a bootstrap file - # $* url + # $1 url + # $2 local repository # return : local directory local ret @@ -156,6 +159,19 @@ private:clone_repository () [[ $? -ne 0 ]] && die "Can't clone_repository : $ret" 20 0 } +private:update_repository () +{ + # Update a git repository + # $1 : local repository + + [ ! -d "$1" ] && die "Git update : directory $1 does not exist" 25 0 + local current_dir=$(pwd) + local ret + cd "$1" + ret=$(git pull 2>&1) + [[ $? -ne 0 ]] && die "Can't update repository : $ret" 24 0 +} + process_dirs () { # Process a directory than contains subdir, create symblink from each subdir @@ -167,7 +183,7 @@ process_dirs () { local dest="${repository}/$1" printf "\nProcess directory %s\n" "$1" - [ ! -d "$dest" ] && { error " -> source is not a directory"; return; } + [ ! -d "$dest" ] && { error " -> source is not a directory $dest"; return; } [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } while read d @@ -310,7 +326,7 @@ define_env () ## create bin directory [ ! -d $BIN_DIRECTORY ] && mkdir -p $BIN_DIRECTORY || printf "bin exist\n" - +[ ! -d $CACHE_DIR ] && mkdir -p $CACHE_DIR || printf "cache exist\n" # define mode : install, uninstall or update case $1 in "uninstall") @@ -336,45 +352,56 @@ then localrepo="${DOTREPO}/$(basename $* .git)" if [ $update -eq 1 ] then - [ ! -d $localrepo ] && die "The local repository does not exist" 22 0 - - # For update we need to uninstall repo, then git pull and install - current_dir=$(pwd) - $0 uninstall ${locarepo}/bootstrap - cd $localrepo - pwd - git pull - [ $? -ne 0 ] && die "$localrepo does not seems to be a git repo" 23 0 - $0 bootstrap - cd $current_dir + private:update_repository "$localrepo" else - if [ $install -eq 0 ] + if [ $install -eq 1 ] then - if [ -f "$localrepo" ] - then - $0 uninstall ${localrepo}/bootstrap - printf "\nRemove $localrepo folder : " - ret=$(rm -rf $localrepo) - [ $? -eq 0 ] && printf " \e[32mdone\e[0m\n" || error "$ret" - else - die "destination folder for git repository not exist in $DOTREPO" 21 0 - fi - else private:clone_repository "$*" "$localrepo" - if [ -f "${localrepo}/bootstrap" ] - then - $0 "${localrepo}/bootstrap" - exit $? - else - die "Can't find a \`boostrap\` file in ${repo}" 22 0 - fi fi fi + bootstrap_file="${localrepo}/bootstrap" else - [ ! -f "$*" ] && die "$* does not exist" 10 1 - private:get_bootstrap_path "$*" - - # Include bootstrap - source $* + bootstrap_file="$*" fi + +[ ! -f "$bootstrap_file" ] && die "$bootstrap_file does not exist" 10 1 + +private:get_bootstrap_path "$bootstrap_file" + +cache_file="${CACHE_DIR}/$(echo $repository | tr '/' '_')" + +if [ $update -eq 1 ] +then + # Update mode + if [ -f "$cache_file" ] + then + if [ $(diff "$bootstrap_file" "$cache_file" > /dev/null; echo $?;) -eq 0 ] + then + printf "There is no difference between cached copy and the bootstrap file\n"; + exit 0 + else + # Uninstall old files + install=0 + source "$cache_file" + rm "$cache_file" + install=1 + fi + else + error "Can't found cached copy of bootstrap file, just install" + fi +fi + +cp "$bootstrap_file" "$cache_file" + +# Include bootstrap +source "$bootstrap_file" + +# remove cached file and repo if uninstall +if [ $install -eq 0 ] +then + printf "\nRemove files for complete uninstall\n" + rm $cache_file + rm -rf $repository +fi + exit 0 From d5ed3d209d110fa9e1f703370b370a1dcb65adff Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 24 Nov 2019 03:01:12 +0100 Subject: [PATCH 4/4] Rework exit codes --- README.md | 23 +++++++++++++++++++++++ src/dotinstall.sh | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e83934..1412d96 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,12 @@ bootstrap file : dotinstall uninstall bootstrap ``` +Or update with `update` even in git mode + +``` +dotinstall update bootstrap +``` + ## Boostrap file syntax `bootstrap` is a simple bash script sourced by `dotinstall`. The `bootstrap` @@ -128,6 +134,23 @@ define_env "NOTMUCH_CONFIG" "~/.config/notmuch" displayed. - `OVERWRITE_FILE` : do the same with file +## Return code + +This script return 0 if there is not error. + + - **1** : no bootstrap specified. + - **2** : boostrap file not found + +### Git relative error codes + + - **20** : can't clone dotfile git repository. + - **24** : can't update dotfile git repository. + - **25** : local git repository does not exist. + +### Script relative error code + + - **128** : a binary checked by `chek_bin()` is not present + ## Licence This software is licenced under the GNU-GPL 3 licence, you can found a copy of diff --git a/src/dotinstall.sh b/src/dotinstall.sh index 84cbde0..9469260 100755 --- a/src/dotinstall.sh +++ b/src/dotinstall.sh @@ -342,7 +342,7 @@ case $1 in ;; esac -[ "$*" = "" ] && die "You must specify a bootstrap file or repo" 10 1 +[ "$*" = "" ] && die "You must specify a bootstrap file or repo" 1 1 if [[ $* =~ ^https://.*\.git$ || $* =~ ^ssh://.*\.git$ ]] then @@ -364,7 +364,7 @@ else bootstrap_file="$*" fi -[ ! -f "$bootstrap_file" ] && die "$bootstrap_file does not exist" 10 1 +[ ! -f "$bootstrap_file" ] && die "$bootstrap_file does not exist" 2 1 private:get_bootstrap_path "$bootstrap_file"