Skip to content

Commit

Permalink
Add create-bin-wrappers command
Browse files Browse the repository at this point in the history
  • Loading branch information
fsquillace committed Feb 13, 2022
1 parent 5630a0f commit 2b9f183
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Run JuNest installed programs directly from host OS
---------------------------------------

Installed programs can be accessible directly from host without
calling `junest` command.
entering directly into a JuNest session (no need to call `junest` command).
For instance, supposing the host OS is an Ubuntu distro you can directly
run `pacman` by simply updating the `PATH` variable:

Expand Down
24 changes: 24 additions & 0 deletions bin/junest
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ usage() {
echo -e " b[uild] Build a $NAME image (must run in ArchLinux)"
echo -e " -n, --disable-check Disable the $NAME image check"
echo
echo -e " create-bin-wrappers Create bin wrappers in $JUNEST_HOME/usr/bin_wrappers"
echo -e " -f, --force Replace the wrapper files even if they already exist"
echo
}

version() {
Expand All @@ -78,6 +81,7 @@ function parse_arguments(){
# Actions
ACT_SETUP=false
ACT_BUILD=false
ACT_CREATE_WRAPPERS=false
ACT_NAMESPACE=false
ACT_PROOT=false
ACT_GROOT=false
Expand All @@ -88,6 +92,7 @@ function parse_arguments(){
case "$1" in
s|setup) ACT_SETUP=true ; shift ;;
b|build) ACT_BUILD=true ; shift ;;
create-bin-wrappers) ACT_CREATE_WRAPPERS=true ; shift ;;
n|ns) ACT_NAMESPACE=true ; shift ;;
p|proot) ACT_PROOT=true ; shift ;;
g|groot) ACT_GROOT=true ; shift ;;
Expand All @@ -103,6 +108,9 @@ function parse_arguments(){
elif $ACT_BUILD
then
_parse_build_opts "$@"
elif $ACT_CREATE_WRAPPERS
then
_parse_create_wrappers_opts "$@"
elif $ACT_NAMESPACE
then
_parse_ns_opts "$@"
Expand Down Expand Up @@ -204,6 +212,17 @@ function _parse_build_opts() {
done
}

function _parse_create_wrappers_opts() {
OPT_FORCE=false
while [[ -n "$1" ]]
do
case "$1" in
-f|--force) OPT_FORCE=true ; shift ;;
*) die "Invalid option $1" ;;
esac
done
}

function _parse_setup_opts() {
OPT_FROM_FILE=false
IMAGE_FILE=""
Expand Down Expand Up @@ -256,6 +275,11 @@ function execute_operation() {
die "Error: The image is still not installed in $JUNEST_HOME. Run this first: $CMD setup"
fi

if $ACT_CREATE_WRAPPERS; then
create_wrappers $OPT_FORCE
exit
fi

local run_env
if $ACT_NAMESPACE; then
if $OPT_FAKEROOT; then
Expand Down
23 changes: 21 additions & 2 deletions lib/core/wrappers.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
#!/usr/bin/env bash
#
# Dependencies:
# None
#
# vim: ft=sh


#######################################
# Create bin wrappers
#
# Globals:
# JUNEST_HOME (RO) : The JuNest home directory.
# Arguments:
# force ($1?) : Create bin wrappers even if the bin file exists.
# Defaults to false.
# Returns:
# None
# Output:
# None
#######################################
function create_wrappers() {
local force=${1:-false}
mkdir -p "${JUNEST_HOME}/usr/bin_wrappers"

cd "${JUNEST_HOME}/usr/bin" || return 1
for file in *
do
[[ -x $file ]] || continue
if [[ -e ${JUNEST_HOME}/usr/bin_wrappers/$file ]]
if [[ -e ${JUNEST_HOME}/usr/bin_wrappers/$file ]] && ! $force
then
continue
fi
Expand Down
19 changes: 16 additions & 3 deletions tests/unit-tests/test-wrappers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,26 @@ function test_create_wrappers_already_exist(){
touch $JUNEST_HOME/usr/bin/myfile
chmod +x $JUNEST_HOME/usr/bin/myfile
mkdir -p $JUNEST_HOME/usr/bin_wrappers
touch $JUNEST_HOME/usr/bin_wrappers/myfile
echo "original" > $JUNEST_HOME/usr/bin_wrappers/myfile
chmod +x $JUNEST_HOME/usr/bin_wrappers/myfile
assertCommandSuccess create_wrappers
assertCommandSuccess create_wrappers false
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers should exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
assertTrue "myfile wrapper should exist" "[ -x $JUNEST_HOME/usr/bin_wrappers/myfile ]"
assertEquals "original" "$(cat $JUNEST_HOME/usr/bin_wrappers/myfile)"
}

function test_create_wrappers_forced_already_exist(){
echo "new" > $JUNEST_HOME/usr/bin/myfile
chmod +x $JUNEST_HOME/usr/bin/myfile
mkdir -p $JUNEST_HOME/usr/bin_wrappers
echo "original" > $JUNEST_HOME/usr/bin_wrappers/myfile
chmod +x $JUNEST_HOME/usr/bin_wrappers/myfile
assertCommandSuccess create_wrappers true
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers should exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
assertTrue "myfile wrapper should exist" "[ -x $JUNEST_HOME/usr/bin_wrappers/myfile ]"
assertEquals "" "$(touch $JUNEST_HOME/usr/bin_wrappers/myfile)"
assertNotEquals "original" "$(cat $JUNEST_HOME/usr/bin_wrappers/myfile)"
}

function test_create_wrappers_executable_no_longer_exist(){
Expand Down

0 comments on commit 2b9f183

Please sign in to comment.