|
#!/usr/bin/env fish |
|
|
|
# use_nbox_vars.fish |
|
# This accepts three arguments: |
|
# |
|
# 1. The name of a nanobox environment, e.g., `local` or `dry-run` |
|
# 2. The name of a command to run, e.g., `mysql` |
|
# 3. Arguments for the command, e.g., `--host='$DATA_MYSQL_HOST' ...` |
|
# |
|
# For example, to load a database dump into your dry-run environment: |
|
# |
|
# ./use_nbox_vars.fish dry-run mysql \ |
|
# --host='$DATA_MYSQL_HOST' \ |
|
# --user='$DATA_MYSQL_USER' \ |
|
# --password='$DATA_MYSQL_PASS' \ |
|
# --database=gonano \ |
|
# < ../mydbdump.sql |
|
# |
|
# Note that the arguments which use the nanobox environment variables are in |
|
# single quotes `''`; this keeps them from expanding until the command and its |
|
# arguments are eval'd. |
|
# |
|
# This can be made more useful if you create your own commands for common |
|
# tasks, e.g. a script called `load_dbdump`: |
|
# |
|
# #!/usr/bin/env fish |
|
# mysql --host='$DATA_MYSQL_HOST' --user='$DATA_MYSQL_USER' --password='$DATA_MYSQL_PASS' --database=gonano $argv |
|
# |
|
# Then it is easier to type and remember: |
|
# |
|
# ./use_nbox_vars.fish dry-run ./load_dbdump < mydbdump.sql |
|
|
|
|
|
# Currently this only uses `DATA_*` vars; it could be modified to use |
|
# all vars, or allow an option to be passed in to th sed pattern. |
|
function use_nbox_vars -a env \ |
|
-d "Set local environment variables for this command from nanobox environment variables." |
|
# Parse the output of `nanobox info <env>` and eval it to set environment variables. |
|
eval (nanobox info $env | sed -nE "s/[[:space:]]*(DATA_[^=[:space:]]+)[=[:space:]]+(.*)/set -gx \1 '\2';/p") |
|
end |
|
|
|
|
|
function main -a env -a cmd \ |
|
-d "Evaluate `cmd` and its arguments with the given nanobox `env`'s variables." |
|
set -l code "$cmd $argv[3..-1]" |
|
|
|
use_nbox_vars $env |
|
|
|
printf "=> %s\n" "Running with nanobox $env env vars..." "$code" (eval "echo $code") |
|
eval $code |
|
end |
|
|
|
|
|
# TODO: Wrap this call to main() in an if statement checking on the script name |
|
# to allow for sourcing this as well as calling it. That may feel more natural |
|
# as arguments would not need to be single quoted E.g.: |
|
# |
|
# source use_nbox_vars.fish |
|
# use_nbox_vars dry-run; echo $DATA_MYSQL_HOST |
|
|
|
main $argv |