-
Notifications
You must be signed in to change notification settings - Fork 8
Scripts
You can run a series of commands in one go by putting them in a script. This can come in handy when you have to repeat a complex task many times or when you are automating a lenghty process like a MOLGENIS deployment.
A simple script consists of just a bunch of commands. For example:
add group research
add user henk
make henk research_MANAGER
--as-user henk import research_data --in research
set app title "Henks's research portal"
Scripts that are stored in your Commander's home folder (~/.mcmd/scripts/
) can
be run without specifying the path. Assuming the example
script is
stored there, it can be run as follows:
mcmd run example
Scripts that are not stored in the scripts
folder can be run with the
--from-path
argument:
mcmd run --from-path /path/to/your/script
If you've run some commands (one by one) and quickly want to run them again (for
example on another server), you can use the script
command to create a script
from history:
mcmd script
These scripts will automatically be stored in the Commander's scripts folder.
For more demanding use cases, the Commander scripts support additional features like templating and requesting user input at runtime. Here's the example script of before, but now made more reusable with templating. Continue reading below to learn more about all the features.
$value group
$value name
$value role = "manager"
$input pass password : "Please enter the password of {{name}}"
add group {{group}}
add user {{name}}
make henk {{group}}_{{role|upper}}
--as-user {{name}} import research_data --in {{group}}
set app title "{{name|capitalize}}'s research portal"
The Commander scripts use the Jinja2 templating language. You can use templates in commands, comments and text values. It is good to know that every line in the script is processed as a standalone template, so multi-line templates or templates with multiple lines of output are not supported (yet).
Please read the Jinja2 documentation to learn more about all the possibilities!
To assign a value, you can use the $value
declaration. The supported types
are text and boolean, and you can supply a default value.
# Not specifying a default value makes it a required argument
$value name
# Here a default value is declared, so it does not need to be supplied as an argument
$value surname = "Johnson"
# Both single quoted text and double quoted text are allowed
$value single = 'Single quoted text so we can use "double" quoted text inside'
$value double = "Double quoted text so we can use 'single' quoted text inside"
# Text values are processed as templates, so you can use other values in them
$value fullname = "{{name}} {{surname}}"
# Booleans are also supported (true/false)
$value active = true
To show input prompts while the script is running, use an $input
declaration.
You can ask the user to input text, passwords or yes/no. Like with values,
these inputs can then be used in templates.
# This will ask the user to enter a 'name'
$input text name
# You can add message to make the prompt more readable
$input text email : "Please enter the email of {{name}}"
# The 'pass' and 'bool' types will show a password and yes/no prompt respectively
$input pass password : "Please enter a password"
$input bool continue : "Do you want to continue?"
The $wait
function will wait until the user presses enter. It shows an
optional message, which you can use to give the user an instruction (for example
to do something the Commander can't).
$wait : "Go get a snack"
To pass arguments to a script, you run it with the --with-arguments
parameter.
You can override both $value
and $input
declarations this way.
The following script:
$value group
$input name
# Welcome to {{group|upper}}, {{name|capitalize}}!
when run with:
mcmd run example --with-arguments group=research name=henk
will print:
Welcome to group RESEARCH, Henk!
You can spread a long command over multiple lines by using the \
symbol at the
end of the line:
add user {{name}} \
--with-email {{email}} \
--set-password {{password}} \
--is-superuser