enonic / cli-enonic

GNU General Public License v3.0
41 stars 1 forks source link

Implement 'env' command that will generate a string with env variables for use in shell #130

Closed ComLock closed 5 years ago

ComLock commented 5 years ago

enonic project shell starts a new terminal, typically a completely other terminal than I use.

I would be nice to have a enonic project env which exports the required variables so one can run source enonic project env to change the current environment.

https://bash.cyberciti.biz/guide/Source_command

Other possible names:

ComLock commented 5 years ago

~/.bash_profile

export DOT_ENONIC="$HOME/.enonic"
export XP_VERSION='7.0.0'

export XP_INSTALL="$DOT_ENONIC/distributions/enonic-xp-mac-sdk-$XP_VERSION"
export JAVA_HOME="$XP_INSTALL/jdk"

export XP_SANDBOX='whatever'
export XP_HOME="$DOT_ENONIC/sandboxes/$XP_SANDBOX/home"
ComLock commented 5 years ago

It looks like you don't need to use the source command.

What you need is to return a string, which contains the bash commands that is needed to setup the environment. Then you have to eval that string in the current terminal.

ASFAIK this can be done in bash by using the dollar paranteses syntax:

$(echo "export XP_HOME='/tmp'"); echo $XP_HOME

https://stackoverflow.com/questions/39110485/difference-between-and-in-bash

ComLock commented 5 years ago

Here's how to set two environment variables without error messages:

$(echo "set ONE='1';set TWO='2'"); echo $ONE; echo $TWO

NOPE SORRY I NEED TO TEST MORE, IT DOESN'T WORK

ComLock commented 5 years ago

To check whether a variable is marked as exported in the current shell use

compgen -e -X '!ONE'
ComLock commented 5 years ago

So the final command would look like this:

$(enonic project env)
ComLock commented 5 years ago
eval $(echo "XP_HOME=/x;JAVA_HOME=/j;echo XP_HOME=\$XP_HOME; echo JAVA_HOME=\$JAVA_HOME")
ComLock commented 5 years ago

Backticks are shorter and perhaps supported by more terminals, but then again harder to write:

eval `echo "XP_HOME=/x;JAVA_HOME=/j;echo XP_HOME=$XP_HOME; echo JAVA_HOME=$JAVA_HOME"`
ComLock commented 5 years ago

You can also use redirection like this:

eval <<< echo 'XP_HOME=/x;JAVA_HOME=/j;echo XP_HOME=$XP_HOME; echo JAVA_HOME=$JAVA_HOME'
ComLock commented 5 years ago
eval export `echo 'XP_HOME=/x;JAVA_HOME=/j;echo XP_HOME=$XP_HOME; echo JAVA_HOME=$JAVA_HOME'`
ComLock commented 5 years ago

I think

eval <<< enonic project env

Is the best, because it's short and does not use backticks

ComLock commented 5 years ago

Another option, but with backticks

export `echo -e "XP_HOME=/x\nJAVA_HOME=/j"`
ComLock commented 5 years ago

List all exported vars:

export -p