changes the second input value to trigger push to create.

This commit is contained in:
Llewellyn van der Merwe 2020-11-12 11:02:31 +02:00
parent 6d5c3974ff
commit db28a20c04
Signed by: Llewellyn
GPG Key ID: EFC0C720A240551C

294
run.sh
View File

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