feat: add source bundle function which will convert any module_load command into the prepended source.

refactor: dirs module, cleanup
feat: added prepend_file function to file module
master
Kebler Network System Administrator 2022-02-26 17:19:37 -08:00
parent d0209fc742
commit a31e294a87
3 changed files with 95 additions and 36 deletions

49
modules/utility/bundle.sh Normal file
View File

@ -0,0 +1,49 @@
#!/bin/bash
# $1 file to bundle
# $2 bundled output file (optional)
# if no output file then the bundled source is returned
# it can be immediately invoked like this
# source <(bundle myscript.sh)
bundle () {
[[ ! -f $1 ]] && return 1
module_load file
if [[ ! $2 == "__recurse__" ]]; then
tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX )
chmod 600 "$tmp_file"
cp $1 $tmp_file
else
tmp_file=$1
fi
# echo current temp file: $tmp_file
modules=$(sed -n -e 's/^module_load //p' < $tmp_file)
# echo found: $modules
# return
if [[ $modules ]]; then
# echo Modules: $modules
sed -i '/module_load/d' $tmp_file
for module in $(printf '%s\n' "${modules[@]}"|tac);
do
# echo module: $module
# echo module path: $(module_find $module)
prepend_file $(module_find $module) $tmp_file
done
bundle $tmp_file __recurse__
fi
if [[ ! $2 == "__recurse__" ]]; then
if [[ $2 ]]; then
cp $tmp_file $2
echo $2
else
echo -e "$(cat $tmp_file)"
fi
rm -f $tmp_file;
return 0
fi
}
# # if script was executed then call the function
(return 0 2>/dev/null) || bundle $@

View File

@ -1,35 +1,4 @@
# find /path/to/base/dir -type d -exec chmod 755 {} +
# To recursively give files read privileges:
# find /path/to/base/dir -type f -exec chmod 644 {} +
# Or, if there are many objects to process:
# chmod 755 $(find /path/to/base/dir -type d)
# chmod 644 $(find /path/to/base/dir -type f)
# Or, to reduce chmod spawning:
# find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
# find /path/to/base/dir -type f -print0 | xargs -0 chmod 644
# chmodr.sh
#
# author: Francis Byrne
# date: 2011/02/12
#
# Generic Script for recursively setting permissions for directories and files
# to defined or default permissions using chmod.
#
# Takes a path to recurse through and options for specifying directory and/or
# file permissions.
# Outputs a list of affected directories and files.
#
# If no options are specified, it recursively resets all directory and file
# permissions to the default for most OSs (dirs: 755, files: 644).
# Usage message
################## BEGIN: MODULE dirs ###############
isDir() {
if [[ -d $1 ]]
@ -43,6 +12,16 @@ isDir() {
chmodr () {
# Generic Script for recursively setting permissions for directories and files
# to defined or default permissions using chmod.
#
# Takes a path to recurse through and options for specifying directory and/or
# file permissions.
# Outputs a list of affected directories and files.
#
# If no options are specified, it recursively resets all directory and file
# permissions to the default for most OSs (dirs: 755, files: 644).
usage()
{
echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS"
@ -95,4 +74,6 @@ if [ -n "$FILEPERMS" ] ; then
find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS
fi
}
}
################## END: MODULE dirs ###############

View File

@ -1,7 +1,8 @@
#!/bin/bash
# export BASH_DEBUG=true
module_load debug
# module_load debug
module_load iecho
isFile() {
if [[ -f $1 ]]
@ -14,11 +15,11 @@ isFile() {
}
build_file () {
[[ -f "$2" ]] || (echo "output file $2 does not exist";return 1)
[[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1)
if [[ -f "$1" ]]; then
echo -e "\n####### ADDED $1 ########" >> $2
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
else debug "no such file $1 to append"
else iecho "no such file $1 to append"
fi
}
@ -201,3 +202,31 @@ source_dir () {
done
}
prepend_file () {
# ----------------------------------------------------------------------------------------------------------------------
# usage prepend_file <somefile> <anotherfile>
# ----------------------------------------------------------------------------------------------------------------------
# Prepend the contents of <somefile> [$1], to <anotherfile> [$2], leaving the result in <anotherfile> [$2].
# insert a newline at the end of <somefile> [$1] if necessary
# ----------------------------------------------------------------------------------------------------------------------
# check # echo $1 $2
[[ -f $1 ]] || { iecho "no file $1 to prepend";return 1; }
[[ -f $2 ]] || { iecho "no file $2 to which to prepend $1";return 2; }
# init
tmp_fn=$( mktemp -t TEMP_FILE_prepend.XXXXXXXX )
chmod 600 "$tmp_fn"
\cp $1 $tmp_fn
sed -i '$a\' $tmp_fn
cat $2 >> $tmp_fn
\mv "$tmp_fn" "$2"
# cleanup
rm -f "$tmp_fn"
iecho "file $1 prepened to $2"
return 0
# [End]
}