milpa help docs milpa internals
milpa
is a bash script in charge of setting environment variables and handing off to the user-requested script. Most of the heavy lifting, including argument/option parsing and validation, as well as finding commands, is done by a companion binary named compa
(a slang term for friend in spanish).
milpa
is built with, and thanks to:
How it works
milpa
sets the stage
- As it starts running, milpa will set
MILPA_ROOT
or exit unless it points to an existing directory, - We'll hand off to
compa
after setting the global environment. If the user requested a completion (with the hidden__complete
command), the version flag, or the doctor/help docs server commands,milpa
exits immediately afterexec compa $@
. @milpa.load_util
is defined otherwise, and we'll immediately use it to load logging-related functions.@milpa.fail
is defined.- A couple of temporary pipes are created (
COMPA_OUT
andCOMPA_ERR
), and compa is started
compa
resolves intentions
- After setting up logging,
compa
builds and processesMILPA_PATH
(unlessMILPA_PATH_PARSED
is already set), then looks for commands atcommands/
on every directory ofMILPA_PATH
and builds a command tree; it can error out here if any command has an invalid spec (unless runningmilpa itself doctor
). - A
spf13/cobra.Command
is created and the known command tree is mapped into child commands. cobra
takes over, handling help, argument/flag parsing, and invoking validation.- Any errors are communicated back to milpa over the temporary pipes (
COMPA_OUT
andCOMPA_ERR
). - If a command is found, the user provided parseable arguments and options (and these are valid), the command environment is printed out to
COMPA_OUT
.
milpa
acts on the resolved intention
- If a non-zero exit code is returned by
compa
,milpa
will print outcompa
'sstdout
, piping it throughless -FIRX
if help or docs are being rendered, and exit with status code 0. Otherwise, we'll print out both pipes before removing them and exiting withcompa
's original exit code. - If
compa
returned 0,milpa
evals the contents ofCOMPA_OUT
to set the found sub-command's environment. If an incomplete environment is found, we exit with status code 2, print debugging information and cleanup temporary pipes. - a version check is performed to nag the user to update to the latest available version
- if requested, debug information of this session is printed to stderr, and temporary pipes are removed.
before-run
hooks are ran before finally invoking your script.
Exit codes
Mostly based on Bash's Appendix E and FreeBSD's sysexits
code | reason |
---|---|
2 |
@milpa.fail was called |
42 |
compa is requesting pretty printing and a clean milpa exit |
64 |
arguments/flags could not be parsed or failed validation |
70 |
a spec could not be parsed or help failed rendering |
78 |
MILPA_ROOT points to something that's not a directory, or MILPA_PATH has an incorrect path set |
127 |
sub-command not found |