tmuxp
tmuxp copied to clipboard
Implements `if` conditions for pane and window
Closes #741
This implements both shell and python conditions.
-
ifcan be a:- string or, (short-hand for
shellkey) - dict that contains
pythonorshellkey.
- string or, (short-hand for
-
A
pythonkey will be evaluated as a python expression, and will test for its pythonic truthfulness -
A
shellkey will test as a shell variable and any of the following will be evaluated to true:("y", "yes", "1", "on", "true", "t")(case-insensitivity)
session_name: if conditions test
environment:
Foo: 'false'
show_htop: 'true'
windows:
# the following would not shows up as it evaluates to false
- window_name: window 1 ${ha} $Foo
if:
shell: ${Foo}
panes:
- shell_command:
- echo "this shouldn't shows up"
- echo neither should this $Foo
- window_name: window 2
panes:
# should not shows up
- if:
python: 1+1==3
shell_command:
- echo the above is a false statement
# no if conditions
- shell_command:
- echo no condition
- python -m http.server
# display by default, but can be disabled by running `show_htop=false tmuxp load .....`
- if: ${show_htop}
shell_command:
- echo the above is a true statement (by default), but can be disabled on-demand
- htop
In the example, by default, show_htop=true, but since it's a shell variable, it can be override by user. Hence, use can on-deamnd customise their pane/window configuration by
tmuxp load examples/if-conditions.yaml
and
show_htop=false tmuxp load examples/if-conditions.yaml
which will have different behaviour, for different use-cases
I've expanded the approach, where shell now refers to actual shell expressions, for expressions like:
[ 5 -gt 1 ]
or
echo ${MY_ENV} | grep foo
The shell_var now refers to just testing for variables.
python now supports statements (using eval only supports expression, so 1+2>=3 works but not import sys; 1+2>=3). Instead, We can use exec and extract the results (from the last expression) afterwards.
Yes I also partially agree that the use of eval and supprocess tends to be discourage (especially when we are executing unsanitised user input); therefore I did put comments on stating the potential danger within this approach in previous revision.
However, the more I think about it, the more I reckon that the nature of tmuxp or other similar tools like tmuxinator might render these concerns a bit pointless. The fact is, tmuxp will already be executing arbitrary commands based on the given list of shell_command.
From my point of view,
- The use of
subprocessto evaluate shell output shouldn't be of concern, as it's not too different than whattmuxpis designed to do (execute arbitrary commands to setup workspace). - My only potential concern is using
eval/execfor python statements, for the fact that they are executing inside the python interpreter, and they might change the internal state insidetmuxp(e.g. they can modify some variables withintmuxp).- Perhaps this concern can be eliminated by evaluating the python statement inside a subprocess as well (to isolate the effect)? We can probably re-export the environment variable to the subprocess Python to achieve the same effect.
RE:
Can you think of any other software projects that have declarative configurations directly execute code? 1
Aside from the examples that you have given (many of which just execute any commands given without any security measure), my experience with direnv is that it requires user to explicitly opt-in when encountering a new .envrc file.
- However, the main difference is that
direnvauto-load files whencd-ing, so it provides user a chance to manually review the file before auto-loading (it hashes the file to monitor changes to.envrcfile), so even if they had just cloned some repository onlinedirenvwould prompt user before auto-loading it. - In
tmuxp's case, users manually issues aloadcommand, so they should be in-charge of reviewing the file (if it comes from internet; the same pretty goes for any scripts that comes from the internet)?