From 9abdfb89f999ae9672dcba5afda20cfa2e75c16f Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 24 Nov 2019 23:59:38 +0100 Subject: [PATCH 1/9] Replace check_bin() by required_commands in main --- src/dotinstall | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotinstall b/src/dotinstall index 138e91c..679ddc6 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -351,7 +351,7 @@ esac if [[ $* =~ ^https://.*\.git$ || $* =~ ^ssh://.*\.git$ ]] then - check_bin "git" + required_commands "git" # Check localrepo="${DOTREPO}/$(basename $* .git)" From 01b2abba57df50d766768dcafe37f3c2b85fb91b Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 6 Nov 2020 18:05:35 +0100 Subject: [PATCH 2/9] Make spellcheck happy --- src/dotinstall | 191 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 126 insertions(+), 65 deletions(-) diff --git a/src/dotinstall b/src/dotinstall index 679ddc6..61ae603 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -32,35 +32,51 @@ die () # $1 : error message before quit # $2 : exit code - default 99 # $3 : 1 if print usage + local message + local code + local usage - [ -z $2 ] && $2=99 - printf "\e[31mFATAL : %s\e[0m\n" "$1" - [ $3 == 1 ] && usage - exit $2 + message="$1" + code="$2" + usage="$3" + + [ -z "$code" ] && code=99 + printf "\e[31mFATAL : %s\e[0m\n" "$message" + [ $usage -eq 1 ] && usage + exit $code } error () { # print error message in red # $@ : message - - printf "\e[31m%s\e[0m\n" "$*" + + local message + message="$*" + printf "\e[31m%s\e[0m\n" "$message" } private:get_bootstrap_path () { # this function return the absolute path of the bootstrap file # $* : path of bootstrap file + + local path + path="$*" # If the path begin with /, this is an absolute part - if [[ "$*" =~ ^/[[:graph:]]*$ ]] + if [[ "$path" =~ ^/[[:graph:]]*$ ]] then - repository="$(dirname $*)" - return + repository="$(dirname "$path")" + return fi - local relative_path="$*" - local current_path="$(pwd)" + local relative_path + local current_path + + current_path="$(pwd)" + relative_path="$path" + while [[ $relative_path =~ ^../[[:graph:]]*$ ]] do printf "We found ..\n" @@ -74,19 +90,22 @@ private:remove_symblink() { # Remove a symblink # $* source + + local source + source="$*" printf " -> Remove symblink %s : " "$symblink" - if [ -L "$*" ] + if [ -L "$source" ] then local ret; ret=$(rm "$*" 2>&1) [ $? -ne 0 ] && { error "can't remove : $ret"; return; } - elif [ ! -f "$*" ] + elif [ ! -f "$source" ] then - error "$* not exist" + error "$source not exist" return else - error "$* is not a symblink" + error "$source is not a symblink" fi printf "\e[32mdone\e[0m\n" } @@ -97,8 +116,11 @@ private:create_symblink () # $1: source # $2: destination - local source="$1" - local dest="$2" + local source + local dest + + source="$1" + dest="$2" printf " -> Create a symblink from %s : " "$source" @@ -116,11 +138,11 @@ private:create_symblink () error "can't overwrite destination" return else - rm -rf $dest + rm -rf "$dest" fi - elif [ -e $dest ] + elif [ -e "$dest" ] then - error "destination exist but is not a directory" + error "destin ation exist but is not a directory" return fi @@ -153,9 +175,15 @@ private:clone_repository () # $1 url # $2 local repository # return : local directory - + + local url + local repo local ret - ret=$(git clone -q "$1" "${2}" 2>&1) + + url="$1" + repo="$2" + + ret=$(git clone -q "$url" "$repo" 2>&1) [[ $? -ne 0 ]] && die "Can't clone_repository : $ret" 20 0 } @@ -164,12 +192,21 @@ 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 repo + repo="$1" + + [ ! -d "$repo" ] && die "Git update : directory $1 does not exist" 25 0 + + local current_dir local ret - cd "$1" + + current_dir=$(pwd) + + cd "$repo" || die "Can't go to directory ${repo}" ret=$(git pull 2>&1) [[ $? -ne 0 ]] && die "Can't update repository : $ret" 24 0 + + cd "$current_dir" || die "Can't return to directory $current_dir" } process_dirs () { @@ -180,20 +217,22 @@ process_dirs () { # $1: source directory # $2: destination directory - local source="${repository}/$1" - local dest="$2" + local source + source="${repository}/$1" + local dest + dest="$2" printf "\nProcess directory %s\n" "$source" [ ! -d "$source" ] && { error " -> source is not a directory"; return; } [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } - while read d + while read -r d do if [ $install -eq 1 ] then private:create_symblink "$d" "${dest}/$(basename "$d")" else - local symblink="${dest}/$(basename ${d})" + local symblink="${dest}/$(basename "$d")" private:remove_symblink "$symblink" fi @@ -207,20 +246,25 @@ process_files () { # $1: source directory # $2: destination directory - local source="${repository}/$1" - local dest="$2" + local source + local dest + + dest="$2" + source="${repository}/$1" + printf "Process files from directory %s:\n" "$source" [ ! -d "$source" ] && { error " -> source is not a directory"; return; } [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } - while read d + while read -r d do if [ $install -eq 1 ] then private:create_symblink "${source}/$d" "${dest}/$(basename "$d")" else - local symblink="${2}/$(basename ${d})" + local symblink + symblink="${dest}/$(basename ${d})" private:remove_symblink "$symblink" fi @@ -234,30 +278,36 @@ install_service () # $1 file # $2 1 to Activate the service - local source="${repository}/$1" + local source + local activate + source="${repository}/$1" + activate=$2 local ret [ ! -f "$source" ] && { error "$1 not found"; return; } - local basename=$(basename "$source") + + local basename + basename=$(basename "$source") + if [ $install -eq 1 ] then printf "\nInstall service %s :\n" "$source" private:create_symblink "$source" "${SYD_DIRECTORY}/${basename}" # activate service - if [ $2 ] + if [ $activate -eq 1 ] then - printf " -> Activate $basename : " - ret=$(systemctl --user enable ${basename} 2>&1) - [[ $? -ne 0 || ! $(systemctl --user is-enabled $basename) ]] && { error "$ret"; return; } + printf " -> Activate %s :" "$basename" + ret=$(systemctl --user enable "${basename}" 2>&1) + [[ $? -ne 0 || ! $(systemctl --user is-enabled "$basename") ]] && { error "$ret"; return; } printf "\e[32mdone\e[0m\n" fi else printf "\nUninstall service %s :\n" "$source" #Deactivate service - if [ $1 ] + if [ $activate -eq 1 ] then - printf " -> Deactivate $basename : " - ret=$(systemctl --user disable ${basename} 2>&1) + printf " -> Deactivate %s : " "$basename" + ret=$(systemctl --user disable "${basename}" 2>&1) [[ $? -ne 0 ]] && { error "$ret"; return; } printf "\e[32mdone\e[0m\n" fi @@ -269,14 +319,17 @@ required_commands () { # Check if an executable exist # $1 executables separated with spaces + + local bins + bins="$*" # if uninstall, we don't need to process required commands. [ $install -eq 0 ] && return printf "Checking required programs :\n" - for p in $1 + for bin in $bins do - printf " -> %s:" "$p" - command -v $p >/dev/null 2>&1 || die "not found" 128 + printf " -> %s:" "$bin" + command -v "$bin" >/dev/null 2>&1 || die "not found" 128 printf " \e[32mfound\e[0m\n" done } @@ -287,8 +340,10 @@ install_bin () # Via a symbolic link # $1 : path to script - local source="${repository}/${1}" - local dest="${BIN_DIRECTORY}/$(basename $1)" + local source + source="${repository}/${1}" + local dest + dest="${BIN_DIRECTORY}/$(basename "$1")" printf "\nProcess executable :\n" [ ! -f "$source" ] && { error "$source is not a file"; return; } @@ -308,21 +363,27 @@ define_env () # on uninstall mode # $1 variable name # $2 value + local name + local value + + name="$1" + value="$2" + + [ -z "$name" ] && { error "You must define a name"; return; } + [ ! -f "$ENV_FILE" ] && touch "$ENV_FILE" - [ -z $1 ] && { error "You must define a name"; return; } - [ ! -f $ENV_FILE ] && touch $ENV_FILE if [ $install -eq 1 ] then - printf "\nCreate an environment variable %s : " "$1" - [ $(grep -c -m 1 $1 $ENV_FILE) -eq 1 ] && { error "already exist"; return; } + printf "\nCreate an environment variable %s : " "$name" + [ $(grep -c -m 1 "$name" "$ENV_FILE") -eq 1 ] && { error "already exist"; return; } local line - line="export ${1}=\"${2}\"" - echo "$line" >> $ENV_FILE + line="export ${name}=\"${value}\"" + echo "$line" >> "$ENV_FILE" printf "\e[32mdone\e[0m\n" else - printf "\nRemove an environment variable %s : " "$1" - [ $(grep -c -m 1 $1 $ENV_FILE) -eq 0 ] && { error "not exist"; return; } - sed -i "/^export $1=*/d" $ENV_FILE + printf "\nRemove an environ ment variable %s : " "$1" + [ $(grep -c -m 1 "$1" "$ENV_FILE") -eq 0 ] && { error "not exist"; return; } + sed -i "/^export $1=*/d" "$ENV_FILE" printf "\e[32mdone\e[0m\n" fi @@ -354,7 +415,7 @@ then required_commands "git" # Check - localrepo="${DOTREPO}/$(basename $* .git)" + localrepo="${DOTREPO}/$(basename "$*" .git)" if [ $update -eq 1 ] then private:update_repository "$localrepo" @@ -362,7 +423,7 @@ then if [ $install -eq 1 ] then private:clone_repository "$*" "$localrepo" - fi + fi fi bootstrap_file="${localrepo}/bootstrap" else @@ -372,21 +433,21 @@ fi [ ! -f "$bootstrap_file" ] && die "$bootstrap_file does not exist" 2 1 private:get_bootstrap_path "$bootstrap_file" -cache_file="${CACHE_DIR}/$(echo $repository | tr '/' '_')" +cache_file="${CACHE_DIR}/$(echo "$repository" | tr '/' '_')" if [ $update -eq 1 ] then # Update mode - if [ -f "$cache_file" ] + if [ -f "$cache_file" ] then - if [ $(diff "$bootstrap_file" "$cache_file" > /dev/null; echo $?;) -eq 0 ] + if [ $(diff "$bootst rap_file" "$cache_file" > /dev/null; echo $?;) -eq 0 ] then - printf "There is no difference between cached copy and the bootstrap file\n"; + printf "There is no difference between cached copy and the bootstrap file\n"; exit 0 else # for updating the dotfile repo, we need to uninstall it then # install it. We need the cached copy for unistall. - install=0 + install=0 source "$cache_file" rm "$cache_file" install=1 @@ -405,8 +466,8 @@ source "$bootstrap_file" if [ $install -eq 0 ] then printf "\nRemove files for complete uninstall\n" - rm $cache_file - rm -rf $repository + rm "$cache_file" + rm -rf "$repository" fi exit 0 From 77d78841e3f20740fd02ed6fa02c16b977930213 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 6 Nov 2020 18:07:28 +0100 Subject: [PATCH 3/9] Don't delete repository when uninstalling --- src/dotinstall | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dotinstall b/src/dotinstall index 61ae603..3bc8aab 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -449,7 +449,6 @@ then # install it. We need the cached copy for unistall. install=0 source "$cache_file" - rm "$cache_file" install=1 fi else From 31a79b6238c3860d3361bb2c2a694a8a16157371 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 6 Nov 2020 18:14:13 +0100 Subject: [PATCH 4/9] Don't delete repository when uninstalling --- src/dotinstall | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dotinstall b/src/dotinstall index 3bc8aab..da44b0d 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -252,7 +252,7 @@ process_files () { dest="$2" source="${repository}/$1" - printf "Process files from directory %s:\n" "$source" + printf "\nProcess files from directory %s:\n" "$source" [ ! -d "$source" ] && { error " -> source is not a directory"; return; } [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } @@ -466,7 +466,6 @@ if [ $install -eq 0 ] then printf "\nRemove files for complete uninstall\n" rm "$cache_file" - rm -rf "$repository" fi exit 0 From 3546da3d2fd6c7f92d92429ff5474d2ceeb5a77a Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 6 Nov 2020 18:22:23 +0100 Subject: [PATCH 5/9] Uninstall unactivated service symlink --- src/dotinstall | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dotinstall b/src/dotinstall index da44b0d..f20c561 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -282,6 +282,9 @@ install_service () local activate source="${repository}/$1" activate=$2 + + [ -z "$activate" ] && activate=0 + local ret [ ! -f "$source" ] && { error "$1 not found"; return; } @@ -306,10 +309,12 @@ install_service () #Deactivate service if [ $activate -eq 1 ] then - printf " -> Deactivate %s : " "$basename" + printf " -> Deactivate %s : " "$basename" ret=$(systemctl --user disable "${basename}" 2>&1) [[ $? -ne 0 ]] && { error "$ret"; return; } printf "\e[32mdone\e[0m\n" + else + private:remove_symblink "${SYD_DIRECTORY}/${basename}" fi fi From 4bc141d2af8a68b4235f4c5a40e21c54c6ce8343 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 5 Oct 2021 16:40:55 +0200 Subject: [PATCH 6/9] Rename process functions --- src/dotinstall | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/dotinstall b/src/dotinstall index f20c561..a6b2a41 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -209,13 +209,12 @@ private:update_repository () cd "$current_dir" || die "Can't return to directory $current_dir" } -process_dirs () { - - # Process a directory than contains subdir, create symblink from each subdir - # to the destination. - # - # $1: source directory - # $2: destination directory +link_directory () { + + # Create a symblink to directory inside an other directory + # + # $1: source directory + # $2: destination directory local source source="${repository}/$1" @@ -223,23 +222,42 @@ process_dirs () { dest="$2" printf "\nProcess directory %s\n" "$source" + [ ! -d "$source" ] && { error " -> source is not a directory"; return; } + [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } + + if [ $install -eq 1 ] + then + private:create_symblink "$source" "${dest}/$(basename "$d")" + else + local symblink="${dest}/$(basename "$d")" + private:remove_symblink "$symblink" + fi +} + +link_directories () { + + # Process a directory than contains subdir, create symblink from each subdir + # to the destination. + # + # $1: source directory + # $2: destination directory + + local source + source="$1" + local dest + dest="$2" + printf "\nProcess directory %s\n" "$source" + [ ! -d "$source" ] && { error " -> source is not a directory"; return; } [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } while read -r d do - if [ $install -eq 1 ] - then - private:create_symblink "$d" "${dest}/$(basename "$d")" - else - local symblink="${dest}/$(basename "$d")" - private:remove_symblink "$symblink" - fi - + link_directory "$d" "${dest}" done < <(ls -d -1 "${source}"/*/) } -process_files () { +link_files () { # Process a directory than contains config files, create symblink from each # files to the destination. From bc2836bbda090c224eebfe794d0f3607d82bb1e0 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 5 Oct 2021 22:20:52 +0200 Subject: [PATCH 7/9] Some corrections on link_* functions --- src/dotinstall | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/dotinstall b/src/dotinstall index a6b2a41..8669469 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -217,11 +217,15 @@ link_directory () { # $2: destination directory local source - source="${repository}/$1" + source="$1" local dest dest="$2" printf "\nProcess directory %s\n" "$source" - + if [[ ! $source =~ ^$repository ]] + then + echo "Add repo before source" + source="${repository}/$source" + fi [ ! -d "$source" ] && { error " -> source is not a directory"; return; } [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } @@ -248,13 +252,10 @@ link_directories () { dest="$2" printf "\nProcess directory %s\n" "$source" - [ ! -d "$source" ] && { error " -> source is not a directory"; return; } - [ ! -d "$dest" ] && { error " -> destination is not a directory"; return; } - while read -r d do link_directory "$d" "${dest}" - done < <(ls -d -1 "${source}"/*/) + done < <(ls -d -1 "${repository}/${source}"/*/) } link_files () { From 94e1996ed4fb1a184b8fa516791de5877966f445 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 5 Oct 2021 22:43:15 +0200 Subject: [PATCH 8/9] Add process_* function for compatibility --- src/dotinstall | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/dotinstall b/src/dotinstall index 8669469..07d2a8d 100755 --- a/src/dotinstall +++ b/src/dotinstall @@ -258,6 +258,12 @@ link_directories () { done < <(ls -d -1 "${repository}/${source}"/*/) } +process_dirs () { + + printf "This function is deprecated, use link_directories instead.\n" + link_directories $@ +} + link_files () { # Process a directory than contains config files, create symblink from each @@ -290,6 +296,10 @@ link_files () { done < <(ls -A -1 "$source") } +process_files () { + printf "process_files is deprecated, use link_files instead\n" + link_files $@ +} install_service () { From 419c89540817fe96cd6cb33435bec50079fdb547 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 23 Nov 2021 17:24:51 +0100 Subject: [PATCH 9/9] Update dosumentation --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7d7bc88..aa204e4 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ This function check if binary file is avaible in the `PATH`. required_commands "ls mv git" ``` -#### conf_process_dirs +#### link_dirs Process a directory that contains others directories an symblink them to the destination folder : @@ -68,10 +68,11 @@ destination folder : - **return** : no value, exit the funtion if there is an error on path ``` -conf_process_dirs "${repository}/config" "${HOME}/.config" +link_dirs "${repository}/config" "${HOME}/.config" ``` +Old function `conf_process_dirs` still avaiable for compatibility. -#### conf_process_files +#### link_files Process a directory than contains files and symblink them to the destination folder : @@ -82,9 +83,11 @@ folder : - **return** : no value, exit the funtion if there is an error on path ``` -conf_process_dirs "${repository}/zshrc" "${HOME}" +link_files "${repository}/zshrc" "${HOME}" ``` +Old function `conf_process_files` still avaiable for compatibility + #### bin_install Symblink an executable file to the `~/.local/bin` folder.