Skip to content

Commit

Permalink
feat: improve shell compatibility and error handling
Browse files Browse the repository at this point in the history
* Enhance hostname detection and fallbacks
  - Add multiple hostname detection methods
  - Improve fallback chain with /etc/hostname support
  - Add nautilus as final fallback hostname
  - Fix hostname handling for various Unix systems

* Improve color and logging functionality
  - Add color deinitialization support
  - Add logging trap uninstallation
  - Fix color handling for dumb terminals
  - Make NO_COLOR handling more consistent

* Enhance technical flags and options
  - Rename --clear-flags to --mulle-clear-flags
  - Add --mulle-no-color and --mulle-no-colors flags
  - Add --mulle-no-error and --mulle-no-errors flags
  - Improve technical flags documentation

* Add filesystem improvements
  - Add timestamp_now function
  - Improve file_devicenumber for BSD systems
  - Add better logging for directory operations
  - Make directory listing more verbose in debug mode

* CMake improvements
  - Use CMAKE_CURRENT_SOURCE_DIR instead of PROJECT_SOURCE_DIR
  - Add explicit project version (6.4.0)
  - Add LANGUAGES NONE to project declaration
  • Loading branch information
mulle-nat committed Nov 19, 2024
1 parent e61077a commit f3a9fde
Show file tree
Hide file tree
Showing 18 changed files with 669 additions and 142 deletions.
50 changes: 25 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.13)

project( mulle-bashfunctions NONE)
project( mulle-bashfunctions VERSION 6.4.0 LANGUAGES NONE)


#
Expand Down Expand Up @@ -93,76 +93,76 @@ ${ALL_ADDITIONAL_SCRIPTS}
string( REPLACE ";" " " DEFAULT_EMBED_SCRIPTS_STR "${DEFAULT_EMBED_SCRIPTS}")

execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions-embed.sh\" ] || chmod a+w \"src/mulle-bashfunctions-embed.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/embed-header.sh ; grep -E -h -v \"^#|^\ *#\" ${DEFAULT_EMBED_SCRIPTS_STR}; cat src/embed-footer.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions-embed.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions-embed.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")


string( REPLACE ";" " " MINIMAL_EMBED_SCRIPTS_STR "${MINIMAL_EMBED_SCRIPTS}")


execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions-minimal-embed.sh\" ] || chmod a+w \"src/mulle-bashfunctions-minimal-embed.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/embed-minimal-header.sh ; grep -E -h -v \"^#|^\ *#\" ${MINIMAL_EMBED_SCRIPTS_STR}; cat src/embed-minimal-footer.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions-minimal-embed.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions-minimal-embed.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")


string( REPLACE ";" " " ALL_EMBED_SCRIPTS_STR "${ALL_EMBED_SCRIPTS}")

execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions-all-embed.sh\" ] || chmod a+w \"src/mulle-bashfunctions-all-embed.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/embed-all-header.sh ; grep -E -h -v \"^#|^\ *#\" ${ALL_EMBED_SCRIPTS_STR}; cat src/embed-all-footer.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions-all-embed.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions-all-embed.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")


# probably more of a fun thing:
#
# embed using eval `base64 <<< "<data>" | gzip -d -c`
# execute_process( COMMAND bash "-c" "gzip -c -9 \"src/mulle-bashfunctions-embed.sh\" | base64 \"src/mulle-bashfunctions-embed.sh.gz.base64\""
# WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
# WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
# INPUT_FILE "src/mulle-bashfunctions-embed.sh"
# OUTPUT_FILE "src/mulle-bashfunctions-embed.sh.gz.base64")

execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions-none.sh\" ] || chmod a+w \"src/mulle-bashfunctions-none.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/header.sh ; grep -E -h -v \"^#|^\ *#\" ${NONE_SCRIPTS_STR}"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions-none.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions-none.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions.sh\" ] || chmod a+w \"src/mulle-bashfunctions.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/header.sh ; grep -E -h -v \"^#|^\ *#\" ${DEFAULT_SCRIPTS_STR}"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions-minimal.sh\" ] || chmod a+w \"src/mulle-bashfunctions-minimal.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/header.sh ; grep -E -h -v \"^#|^\ *#\" ${MINIMAL_SCRIPTS_STR}"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions-minimal.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions-minimal.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

execute_process( COMMAND bash -c "[ ! -f \"src/mulle-bashfunctions-all.sh\" ] || chmod a+w \"src/mulle-bashfunctions-all.sh\""
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
execute_process( COMMAND bash -c "cat src/header.sh ; grep -E -h -v \"^#|^\ *#\" ${ALL_SCRIPTS_STR}"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_FILE "src/mulle-bashfunctions-all.sh")
execute_process( COMMAND chmod a-w "src/mulle-bashfunctions-all.sh"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")


set( SCRIPTS
Expand All @@ -184,8 +184,8 @@ file( GLOB LIBRARY_SCRIPTS
#
# Install
#
execute_process( COMMAND "${PROJECT_SOURCE_DIR}/mulle-bashfunctions" version
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
execute_process( COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/mulle-bashfunctions" version
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE MULLE_BASHFUNCTIONS_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
message( STATUS "MULLE_BASHFUNCTIONS_VERSION is ${MULLE_BASHFUNCTIONS_VERSION} according to mulle-bashfunctions")
Expand Down
4 changes: 2 additions & 2 deletions CMakePackage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
set( CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set( CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set( CPACK_PACKAGE_CONTACT "Nat! <[email protected]>")
set( CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
set( CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "🥊 A versioned collection of bash functions")
set( CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
set( CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set( CPACK_STRIP_FILES false)

# stuff needed for Debian
Expand Down
34 changes: 34 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
## 6.4.0

feat: improve shell compatibility and error handling

* Enhance hostname detection and fallbacks
- Add multiple hostname detection methods
- Improve fallback chain with /etc/hostname support
- Add nautilus as final fallback hostname
- Fix hostname handling for various Unix systems

* Improve color and logging functionality
- Add color deinitialization support
- Add logging trap uninstallation
- Fix color handling for dumb terminals
- Make `NO_COLOR` handling more consistent

* Enhance technical flags and options
- Rename --clear-flags to --mulle-clear-flags
- Add --mulle-no-color and --mulle-no-colors flags
- Add --mulle-no-error and --mulle-no-errors flags
- Improve technical flags documentation

* Add filesystem improvements
- Add `timestamp_now` function
- Improve `file_devicenumber` for BSD systems
- Add better logging for directory operations
- Make directory listing more verbose in debug mode

* CMake improvements
- Use `CMAKE_CURRENT_SOURCE_DIR` instead of `PROJECT_SOURCE_DIR`
- Add explicit project version (6.4.0)
- Add LANGUAGES NONE to project declaration


## 6.3.0

* added new command to keep mulle-sde out of the README picture
Expand Down
10 changes: 6 additions & 4 deletions mulle-bash
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ EOF
#
# bash/zsh will use arg after -c <arg> as $0, convenient!
#
PATH="/bin:/usr/bin:${PATH}" exec "${exe_shell}" -c ". ${script} --no-auto-shell ${args}" "${script}"
# MEMO: this used to be PATH="/bin:/usr/bin:${PATH}" but i can't
# remember why, and it also tripped up our path search
PATH="${PATH:-/bin:/usr/bin}" exec "${exe_shell}" -c ". ${script} --no-auto-shell ${args}" "${script}"
fi
else
no_auto_shell='YES'
Expand All @@ -149,7 +151,7 @@ fi
# this is "our" version
# the actual loaded version may differ (and will change this variable)
#
MULLE_BASHFUNCTIONS_VERSION="6.3.0"
MULLE_BASHFUNCTIONS_VERSION="6.4.0"
MULLE_BASHFUNCTIONS_LIBEXEC_DIRNAME="libexec"
MULLE_EXECUTABLE="$1"

Expand Down Expand Up @@ -771,7 +773,7 @@ then
fi


# get rid of all these functions, so mulle-bashfunctions can silenty load
# get rid of all these functions, so mulle-bashfunctions can silently load
# better versions

unset -f r_add_line
Expand All @@ -794,7 +796,7 @@ unset -f versions_sort
# ***

#
# Conondrum : if we turn off posix the :: separators don't work anymore
# Conundrum : if we turn off posix the :: separators don't work anymore
# on older : if we turn posix on the alias expansion doesn't work anymore
# bash :
#
Expand Down
70 changes: 44 additions & 26 deletions mulle-bashfunctions
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Examples:
Commands:
apropos <f> : search for a function by keywords
embed : embed mulle-bashfunctions into a script
common-unames : list of known values for uname
env : print environment needed for "mulle-bashfunctions.sh"
eval <cmd> : evaluate cmd inside of mulle-bashfunctions
functions : list defined functions
Expand Down Expand Up @@ -430,7 +431,7 @@ ${prefix}usage()

cat <<EOF >&2
Usage:
x [flags]
${name} [flags]

##
## ADD YOUR USAGE DESCRIPTION HERE
Expand Down Expand Up @@ -1197,59 +1198,59 @@ main()
MULLE_EXECUTABLE_FAIL_PREFIX="${MULLE_EXECUTABLE_NAME} ${cmd}"

case "${cmd}" in
help)
'help')
usage
;;

apropos)
'apropos')
apropos_function "$@" || exit 1
;;

env)
'env')
echo "\
MULLE_BASHFUNCTIONS_LIBEXEC_DIR=\"${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}\"
MULLE_USERNAME=\"${MULLE_USERNAME}\"
MULLE_HOSTNAME=\"${MULLE_HOSTNAME}\"
MULLE_UNAME=\"${MULLE_UNAME}\""
;;

embed-boot)
'embed-boot')
mulle_boot_embed "$@"
;;

embed)
'embed')
mulle_boot_embed "$@" | mulle_bashfunctions_embed "$@"
;;

extract-boot)
'extract-boot')
mulle_boot_extract "$@"
;;

extract|unembed)
'extract'|'unembed')
mulle_boot_extract "$@" | mulle_bashfunctions_extract "$@"
;;

functions)
'functions')
list_functions "$@" || exit 1
;;

hostname)
'hostname')
printf "%s\n" "${MULLE_HOSTNAME}"
;;

init|script)
'init'|'script')
fail "Use mulle-sde add --extension \"mulle-nat/file.sh\" instead"
;;

libexec-dir)
'libexec-dir')
printf "%s\n" "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}"
;;

libraries)
'libraries')
list_libraries "$@" || exit 1
;;

load)
'load')
local format

[ $# -ne 0 ] && format="-$1"
Expand All @@ -1267,15 +1268,32 @@ export MULLE_UNAME ;
"
;;

man)
'common-unames')
cat <<EOF
android
darwin
dragonfly
freebsd
hpux
linux
mingw
msys
netbsd
openbsd
sunos
windows
EOF
;;

'man')
man_function "$@" || exit 1
;;

new|script)
'new')
new_function "$@" || exit 1
;;

ncores)
'ncores')
include "path"
include "file"
include "parallel"
Expand All @@ -1284,7 +1302,7 @@ export MULLE_UNAME ;
printf "%s\n" "${RVAL}"
;;

path)
'path')
local format

[ $# -ne 0 ] && format="-$1"
Expand All @@ -1293,7 +1311,7 @@ export MULLE_UNAME ;
;;

# useful for accessing a single function from the library
eval)
'eval')
include "path"
include "file"
include "case"
Expand All @@ -1307,7 +1325,7 @@ export MULLE_UNAME ;
;;

# useful for accessing a r function from the library
r-eval)
'r-eval')
include "path"
include "file"
include "case"
Expand All @@ -1321,31 +1339,31 @@ export MULLE_UNAME ;
return 0
;;

shell)
'shell')
printf "%s\n" `ps -h -o cmd -p $$ | awk '{ print $1 }'`
;;

uname)
'uname')
printf "%s\n" "${MULLE_UNAME}"
;;

username)
'username')
printf "%s\n" "${MULLE_USERNAME}"
;;

uuid)
'uuid')
include "path"
include "file"

r_uuidgen
printf "%s\n" "${RVAL}"
;;

version)
'version')
printf "%s\n" "${MULLE_BASHFUNCTIONS_VERSION}"
;;

versions)
'versions')
list_versions "$@" || exit 1
;;

Expand Down
Loading

0 comments on commit f3a9fde

Please sign in to comment.