Skip to content

Commit

Permalink
feat: Add utility functions for shell operations (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsanima authored Feb 9, 2024
1 parent 5470427 commit 73751a8
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/mdsanima-shell/libutil.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2024 MDSANIMA DEV. All rights reserved.
# Licensed under the MIT license.

# This library is for utility functions.


# Cleaned line sequence
declare clean_line_seq="\r\e[0K"


#-------------------------------------------------------------------------------
# This function provides a simple way to execute bash commands with one line in
# the stdout progress by reading each line of the stdout and displaying it with
# overwriting the previous line.
#
# Arguments:
# <command> The command to be executed, required
#
# Usage:
# util::one_line_progress <command>
# util::one_line_progress sudo apt update
#-------------------------------------------------------------------------------
function util::one_line_progress() {
local command="$@"

# Execute the command and read each line of the stdout
${command} 2>&1 | while IFS= read -r line; do
echo -n -e "${clean_line_seq}${line}"
done
}


#-------------------------------------------------------------------------------
# This function provides a simple way to check if a package is installed.
#
# Arguments:
# <package> The package name to check if is installed, required
#
# Returns:
# 0 if the package is installed
# 1 if the package is not installed
#
# Usage:
# util::check_package_is_installed <package>
# util::check_package_is_installed git
#-------------------------------------------------------------------------------
function util::check_package_is_installed() {
local package="$1"
local query=$(dpkg-query -W -f='${Status}' "${package}" 2>/dev/null)

# Check if the package is installed
if echo "${query}" | grep -q "install ok installed"; then
return 0
else
return 1
fi
}

0 comments on commit 73751a8

Please sign in to comment.