Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 2 additions & 77 deletions kreypi/README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,5 @@
# KREYPI!
Kreyren's way to be more effective writing bash scripts
Kreyren's way to be more effective writing shell/bash scripts

## Usage
To use this BASH API place this codeblock at the top of your bash script:

```bash
### START OF KREYPI INIT ###
# https://github.com/RXT067/Scripts/tree/kreyren/kreypi

# Do not make additional functions here since we are going to source a library

# Check for root
if [ "$(id -u)" != 0 ]; then
printf 'FATAL: %s\n' "This script is using KREYPI library which needs to be exported in /lib/shell using root permission"
exit 3
elif [ "$(id -u)" = 0 ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Script executed from an user with ID $(id -u)"
else
printf 'FATAL: %s\n' "Unexpected happend in KREYPI_INIT for checking root"
exit 255
fi

# Create a new directory for shell libraries if not present already
if [ ! -e /lib/shell ]; then
mkdir /lib/shell || { printf 'FATAL: %s\n' "Unable to make a new directory in '/lib/shell', is this non-standard file hierarchy?" ; exit 1 ;}
elif [ -f /lib/shell ]; then
printf 'FATAL: %s\n' "File '/lib/shell' is a file which is unexpected, expecting directory to export kreypi library"
exit 1
elif [ -d /lib/shell ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory '/lib/shell' already exists, no need to make it"
else
printf 'FATAL: %s\n' "Unexpected result in KREYPI_INIT checking for /lib/shell"
exit 255
fi

# Fetch the library
if [ -e /lib/shell/kreypi.sh ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory in '/lib/shell' already exists, skipping fetch"
elif command -v wget >/dev/null; then
wget https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh -O /lib/shell/kreypi.sh || { printf 'FATAL: %s\n' "Unable to fetch kreypi.sh from https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh in /lib/shell/kreypi.sh using wget" ; exit 1;}
elif command -v curl >/dev/null; then
curl https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh -o /lib/shell/kreypi.sh || { printf 'FATAL: %s\n' "Unable to fetch kreypi.sh from https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh in /lib/shell/kreypi.sh using curl" ; exit 1 ;}
else
printf 'FATAL: %s\n' "Unable to download kreypi library from 'https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh' in '/lib/shell/kreypi.sh'"
exit 255
fi

# Sanitycheck for /lib/shell
if [ -e /lib/shell ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory in '/lib/shell' already exists, passing sanity check"
elif [ ! -e /lib/shell ]; then
printf 'FATAL: %s\n' "Sanitycheck for /lib/shell failed"
exit 1
else
printf 'FATAL: %s\n' "Unexpected happend in sanitycheck for /lib/shell"
exit 255
fi

# Source KREYPI
if [ -e "/lib/shell/kreypi.sh" ]; then
# 'source' can not be used on POSIX sh
# shellcheck source="/lib/shell/kreypi.sh"
. "/lib/shell/kreypi.sh" || { printf 'FATAL: %s\n' "Unable to source '/lib/shell/kreypi.sh'" ; exit 1 ;}
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Kreypi in '/lib/shell/kreypi.sh' has been successfully sourced"
elif [ ! -e "/lib/shell/kreypi.sh" ]; then
printf 'FATAL: %s\n' "Unable to source '/lib/shell/kreypi.sh' since path does not exists"
exit 1
else
printf 'FATAL: %s\n' "Unexpected happend in sourcing KREYPI_INIT"
exit 255
fi

### END OF KREYPI INIT ###
```
FIXME
24 changes: 24 additions & 0 deletions kreypi/functions/checkroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
# Created by Jacob Hrbek <kreyren@rixotstudio.cz> in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html)

: '
Check if script has been executed with root permission if not and variale KREPI_CHECKROOT_WORKAROUND_ROOT is set it will try to invoke itself as root'

# Checkroot - Check if executed as root, if not tries to use sudo if KREYPI_CHECKROOT_USE_SUDO variable is not blank
checkroot() {
die fixme "Checkroot needs more sanitization"

if [ "$(id -u)" = 0 ]; then
return 0
elif command -v sudo >/dev/null && [ -n "$KREYPI_CHECKROOT_USE_SUDO" ] && [ -n "$(id -u)" ]; then
info "Failed to aquire root permission, trying reinvoking with 'sudo' prefix"
exec sudo "$0 -c\"$*\""
elif ! command -v sudo >/dev/null && [ -n "$KREYREN" ] && [ -n "$(id -u)" ]; then
einfo "Failed to aquire root permission, trying reinvoking as root user."
exec su -c "$0 $*"
elif [ "$(id -u)" != 0 ]; then
die 3
else
die 255 "checkroot"
fi
}
33 changes: 33 additions & 0 deletions kreypi/functions/eexeccheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh
# Created by Jacob Hrbek <kreyren@rixotstudio.cz> in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html)

: '
Wrapper used to check if parsed command is executable on the system

SYNOPSIS: eexechcheck [command]

Returns 0 if command is executable or 126 if it is not

Expected to be piped in die if fatal is expected

eexeccheck wget | die'

# Check executable
eexeccheck() {
eexeccheckCommand="$1"

fixme "Terminator for eexecheck is not configured to output command name in output"
die 1 "I'm not sure how i want to implement this, stubbing for now - Kreyren"

if ! command -v "$eexeccheckCommand" >/dev/null; then
# Not executable
return 126
elif command -v "$eexeccheckCommand" >/dev/null; then
# Is executable
return 0
else
die 255 "check_exec"
fi

unset eexeccheckCommand
}
28 changes: 28 additions & 0 deletions kreypi/functions/eextract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
# Created by Jacob Hrbek <kreyren@rixotstudio.cz> in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html)

: '
Wrapper used to extract various archives in target directory

SYNOPSIS: eextract [archive] [destdir] (expected files)'

eextract() {
# Capture arguments
while [ $# -ge 1 ]; do case "$1" in
*.tar.gz) file="$1" ; shift 1 ;;
*/) targetdir="$1" ; shift 1 ;;
*) die 2 "Unsupported argument parsed in extractor - '$1'"
esac; done

die fixme "extractor is not finished"

# Action based on file imported
case "$file" in
*.tar.xz)
if command -v tar >/dev/null; then
tar -Jxpf "$file" -C "$targetdir" || die 1 "Unable to extract archive '$file' to '$targetdir' using tar"
fi
;;
*) die 255 "Unexpected file parsed in extractor"
esac
}
Empty file added kreypi/functions/efetch.sh
Empty file.
59 changes: 59 additions & 0 deletions kreypi/functions/egit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/sh
# Created by Jacob Hrbek <kreyren@rixotstudio.cz> in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html)

: "
Wrapper used for git command"

egit() {
argument="$1"
MYFUNCNAME="egit"

case "$argument" in
clone)
git_url="$3"
destdir="$4"

# Check if 'git' is executable
if ! command -v "git" >/dev/null; then die 126 "command 'git' is not executable"; fi

# Sanitization for variable 'git_url'
case $git_url in
https://*.git) true ;;
*) die 1 "$MYFUNCNAME: Argument '$1' doesn't match 'https://*.git'"
esac

# Sanitization for variable 'destdir'
if [ -d "$destdir" ]; then
true
elif [ ! -d "$destdir" ]; then
case $destdir in
/*) true ;;
# Sanitization to avoid making a git repositories in a current working directory
"") die 2 "$MYFUNCNAME-$argument is not supported to run without a specified directory" ;;
*) die 1 "Variable destdir '$destdir' is not a valid directory for command '$MYFUNCNAME $argument $git_url $destdir'"
esac
else
die 255 "$MYFUNCNAME $argument - destdir"
fi

fixme "$MYFUNCNAME $argument: Check if directory already cloned git repository"

# Action
git clone "$git_url" "$destdir"

git_err_code="$?"

fixme "Add translate for $MYFUNCNAME $argument"
case $git_err_code in
0) debug "Command 'git $argument $git_url $destdir' returned true" ;;
128) info "Command 'git' already cloned '$git_url' in '$destdir'" ;;
*) die 1 "Command 'git clone $git_url $destdir' returned an unknown error code: $git_err_code"
esac

unset git_url destdir git_err_code
;;
*) die fixme "Argument $argument is not supported by $MYFUNCNAME"
esac

unset argument MYFUNCNAME
}
79 changes: 79 additions & 0 deletions kreypi/functions/emkdir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/sh
# Created by Jacob Hrbek <kreyren@rixotstudio.cz> in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html)

: '
Wrapper for `mkdir` command

SYNOPSIS: emkdir pathname/to/directory permission user group

Currently accepted values for permission are {0000..7777}'

emkdir() {
# TODO: capture everything that has syntax of path in $1

# Naming set this way to avoid conflicts with other variables since we are unseting these at the end
emkdirTargetdir="$1"
emkdirPermission="$2"
emkdirUserperm="$3"
emkdirGroupperm="$4"

# Path check
if [ ! -d "$emkdirTargetdir" ]; then
debug "Creating a directory in '$emkdirTargetdir'"
mkdir "$emkdirTargetdir" || die 1 "Unable to make a new directory in '$emkdirTargetdir'"
elif [ -d "$emkdirTargetdir" ]; then
debug "Directory '$emkdirTargetdir' already exists, skipping creation"
elif [ -f "$emkdirTargetdir" ]; then
die 1 "Path '$emkdirTargetdir' is a file which is unexpected, skipping creation of directory"
else
die 255 "emkdir - path check"
fi

# Check permission
case "$emkdirPermission" in
[0-9][0-9][0-9][0-9])
if [ "$(stat -c "%#a" "$emkdirTargetdir" 2>/dev/null)" != "$emkdirPermission" ]; then
debug "Changing permisson of '$emkdirTargetdir' on '$emkdirPermission'"
chmod "$emkdirPermission" "$emkdirTargetdir" || die 1 "Unable to change permission '$emkdirPermission' for '$emkdirTargetdir'"
elif [ "$(stat -c "%#a" "$emkdirTargetdir" 2>/dev/null)" = "$emkdirPermission" ]; then
debug "Directory '$emkdirTargetdir' already have permission set on '$emkdirPermission'"
else
die 255 "Checking permission for '$emkdirTargetdir'"
fi ;;
*) die 2 "Second argument '$emkdirPermission' does not match syntax '[0-9][0-9][0-9][0-9]'"
esac

# Check user permission
if [ -n "$emkdirUserperm" ]; then
if [ "$(stat -c "%U" "$emkdirTargetdir" 2>/dev/null)" != "$emkdirUserperm" ]; then
debug "Changing user permission of '$emkdirTargetdir' on '$emkdirUserperm'"
chown "$emkdirUserperm" "$emkdirTargetdir" || die 1 "Unable to change user permission of '$emkdirTargetdir' on '$emkdirUserperm'"
elif [ "$(stat -c "%U" "$emkdirTargetdir" 2>/dev/null)" = "$emkdirUserperm" ]; then
debug "User permission of '$emkdirTargetdir' is already '$emkdirUserperm'"
else
die 255 "emkdir checking for userperm"
fi
elif [ -n "$emkdirUserperm" ]; then
debug "User permission for '$emkdirTargetdir' is not specified, skipping changing"
else
die 255 "emkdir check for userperm variable"
fi

# Check group permission
if [ -n "$emkdirGroupperm" ]; then
if [ "$(stat -c "%G" "$emkdirTargetdir" 2>/dev/null)" != "$emkdirGroupperm" ]; then
debug "Changing group permission of '$emkdirTargetdir' on '$emkdirGroupperm'"
chgrp "$emkdirGroupperm" "$emkdirTargetdir" || die 1 "Unable to change group permission of '$emkdirTargetdir' on '$emkdirGroupperm'"
elif [ "$(stat -c "%G" "$emkdirTargetdir" 2>/dev/null)" = "$emkdirGroupperm" ]; then
debug "Group permission of '$emkdirTargetdir' is already '$emkdirGroupperm'"
else
die 255 "Checking group permission of '$emkdirTargetdir'"
fi
elif [ -z "$emkdirGroupperm" ]; then
debug "Group permission is not specified for '$emkdirTargetdir', skipping change"
else
die 255 "emkdir checking for groupperm variable"
fi

unset emkdirTargetdir emkdirPermission emkdirUserperm emkdirGroupperm
}
Loading