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_ROOTor exit unless it points to an existing directory, - We'll hand off to
compaafter setting the global environment. If the user requested a completion (with the hidden__completecommand), the version flag, or the doctor/help docs server commands,milpaexits immediately afterexec compa $@. @milpa.load_utilis defined otherwise, and we'll immediately use it to load logging-related functions.@milpa.failis defined.- A couple of temporary pipes are created (
COMPA_OUTandCOMPA_ERR), and compa is started
compa resolves intentions
- After setting up logging,
compabuilds and processesMILPA_PATH(unlessMILPA_PATH_PARSEDis already set), then looks for commands atcommands/on every directory ofMILPA_PATHand builds a command tree; it can error out here if any command has an invalid spec (unless runningmilpa itself doctor). - A
spf13/cobra.Commandis created and the known command tree is mapped into child commands. cobratakes over, handling help, argument/flag parsing, and invoking validation.- Any errors are communicated back to milpa over the temporary pipes (
COMPA_OUTandCOMPA_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,milpawill print outcompa'sstdout, piping it throughless -FIRXif 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
compareturned 0,milpaevals the contents ofCOMPA_OUTto 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-runhooks 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 |