Exec

The exec command allows you to run short pieces of code within the maze application scope. Code can be specified in the following three ways:

  1. Inline mode: inline code specified in the command line as a string argument. maze exec User.first

  2. Editor mode: a terminal-based code editor is opened and the resulting code is executed once you save and exit. maze exec

  3. File mode: Code within an existing .cr file is copied to a tmp file for editing and run once editor is closed. maze exec scripts/stuff.cr

Usage

Here is a list of the commands available:

maze exec [OPTIONS] [CODE | FILE_PATH]

Arguments:
  FILE_PATH  Path to a .cr file to run in the application scope (file mode)
  CODE       String containing valid crystal code to run in the application scope (inline mode)

  Note: specifying no argument activates editor mode

Options:
  -e, --editor       Prefered Editor: [vim, nano, pico, etc] (default: vim)
  -b, --back [n]     Load the nth previous script (only valid in editor mode)

Inline Mode

The exec command in inline mode allows you to execute a line of code (or several with semicolons) with your application code and environment loaded.

Inline mode can be helpful in executing one-liners on a remote production machine, or to quickly execute some code locally.

Examples:

Full Example:

In this example, an article was accidentally archived, and you want to recover it via command line: 1. Find the article and ensure it exists 1. maze exec 'Article.find(1)' 1. Set the article's archived flag to be false 1. $ maze exec 'a = Article.find(1); (a.archived = false) if a; a.save if a;'

The history of your code, and the resulting output is saved in a ./tmp directory:

Editor Mode

Editor mode is activated by entering maze exec with no arguments. By default, editor mode will open the Vim text editor (this can be overridden by specifying your preferred text editor as the -e option). Once you are done editing your code, you can quit without saving to abort, or you can save and quit, which will cause maze to run the code. Specifying a value for the --back option allows you to go n scripts back and edit+run that script.

Example

File Mode

File mode is invoked in a similar to inline mode and it is executed in editor mode (opening a terminal editor).

The invocation, just pass a path to a crystal file, rather than a string to execute - maze exec filename.cr

The argument must be a string ending in .cr, and must correspond with a file that exists on the file system, within the current project.

The file then opens in edit mode. The changes you make will not modify the original source file, but they will be used when maze exec runs the code and saved as a ./tmp/{timestamp}_console.cr file.

Example

Last updated