second commit

This commit is contained in:
pvincent
2024-02-22 09:56:25 +04:00
parent 35cb36cab6
commit 248193a773
21 changed files with 1865 additions and 2 deletions
+22
View File
@@ -0,0 +1,22 @@
#!/bin/bash
usage() {
echo "Usage: $(basename "$0") <REGEX> <STRING> <FILE>"
}
if [[ $# -ne 3 ]]; then
usage
exit 2
fi
REGEX=$1
STRING=$2
FILE=$3
if ! grep -Eq "$REGEX" "$FILE"; then
printf "$STRING\n" >>$FILE
echo appended
else
sed -Ei "s|$REGEX|$STRING|g" $FILE
echo replaced
fi
+42
View File
@@ -0,0 +1,42 @@
#!/bin/bash
# Clears the entire current line regardless of terminal size.
# See the magic by running:
# { sleep 1; clear_this_line ; }&
clear_this_line(){
printf '\r'
cols="$(tput cols)"
for i in $(seq "$cols"); do
printf ' '
done
printf '\r'
}
# Erases the amount of lines specified.
# Usage: erase_lines [AMOUNT]
# See the magic by running:
# { sleep 1; erase_lines 2; }&
erase_lines(){
# Default line count to 1.
test -z "$1" && lines="1" || lines="$1"
# This is what we use to move the cursor to previous lines.
UP='\033[1A'
# Exit if erase count is zero.
[ "$lines" = 0 ] && return
# Erase.
if [ "$lines" = 1 ]; then
clear_this_line
else
lines=$((lines-1))
clear_this_line
for i in $(seq "$lines"); do
printf "$UP"
clear_this_line
done
fi
}
erase_lines "$1"
+12
View File
@@ -0,0 +1,12 @@
#!/bin/bash
SIZE=${1:-12}
re='^[0-9]+$'
if ! [[ $SIZE =~ $re ]] ;then
echo "error: SIZE=$SIZE Not a number" >&2; exit 1
fi
if [[ $SIZE -lt 4 || $SIZE -gt 20 ]]; then
echo "expected SIZE=$SIZE not in range [4..20]" >&2; exit 1
fi
tr -cd '[:alnum:]' < /dev/urandom | fold -w $SIZE | head -n1
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
function usage() {
local BASECMD
BASECMD=$(basename "$0")
echo "usage: $BASECMD <packages...>"
echo 'idempotent debian package installation : update if necessary, install only if not yet done'
exit 1
}
[ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 2
[[ $# -lt 1 ]] && usage
if [ "$(date --date='-12 hours' +%s)" -gt "$(date -d "$(stat -c %y /var/lib/apt/lists/partial)" +%s)" ]; then
echo "updating repositoring..."
apt-get update
fi
for i in "$@"; do
if ! dpkg -l "$i" 2>/dev/null | grep -q ^ii; then
sudo apt-get install -y "$i"
elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then
echo "apt package <$i> already installed!"
fi
done
+18
View File
@@ -0,0 +1,18 @@
#!/bin/bash
set -Eeuo pipefail
if [[ -d /opt/miaou-bash ]]; then
local_release=$(cat /opt/miaou-bash/.semver_git_tag)
fi
echo -n "miaou-bash: collecting latest release from server url ... "
remote_release=$(wget_semver artcode miaou/miaou-bash)
echo -n "$remote_release"
if [[ $local_release != $remote_release ]]; then
echo " , upgrading from <$local_release> to <$remote_release> ..."
curl https://git.artcode.re/miaou/miaou-bash/raw/branch/main/install.sh | sudo bash -s -- --full
else
echo " up-to-date!"
fi
+150
View File
@@ -0,0 +1,150 @@
#!/bin/bash
function usage() {
echo 'usage: --major | -M | --minor | -m | --patch | -p'
exit 1
}
function setCommand() {
#arg1 input, #arg2 return value
declare -n ret=$2
case $1 in
-M | --major)
ret='major'
;;
-m | --minor)
ret='minor'
;;
-p | --patch)
ret='patch'
;;
--help)
usage
;;
*) # unknown option
echo
echo 'unknown option, quitting'
echo
usage
;;
esac
}
############# main
# check git covered
REPOSITORY=$(git config --get remote.origin.url)
if [[ "$?" -ne 0 ]]; then
echo 'no remote origin defined yet, please perform: git remote add'
exit 1
fi
# check if dirty status
changed=$(git status -s | wc -l)
if [[ ! $changed -eq 0 ]]; then
echo "git commit required, please do so before processing further"
exit 2
fi
git remote update
pulled_needed=$(git status -s -u no | wc -l)
if [[ ! $changed -eq 0 ]]; then
echo "git pull required, please do so before processing further"
exit 3
fi
current_branch=$(git rev-parse --abbrev-ref HEAD)
# check if push needed before tagging
push_needed=$(git rev-list --branches --not --remotes | wc -l)
if [[ $push_needed -ne 0 ]]; then
echo 'git push needed'
git push --set-upstream origin "$current_branch"
echo 'git push done'
echo '-------------'
fi
# check latest tagged version
LAST_TAG=$(git rev-list --tags --max-count=1 2>/dev/null)
if [[ $? -eq 0 && -n "$LAST_TAG" ]]; then
echo "LAST=${LAST_TAG}"
VERSION=$(git describe --tags $LAST_TAG)
else
echo 'not yet tagged!'
VERSION=0.0.0
fi
# split into array
VERSION_BITS=(${VERSION//./ })
MAJOR=${VERSION_BITS[0]:=0}
MINOR=${VERSION_BITS[1]:=0}
PATCH=${VERSION_BITS[2]:=0}
REPO_NAME=$(basename $REPOSITORY)
REPO_NAME=${REPO_NAME%.git}
if [[ $VERSION != '0.0.0' ]]; then
echo "LATEST VERSION of '$REPO_NAME' is '$MAJOR.$MINOR.$PATCH'"
COMMIT_ID=$(git log --format="%H" -n 1)
if [[ $COMMIT_ID == $LAST_TAG ]]; then
echo "cannot tag twice the same commit id : $LAST_TAG"
exit 1
fi
fi
COMMAND="${1:-ask}"
if [[ $COMMAND == 'ask' ]]; then
echo -n "Press 'M' for Major, 'm' for minor, 'p' for patch ? "
read -n1 input
echo
COMMAND="-$input"
fi
setCommand $COMMAND COMMAND
echo "before case"
case $COMMAND in
"major")
let "MAJOR++"
let "MINOR=0"
let "PATCH=0"
;;
"minor")
let "MINOR++"
let "PATCH=0"
;;
"patch")
let "PATCH++"
;;
esac
echo "show TAG"
TAG="$MAJOR.$MINOR.$PATCH"
echo "new TAG $TAG"
# check if tag exists already!
if [[ -n $(git tag -l $TAG) ]]; then
echo "WARNING: ${TAG} already exists!"
exit 4
fi
# check whether NPM project detected, containing package.json on root project
GIT_ROOT_FOLDER=$(dirname $(git rev-parse --git-dir))
if [[ -f "${GIT_ROOT_FOLDER}/package.json" ]]; then
echo "package.json detected, version replaced with new tag <${TAG}>"
if ! command -v yq &>/dev/null; then
echo 'command <yq> not found, hint: go and fetch latest release from https://github.com/mikefarah/yq'
exit 5
fi
yq -i ".version=\"$TAG\"" "${GIT_ROOT_FOLDER}/package.json"
git add ${GIT_ROOT_FOLDER}/package.json
fi
echo $TAG >${GIT_ROOT_FOLDER}/.semver_git_tag
git add ${GIT_ROOT_FOLDER}/.semver_git_tag
echo "tagging as $TAG ..."
git commit -am "tagged as ${TAG}"
git tag $TAG
git push origin "$current_branch" --tags
echo "done"
Executable
+13
View File
@@ -0,0 +1,13 @@
#!/bin/bash
echo >&2 "time elapsed in seconds..."
START=$(date +%s.%N)
# do something #######################
$@
#######################################
END=$(date +%s.%N)
DIFF=$(echo "scale=2; (${END} - ${START}) / 1" | bc)
echo "${DIFF}"
+10
View File
@@ -0,0 +1,10 @@
#!/bin/bash
SOURCE=$(realpath "$1")
DEST=$(realpath "$2")
[ ! -d "$SOURCE" ] && echo "SOURCE FOLDER <$SOURCE> not reachable!" && exit 1
[ ! -d "$DEST" ] && echo "DESTINATION FOLDER missing!" && exit 2
[ "$SOURCE" == "$DEST" ] && echo "SOURCE and DESTINATION overriden!" && exit 3
rsync -a --no-inc-recursive --info=progress2 --remove-source-files "$SOURCE" "$DEST" && find "$SOURCE" -type d -empty -delete
+43
View File
@@ -0,0 +1,43 @@
#!/bin/bash
REPO_TYPE=$1
REPO_NAME=$2
DESTINATION=${3:-.}
function usage {
local BASECMD=$(basename "$0")
echo 'usage: <REPO_TYPE> <REPO_NAME> [DESTINATION]'
echo 'example:'
echo -e '\t# REPO_TYPE=github'
echo -e "\t$BASECMD github Dolibarr/dolibarr\n"
echo -e '\t# REPO_TYPE=artcode'
echo -e "\t$BASECMD artcode miaou/miaou-gnome\n"
echo -e '\t# With destination'
echo -e "\t$BASECMD artcode libre/libre-gnome /tmp # folder"
echo -e "\t$BASECMD artcode libre/libre-gnome /tmp/release.tgz # file"
exit -1
}
function get_github {
local BASE="https://github.com"
local release=$( wget_semver $REPO_TYPE $REPO_NAME )
local url="$BASE/${REPO_NAME}/archive/refs/tags/${release}.tar.gz"
if [[ -d $DESTINATION ]]; then
DESTINATION="$DESTINATION/$(echo $REPO_NAME | cut -d '/' -f2 )-${release}.tgz"
fi
>&2 wget $url -O $DESTINATION
echo $DESTINATION
}
[[ $# -lt 2 ]] && usage
case $REPO_TYPE in
github )
get_github
;;
* )
echo "repository type <${REPO_TYPE}> not yet supported!" && exit 1
;;
esac
+60
View File
@@ -0,0 +1,60 @@
#!/bin/bash
REPO_TYPE=$1
REPO_NAME=$2
function usage() {
local BASECMD=$(basename "$0")
echo 'usage: <REPO_TYPE> <REPO_NAME> [DESTINATION]'
echo 'example:'
echo -e '\t# REPO_TYPE=github'
echo -e "\t$BASECMD github Dolibarr/dolibarr\n"
echo -e '\t# REPO_TYPE=artcode'
echo -e "\t$BASECMD artcode libre/libre-gnome\n"
exit -1
}
function get_github() {
local all_releases=$(git ls-remote --tags --sort="v:refname" https://github.com/"$REPO_NAME" | grep -Eo "v?([0-9]+\.){2}[0-9]+$")
# extract only VERSION without 'v'
local non_v_release=$(echo "$all_releases" | grep -v v | tail -n1 | cut -f2 | cut -d '/' -f3)
non_v_release=${non_v_release%^\{\}} # remove extra characters ^{} from github
# extract remaining version including 'v'
local v_release=$(echo "$all_releases" | tail -n1 | cut -f2 | cut -d '/' -f3)
v_release=${v_release%^\{\}} # remove extra characters ^{} from github
# compare which newer from non_v_release to v_release
if [ -n "$non_v_release" ] && [ "v$non_v_release" \> "$v_release" ]; then
echo $non_v_release
else
echo $v_release
fi
}
function get_artcode() {
release=$(git ls-remote --tags --sort="v:refname" \
https://git.artcode.re/$REPO_NAME |
tail -n1 | cut -f2 | cut -d '/' -f3)
echo $release
}
[[ $# -ne 2 ]] && usage
if [ -z "$(command -v git)" ]; then
echo "INSTALLING PACKAGE <GIT> required"
sudo apt install -y git
fi
case $REPO_TYPE in
github)
get_github
;;
artcode)
get_artcode
;;
*)
echo "repository type <${REPO_TYPE}> not yet supported!" && exit 1
;;
esac