changes the second input value to trigger push to create.

This commit is contained in:
2020-11-12 11:02:31 +02:00
parent 6d5c3974ff
commit db28a20c04

194
run.sh
View File

@@ -2,8 +2,8 @@
# set the defaults # set the defaults
ORG="octoleo" ORG="octoleo"
ROOTDIR="$PWD" ROOT_DIR="$PWD"
REMOTESYSTEM="git.vdm.dev" REMOTE_SYSTEM="git.vdm.dev"
PUSH_CREATE=false PUSH_CREATE=false
# add env to system # add env to system
@@ -13,157 +13,215 @@ fi
# catch the values passed # catch the values passed
ORG="${1:-$ORG}" ORG="${1:-$ORG}"
ROOTDIR="${2:-$ROOTDIR}" PUSH_CREATE="${2:-$PUSH_CREATE}"
REMOTESYSTEM="${3:-$REMOTESYSTEM}" ROOT_DIR="${3:-$ROOT_DIR}"
REMOTE_SYSTEM="${4:-$REMOTE_SYSTEM}"
# show globals if needed # show globals if needed
# echo "$ORG" # echo "$ORG"
# echo "$ROOTDIR" # echo "$ROOT_DIR"
# echo "$REMOTESYSTEM" # echo "$REMOTE_SYSTEM"
# exit # exit
main() { main() {
# make sure the root dir exist # make sure the root dir exist
if [ -d "$ROOTDIR" ]; then if [ -d "$ROOT_DIR" ]; then
# go to folder where zip files are placed # go to folder where zip files are placed
cd "$ROOTDIR" cd "$ROOT_DIR" || exit 3
# check that the folder has zip files # check that the folder has zip files
[ $( ls -1 *.zip 2>/dev/null | wc -l ) == 0 ] && showerror "looking for zip files, since no zip files in the folder $ROOTDIR ERROR-NR" 6 # shellcheck disable=SC2012
[ "$(ls -1 ./*.zip 2>/dev/null | wc -l)" == 0 ] && showError "looking for zip files, since no zip files in the folder $ROOT_DIR ERROR-NR" 6
# get all zip files # get all zip files
for z in *.zip; do for z in *.zip; do
# get folder and repo name # get folder and repo name
repo=$(getreponame "$z") repo=$(getRepoName "$z")
# get folder and repo name # get folder and repo name
version=$(getversion "$z") version=$(getVersion "$z")
echo "${repo} - v${version}" echo "${repo} - v${version}"
# check if the repo exist on our system # check if the repo exist on our system
git ls-remote "git@${REMOTESYSTEM}:${ORG}/${repo}.git" -q >/dev/null 2>&1 && updaterepo "$repo" "${version:-0}" "$z" || setuprepo "$repo" "${version:-0}" "$z" if git ls-remote "git@${REMOTE_SYSTEM}:${ORG}/${repo}.git" -q >/dev/null 2>&1; then
setExistingRepository "$repo" "${version:-0}" "$z" || showError "Update Repo:(${repo}) ERROR-NR" 7
else
setNewRepository "$repo" "${version:-0}" "$z" || showError "Setup Repo:(${repo}) ERROR-NR" 7
fi
done done
else else
showerror "opening of folder $ROOTDIR as it does not exist ERROR-NR" 5 showError "opening of folder $ROOT_DIR as it does not exist ERROR-NR" 5
fi fi
} }
# get repository name # get repository name
getreponame() { getRepoName() {
local name local name
name=$(rstrip "$1" "-v*") name=$(rightStrip "$1" "-v*")
name=$(rstrip "$name" "_v*") name=$(rightStrip "$name" "_v*")
name=$(rstrip "$name" ".zip") name=$(rightStrip "$name" ".zip")
echo "$name" echo "$name"
} }
# get current version # get current version
getversion() { getVersion() {
local version local version
version=$(lstrip "$1" "*-v") version=$(leftStrip "$1" "*-v")
version=$(lstrip "$version" "*_v") version=$(leftStrip "$version" "*_v")
version=$(rstrip "$version" ".zip") version=$(rightStrip "$version" ".zip")
version=${version//-/\.} version=${version//-/\.}
version=${version//_/\.} version=${version//_/\.}
version=$(rstrip "$version" "..*") version=$(rightStrip "$version" "..*")
echo "$version" echo "$version"
} }
# Strip pattern from end of string # Strip pattern from end of string
rstrip() { rightStrip() {
# Usage: rstrip "string" "pattern" # Usage: rightStrip "string" "pattern"
printf '%s\n' "${1%%$2}" printf '%s\n' "${1%%$2}"
} }
# Strip pattern from start of string # Strip pattern from start of string
lstrip() { leftStrip() {
# Usage: lstrip "string" "pattern" # Usage: leftStrip "string" "pattern"
printf '%s\n' "${1##$2}" printf '%s\n' "${1##$2}"
} }
# setup new repository # setup new repository
setuprepo() { setNewRepository() {
echo "New repository (${REMOTESYSTEM}:${ORG}/$1)" echo "New repository (${REMOTE_SYSTEM}:${ORG}/$1)"
# set repo path # set repo path
repopath="$ROOTDIR/$1" repo_path="$ROOT_DIR/$1"
# set zip path # set zip path
zippath="$ROOTDIR/$3" zip_path="$ROOT_DIR/$3"
# we creat the repo dir # we creat the repo dir
mkdir -p "$repopath" mkdir -p "$repo_path"
# change to this directory and make sure its empty # change to this directory and make sure its empty
cd "$repopath" >/dev/null 2>&1 && rm -rf * || showerror "$1" 3 if cd "$repo_path" >/dev/null 2>&1; then
rm -rf ./*
else
return 1
fi
# we unzip the data to this new folder # we unzip the data to this new folder
unzip "$zippath" -d "$repopath" >/dev/null 2>&1 && echo "Succefuly unzipped the package" || showerror "$1" 4 if unzip "$zip_path" -d "$repo_path" >/dev/null 2>&1; then
# we inistialize the git repo localy echo "Successfully unzipped the package"
makegitinit "$1" "$2" else
return 1
fi
# we initialize the git repo locally
setGitInit "$1" "$2" || return 1
# check if push create is allowed # check if push create is allowed
if ( $PUSH_CREATE ); then if ($PUSH_CREATE); then
# we push to create this repo # we push to create this repo
git push -u origin master git push -u origin master >/dev/null 2>&1
# check if we have tags to push
git push --tags >/dev/null 2>&1
# always move back to root folder # always move back to root folder
cd "$ROOTDIR" cd "$ROOT_DIR" || return 1
# we remove the ZIP file # we remove the ZIP file
rm "$zippath" rm "${zip_path:?}"
# we can also remove the folder # we can also remove the folder
rm -rf "$repopath" rm -rf "${repo_path:?}"
# pushed create
echo "Push created ($1-v$2) repository"
else else
# give action info # give action info
echo "When ready link and push changes to remote" echo "When ready link and push changes to remote"
# always move back to root folder # always move back to root folder
cd "$ROOTDIR" cd "$ROOT_DIR" || return 1
# we remove the ZIP file # we remove the ZIP file
# rm "$zippath" (don't remove since we may need to run this again if a name of the zip has changed) # rm "$zip_path" (don't remove since we may need to run this again if a name of the zip has changed)
fi fi
return 0
} }
# show an error # show an error
showerror() { showError() {
echo "We encounterd and error on $1:$2" echo "We encountered and error on $1:$2"
exit $2 exit "$2"
} }
# make git commit of the update and set the new tag # make git commit of the update and set the new tag
makegitinit(){ setGitInit() {
# check if this is an existing local repo # check if this is an existing local repo
if [ -d ".git" ]; then if [ -d ".git" ]; then
[[ -z $(git status --porcelain) ]] && echo "No changes found in repository" || makegitcommit "$2" "update - v$2" if [[ -z $(git status --porcelain) ]]; then
echo "No changes found in repository"
else else
git init if [ "$(git tag -l "v$2")" ]; then
makegitcommit "$2" "first commit - v$2" setGitCommit "$2" "update" || return 4
git remote add origin "git@${REMOTESYSTEM}:${ORG}/${1}.git" else
setGitCommit "$2" "update - v$2" || return 4
fi fi
fi
else
git init || return 4
setGitCommit "$2" "first commit - v$2" || return 4
git remote add origin "git@${REMOTE_SYSTEM}:${ORG}/${1}.git"
fi
return 0
} }
# make git commit of the update and set the new tag # make git commit of the update and set the new tag
makegitcommit(){ setGitCommit() {
git add . git add . >/dev/null 2>&1 || return 1
git commit -am"$2" git commit -am"$2" >/dev/null 2>&1 || return 1
git tag "v$1" # check if tag exist
if [ "$(git tag -l "v$1")" ]; then
return 0
else
git tag "v$1" >/dev/null 2>&1 || return 1
fi
return 0
} }
# make git commit of the update and set the new tag # make git commit of the update and set the new tag
pushchanges(){ setPushChanges() {
git push git push >/dev/null 2>&1 || return 1
git push --tags git push --tags >/dev/null 2>&1 || return 1
return 0
} }
# update repository repository # update repository repository
updaterepo() { setExistingRepository() {
echo "Update ($1-v$2) repository" echo "Update ($1-v$2) repository"
# set repo path # set repo path
repopath="$ROOTDIR/$1" repo_path="$ROOT_DIR/$1"
# set zip path # set zip path
zippath="$ROOTDIR/$3" zip_path="$ROOT_DIR/$3"
# check if repo is localy found # check if repo is locally found
cd "$repopath" >/dev/null 2>&1 || git clone git@${REMOTESYSTEM}:${ORG}/${1}.git cd "$repo_path" >/dev/null 2>&1 || git clone "git@${REMOTE_SYSTEM}:${ORG}/${1}.git"
# make sure we are in the correct dir # make sure we are in the correct dir
cd "$repopath" >/dev/null 2>&1 && rm -rf * || showerror "$1" 1 if cd "$repo_path" >/dev/null 2>&1; then
rm -rf ./*
else
return 1
fi
# now lets unzip the new data to this repo # now lets unzip the new data to this repo
unzip "$zippath" -d "$repopath" >/dev/null 2>&1 && echo "Succefuly unzipped the package" || showerror "$1" 2 if unzip "$zip_path" -d "$repo_path" >/dev/null 2>&1; then
echo "Successfully unzipped the package"
else
# break out
return 1
fi
# lets check if there are changes # lets check if there are changes
[[ -z $(git status --porcelain) ]] && echo "No changes found in ($1-v$2) repository" || (makegitcommit "$2" "update - v$2" && pushchanges) if [[ -z $(git status --porcelain) ]]; then
echo "No changes found in ($1-v$2) repository"
else
if [ "$(git tag -l "v$2")" ]; then
setGitCommit "$2" "update" || return 4
else
setGitCommit "$2" "update - v$2" || return 4
fi
# push the changes
setPushChanges || return 1
# updated the repository
echo "Pushed update to ($1-v$2) repository"
fi
# always move back to root folder # always move back to root folder
cd "$ROOTDIR" cd "$ROOT_DIR" || return 1
# we remove the ZIP file # we remove the ZIP file
rm "$zippath" rm "${zip_path:?}"
# we can also remove the folder # we can also remove the folder
rm -rf "$repopath" rm -rf "${repo_path:?}"
# success
return 0
} }
# run main # run main