1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-12-25 02:23:01 +00:00

POSIX compliance, remove bash dependency

This commit is contained in:
Jörn-Michael Miehe 2020-08-25 14:34:30 +02:00
parent bcf348a5a3
commit 8fbc44e3ff
2 changed files with 46 additions and 53 deletions

View file

@ -4,55 +4,39 @@
# CONSTANTS #
#############
# dependencies to run kiwi-config
KIWI_DEPS="bash python3 pipenv less"
# default install directory
# default installation directory
INSTALL_DIR_DEFAULT="/usr/local/bin"
##########
# CHECKS #
##########
printf "checking dependencies ... "
############
# CLI ARGS #
############
for dep in ${KIWI_DEPS}; do
printf "%s, " "${dep}"
# installation directory
install_dir="${1}"
# adjust default if given
INSTALL_DIR_DEFAULT="${1:-${INSTALL_DIR_DEFAULT}}"
if ! command -v "${dep}" >/dev/null 2>/dev/null; then
echo
echo "Dependency '${dep}' not found, please install!" >/dev/stderr
exit 1
fi
done
echo "OK"
########
# MAIN #
########
# prompt for installation directory
valid="no"
while [ "${valid}" = "no" ]; do
while [ ! -d "${install_dir}" ]; do
printf "Select installation directory [Default: '%s']: " "${INSTALL_DIR_DEFAULT}"
read install_dir </dev/tty || install_dir="${INSTALL_DIR_DEFAULT}"
read -r install_dir </dev/tty || install_dir="${INSTALL_DIR_DEFAULT}"
install_dir="${install_dir:-${INSTALL_DIR_DEFAULT}}"
# check
if [ -d "${install_dir}" ]; then
valid="yes"
else
# check if given dir exists
if [ ! -d "${install_dir}" ]; then
printf "Install directory doesn't exist. Try creating? [Y|n] "
read yesno </dev/tty || yesno="yes"
if [ ! "${yesno}" = "N" ] || [ ! "${yesno}" = "n" ]; then
read -r yesno </dev/tty || yesno="yes"
yesno=$(printf '%.1s' "${yesno}")
# check creation failure
if mkdir -p "${install_dir}"; then
valid="yes"
else
if [ ! "${yesno}" = "N" ] && [ ! "${yesno}" = "n" ]; then
# fail this script if we can't create the install dir
if ! mkdir -p "${install_dir}"; then
echo "Invalid install directory." >/dev/stderr
exit 1
fi
@ -60,6 +44,11 @@ while [ "${valid}" = "no" ]; do
fi
done
if [ ! -d "${install_dir}" ]; then
echo "wtf?"
exit 1
fi
# start actual installation
printf "Installing into '%s' ... " "${install_dir}"

44
kiwi
View file

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
#############
# CONSTANTS #
@ -10,7 +10,7 @@ KIWI_CONF_NAME="kiwi.yml"
KIWI_VERSION_TAG="etc/version_tag"
# dependencies to run kiwi-config
KIWI_DEPS=(docker docker-compose)
KIWI_DEPS="python3 pipenv less docker docker-compose"
# base install dir
KIWI_BASEDIR="${HOME}/.local/lib/kiwi-config"
# per-user env setup script
@ -21,28 +21,30 @@ KIWI_REPO="https://github.com/ldericher/kiwi-config"
# use latest version by default
KIWI_VERSION="master"
###################
# DYNAMIC STRINGS #
###################
# directory of correct installation
function kiwi_installdir() {
kiwi_installdir() {
echo "${KIWI_BASEDIR}/${KIWI_VERSION}"
}
# src directory in installed version
function kiwi_root() {
kiwi_root() {
echo "$(kiwi_installdir)/src"
}
# main script in installed version
function kiwi_executable() {
kiwi_executable() {
echo "$(kiwi_root)/kiwi-config.py"
}
# cache current work directory
WORKDIR="$(pwd)"
##################
# PER-USER SETUP #
##################
@ -50,20 +52,22 @@ WORKDIR="$(pwd)"
# add in environment setup
if [ -f "${KIWI_ENVFILE}" ]; then
# shellcheck source=$HOME/.kiwienv
source "${KIWI_ENVFILE}"
. "${KIWI_ENVFILE}"
fi
##########
# CHECKS #
##########
for dep in "${KIWI_DEPS[@]}"; do
if ! command -v "${dep}" &>/dev/null; then
for dep in ${KIWI_DEPS}; do
if ! command -v "${dep}" >/dev/null 2>/dev/null; then
echo "Dependency '${dep}' not found, please install!" >/dev/stderr
exit 1
fi
done
########
# MAIN #
########
@ -77,7 +81,7 @@ fi
# install if kiwi-config not found
if [ ! -x "$(kiwi_executable)" ]; then
echo -n "Installing kiwi-config v${KIWI_VERSION} into ${KIWI_BASEDIR} ... "
printf "Installing kiwi-config v%s into %s ... " "${KIWI_VERSION}" "${KIWI_BASEDIR}"
# switch to temp dir
tmpdir=$(mktemp -d)
@ -92,7 +96,7 @@ if [ ! -x "$(kiwi_executable)" ]; then
if [ -x "$(kiwi_executable)" ]; then
# after version update: no need to install
echo "kiwi-config v${KIWI_VERSION} found!"
echo "v${KIWI_VERSION} already installed!"
else
# install archive
@ -106,15 +110,6 @@ if [ ! -x "$(kiwi_executable)" ]; then
rm -rf "${tmpdir}"
fi
# check virtualenv
cd "$(kiwi_installdir)" || :
if ! pipenv --venv &>/dev/null; then
# install virtualenv
echo -n "Preparing virtualenv ... "
pipenv sync &>/dev/null
echo "OK"
fi
# go back to the original work directory
cd "${WORKDIR}" || :
@ -123,10 +118,19 @@ KIWI_ROOT="$(kiwi_root)"
PIPENV_VERBOSITY=-1
PIPENV_PIPFILE="$(kiwi_installdir)/Pipfile"
export KIWI_ROOT
export KIWI_CONF_NAME
export KIWI_ROOT
export PIPENV_VERBOSITY
export PIPENV_PIPFILE
# check virtualenv
cd "$(kiwi_installdir)" || :
if ! pipenv --venv >/dev/null 2>/dev/null; then
# install virtualenv
printf "Preparing virtualenv ... "
pipenv sync >/dev/null 2>/dev/null
echo "OK"
fi
# run main script
exec pipenv run "$(kiwi_executable)" "${@}"