From 8c334ae2a91edbb44b2c7b9a472dfe3fc4d73276 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Sat, 30 Dec 2017 22:53:25 +0000 Subject: Add --if-unset option to java-vm's set action --- NEWS | 1 + src/modules/java-vm.eselect.in | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 8e0d519..6895cbc 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ * Sort java-vm targets in version order rather than alphanumeric * Fix showing and highlighting current ecj + mvn targets (#585224) * Fix insufficient quoting in java-vm module + * Add --if-unset option to java-vm's set action 0.3.0: diff --git a/src/modules/java-vm.eselect.in b/src/modules/java-vm.eselect.in index b9043e6..b8654e3 100644 --- a/src/modules/java-vm.eselect.in +++ b/src/modules/java-vm.eselect.in @@ -103,20 +103,30 @@ describe_set() { echo "Set a new system or user vm" } +describe_set_options() { + echo "--if-unset : do not change if already set" +} + do_set() { local usage="Usage [user|system] [vm]" + local ifunset=0 + + if [[ ${1} == "--if-unset" ]]; then + ifunset=1 + shift + fi + if [[ ${#} != 2 ]]; then die -q ${usage} - elif [[ ${1} == "system" ]]; then if [[ -w ${VM_SYSTEM%/*} ]]; then - my_set "${VM_SYSTEM}" "${2}" + my_set "${VM_SYSTEM}" "${2}" ${ifunset} else die -q "Sorry, you don't have enough permission to set system" fi elif [[ ${1} == "user" ]]; then if [[ ${UID} != 0 ]]; then - my_set "${VM_USER}" "${2}" + my_set "${VM_USER}" "${2}" ${ifunset} else die -q "Sorry, you cannot set a user vm as root. Set the system vm instead" fi @@ -126,12 +136,14 @@ do_set() { } my_set() { - local target=${2} symlink=${1} + local symlink=${1} target=${2} ifunset=${3} if [[ -z ${target} ]] ; then die -q "You didn't tell me what to set the symlink to" elif [[ -L "${symlink}" ]] ; then - set_symlink "${target}" "${symlink}" || die -q "Couldn't set a new symlink" + if [[ ${ifunset} == 0 || ! -d ${symlink} ]]; then + set_symlink "${target}" "${symlink}" || die -q "Couldn't set a new symlink" + fi elif [[ -e ${symlink} ]] ; then die -q "Target file already exists and is not a symlink: ${symlink}" -- cgit v1.2.3-65-gdbad