kiss: prettier output

This commit is contained in:
Dylan Araps 2019-09-21 20:22:56 +03:00
parent 56e84cac42
commit 6ad2c6e451

134
kiss
View File

@ -12,15 +12,16 @@
#
# Dylan Araps.
die() {
# Print a message and exit with '1' (error).
printf '\033[1;31m!>\033[m %s.\n' "$@" >&2
exit 1
}
log() {
# Print a message prettily.
printf '\033[1;32m->\033[m %s.\n' "$@"
printf '\033[1;32m%s \033[m%s\033[m %s\n' \
"${3:-->}" "${2:+}$1${2:+}" "$2"
}
die() {
# Print a message and exit with '1' (error).
log "$1" "$2" "!>" >&2
exit 1
}
contains() {
@ -31,14 +32,14 @@ contains() {
pkg_lint() {
# Check that each mandatory file in the package entry exists.
log "[$1] Checking repository files"
log "$1" "Checking repository files"
repo_dir=$(pkg_find "$1")
cd "$repo_dir" || die "'$repo_dir' not accessible"
[ -f sources ] || die "[$1] Sources file not found"
[ -x build ] || die "[$1] Build file not found or not executable"
[ -s version ] || die "[$1] Version file not found or empty"
[ -f sources ] || die "$1" "Sources file not found"
[ -x build ] || die "$1" "Build file not found or not executable"
[ -s version ] || die "$1" "Version file not found or empty"
read -r _ release < version
[ "$release" ] || die "Release field not found in version file"
@ -100,7 +101,7 @@ pkg_list() {
pkg_sources() {
# Download any remote package sources. The existence of local
# files is also checked.
log "[$1] Downloading sources"
log "$1" "Downloading sources"
# Store each downloaded source in a directory named after the
# package it belongs to. This avoid conflicts between two packages
@ -114,21 +115,21 @@ pkg_sources() {
# Remote source.
*://*)
[ -f "${src##*/}" ] && {
log "[$1] Found cached source '${src##*/}'"
log "$1" "Found cached source '${src##*/}'"
continue
}
wget "$src" || {
rm -f "${src##*/}"
die "[$1] Failed to download $src"
die "$1" "Failed to download $src"
}
;;
# Local source.
*)
[ -f "$repo_dir/$src" ] || die "[$1] No local file '$src'"
[ -f "$repo_dir/$src" ] || die "$1" "No local file '$src'"
log "[$1] Found local file '$src'"
log "$1" "Found local file '$src'"
;;
esac
done < "$repo_dir/sources"
@ -137,7 +138,7 @@ pkg_sources() {
pkg_extract() {
# Extract all source archives to the build directory and copy over
# any local repository files.
log "[$1] Extracting sources"
log "$1" "Extracting sources"
repo_dir=$(pkg_find "$1")
@ -150,7 +151,7 @@ pkg_extract() {
# allows for manual extraction.
*://*.tar*|*://*.tgz)
tar xf "$src_dir/$1/${src##*/}" --strip-components 1 \
|| die "[$1] Couldn't extract ${src##*/}"
|| die "$1" "Couldn't extract ${src##*/}"
;;
*)
@ -163,7 +164,7 @@ pkg_extract() {
cp -f "$src_dir/$1/${src##*/}" .
else
die "[$1] Local file $src not found"
die "$1" "Local file $src not found"
fi
;;
esac
@ -201,7 +202,7 @@ pkg_strip() {
# Package has stripping disabled, stop here.
[ -f "$(pkg_find "$1")/nostrip" ] && return
log "[$1] Stripping binaries and libraries"
log "$1" "Stripping binaries and libraries"
# Strip only files matching the below ELF types.
find "$pkg_dir/$1" -type f | while read -r file; do
@ -223,7 +224,7 @@ pkg_fixdeps() {
# each binary and library with 'ldd'. This catches any extra
# libraries and or dependencies pulled in by the package's
# build suite.
log "[$1] Checking for missing dependencies"
log "$1" "Checking for missing dependencies"
# Go to the directory containing the built package to
# simplify path building.
@ -284,7 +285,7 @@ pkg_manifest() (
# Generate the package's manifest file. This is a list of each file
# and directory inside the package. The file is used when uninstalling
# packages, checking for package conflicts and for general debugging.
log "[$1] Generating manifest"
log "$1" "Generating manifest"
# This funcion runs as a sub-shell to avoid having to 'cd' back to the
# prior directory before being able to continue.
@ -301,16 +302,16 @@ pkg_manifest() (
pkg_tar() {
# Create a tar-ball from the built package's files.
# This tar-ball also contains the package's database entry.
log "[$1] Creating tar-ball"
log "$1" "Creating tar-ball"
# Read the version information to name the package.
read -r version release < "$(pkg_find "$1")/version"
# Create a tar-ball from the contents of the built package.
tar zpcf "$bin_dir/$1#$version-$release.tar.gz" -C "$pkg_dir/$1" . ||
die "[$1] Failed to create tar-ball"
die "$1" "Failed to create tar-ball"
log "[$1] Successfully created tar-ball"
log "$1" "Successfully created tar-ball"
}
pkg_build() {
@ -375,7 +376,7 @@ pkg_build() {
# This calls 'args' to inherit a root check and call
# to 'sudo' to elevate permissions.
[ -f "$bin_dir/$pkg#$version-$release.tar.gz" ] && {
log "[$pkg] Found pre-built binary, installing"
log "$pkg" "Found pre-built binary, installing"
(KISS_FORCE=1 args i "$bin_dir/$pkg#$version-$release.tar.gz")
# Remove the now installed package from the build
@ -391,7 +392,7 @@ pkg_build() {
for pkg; do
# Ensure that checksums exist prior to building the package.
[ -f "$(pkg_find "$pkg")/checksums" ] || {
log "[$pkg] Checksums are missing"
log "$pkg" "Checksums are missing"
# Instead of dying above, log it to the terminal. Also define a
# variable so we *can* die after all checksum files have been
@ -410,7 +411,7 @@ pkg_build() {
# set.
for pkg; do
pkg_checksums "$pkg" | cmp -s - "$(pkg_find "$pkg")/checksums" || {
log "[$pkg] Checksum mismatch"
log "$pkg" "Checksum mismatch"
# Instead of dying above, log it to the terminal. Also define a
# variable so we *can* die after all checksum files have been
@ -436,13 +437,13 @@ pkg_build() {
cd "$mak_dir/$pkg"
# Call the build script.
"$repo_dir/build" "$pkg_dir/$pkg" || die "[$pkg] Build failed"
"$repo_dir/build" "$pkg_dir/$pkg" || die "$pkg" "Build failed"
# Copy the repository files to the package directory.
# This acts as the database entry.
cp -Rf "$repo_dir" "$pkg_dir/$pkg/$pkg_db/"
log "[$pkg] Successfully built package"
log "$pkg" "Successfully built package"
# Create the manifest file early and make it empty.
# This ensure that the manifest is added to the manifest...
@ -457,7 +458,7 @@ pkg_build() {
# Skip this check if this is a package update.
contains "$explicit" "$pkg" && [ -z "$pkg_update" ] && continue
log "[$pkg] Needed as a dependency or has an update, installing"
log "$pkg" "Needed as a dependency or has an update, installing"
(KISS_FORCE=1 args i "$pkg")
done
@ -473,8 +474,8 @@ pkg_build() {
# Only ask for confirmation if more than one package needs to be installed.
[ $# -gt 1 ] && {
log "Install built packages? [$*]" \
"Press Enter to continue or Ctrl+C to abort here"
log "Install built packages? [$*]"
log "Press Enter to continue or Ctrl+C to abort here"
# POSIX 'read' has none of the "nice" options like '-n', '-p'
# etc etc. This is the most basic usage of 'read'.
@ -504,19 +505,19 @@ pkg_checksums() {
# Die here if source for some reason, doesn't exist.
else
die "[$1] Couldn't find source '$src'"
die "$1" "Couldn't find source '$src'"
fi
# An easy way to get 'sha256sum' to print with the 'basename'
# of files is to 'cd' to the file's directory beforehand.
(cd "$src_path" && sha256sum "${src##*/}") ||
die "[$1] Failed to generate checksums"
die "$1" "Failed to generate checksums"
done < "$repo_dir/sources"
}
pkg_conflicts() {
# Check to see if a package conflicts with another.
log "[$2] Checking for package conflicts"
log "$2" "Checking for package conflicts"
set +ef
@ -547,7 +548,7 @@ pkg_remove() {
# The package is not installed, don't do anything.
pkg_list "$1" >/dev/null || {
log "[$1] Not installed"
log "$1" "Not installed"
return
}
@ -565,9 +566,7 @@ pkg_remove() {
# Disable globbing.
set -f
[ "$required_by" ] &&
die "[$1] Package is required by ${required_by%, }" \
"[$1] Aborting here..."
[ "$required_by" ] && die "$1" "Package is required by ${required_by%, }"
# Block being able to abort the script with 'Ctrl+C' during removal.
# Removes all risk of the user aborting a package removal leaving
@ -590,7 +589,7 @@ pkg_remove() {
# we no longer need to block 'Ctrl+C'.
trap pkg_clean EXIT INT
log "[$1] Removed successfully"
log "$1" "Removed successfully"
}
pkg_install() {
@ -609,8 +608,7 @@ pkg_install() {
tar_name=$1\#$version-$release.tar.gz
[ -f "$bin_dir/$tar_name" ] ||
die "Package '$1' has not been built" \
"Run 'kiss build $1'"
die "Package '$1' has not been built, run 'kiss build $1'"
tar_file=$bin_dir/$tar_name
fi
@ -630,9 +628,9 @@ pkg_install() {
# Extract the tar-ball to catch any errors before installation begins.
tar pxf "$tar_file" -C "$tar_dir/$pkg_name" ||
die "[$pkg_name] Failed to extract tar-ball"
die "$pkg_name" "Failed to extract tar-ball"
log "[$pkg_name] Checking that all dependencies are installed"
log "$pkg_name" "Checking that all dependencies are installed"
# Make sure that all run-time dependencies are installed prior to
# installing the package.
@ -644,9 +642,9 @@ pkg_install() {
install_dep="$install_dep'$dep', "
done < "$tar_dir/$pkg_name/$pkg_db/$pkg_name/depends"
[ "$install_dep" ] && die "[$1] Package requires ${install_dep%, }"
[ "$install_dep" ] && die "$1" "Package requires ${install_dep%, }"
log "[$pkg_name] Installing package incrementally"
log "$pkg_name" "Installing package incrementally"
# Block being able to abort the script with Ctrl+C during installation.
# Removes all risk of the user aborting a package installation leaving
@ -713,11 +711,11 @@ pkg_install() {
trap pkg_clean EXIT INT
if [ -x "$sys_db/$pkg_name/post-install" ]; then
log "[$pkg_name] Running post-install script"
log "$pkg_name" "Running post-install script"
"$sys_db/$pkg_name/post-install" ||:
fi
log "[$pkg_name] Installed successfully"
log "$pkg_name" "Installed successfully"
}
pkg_updates() {
@ -741,24 +739,24 @@ pkg_updates() {
cd "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null ||:
[ -d .git ] || {
log "[$repo] Not a git repository, skipping"
log "$repo" "Not a git repository, skipping"
continue
}
[ "$(git remote 2>/dev/null)" ] || {
log "[$repo] No remote, skipping"
log "$repo" "No remote, skipping"
continue
}
contains "$repos" "$PWD" || {
repos="$repos $PWD "
log "[$PWD] Updating repository"
log "$PWD" "Updating repository"
if [ -w "$PWD" ]; then
git pull
else
log "[$PWD] Need root to update"
log "$PWD" "Need root to update"
sudo git pull
fi
}
@ -786,9 +784,9 @@ pkg_updates() {
# If the package manager has an update, handle it first.
contains "$outdated" kiss && {
log "Detected package manager update" \
"The package manager will be updated first" \
"Continue?: Press Enter to continue or Ctrl+C to abort here"
log "Detected package manager update"
log "The package manager will be updated first"
log "Continue?: Press Enter to continue or Ctrl+C to abort here"
# POSIX 'read' has none of the "nice" options like '-n', '-p'
# etc etc. This is the most basic usage of 'read'.
@ -798,8 +796,8 @@ pkg_updates() {
pkg_build kiss
args i kiss
log "Updated the package manager" \
"Re-run 'kiss update' to update your system"
log "Updated the package manager"
log "Re-run 'kiss update' to update your system"
exit 0
}
@ -892,7 +890,7 @@ args() {
for pkg; do
pkg_checksums "$pkg" > "$(pkg_find "$pkg")/checksums"
log "[$pkg] Generated checksums"
log "$pkg" "Generated checksums"
done
;;
@ -927,7 +925,7 @@ args() {
for pkg in $remove_pkgs; do
pkg_list "$pkg" >/dev/null ||
die "[$pkg] Not installed"
die "$pkg" "Not installed"
pkg_remove "$pkg" "${KISS_FORCE:-check}"
done
@ -950,14 +948,14 @@ args() {
;;
h|help|-h|--help|'')
log 'kiss [b|c|i|l|r|s|u] [pkg] [pkg] [pkg]' \
'build: Build a package' \
'checksum: Generate checksums' \
'install: Install a package' \
'list: List installed packages' \
'remove: Remove a package' \
'search: Search for a package' \
'update: Check for updates'
log 'kiss [b|c|i|l|r|s|u] [pkg] [pkg] [pkg]'
log 'build: Build a package'
log 'checksum: Generate checksums'
log 'install: Install a package'
log 'list: List installed packages'
log 'remove: Remove a package'
log 'search: Search for a package'
log 'update: Check for updates'
;;
*) die "'kiss $action' is not a valid command" ;;