{"title":"Exec","description":"","section":"cli","version":"v1.5","path":"cli/exec","canonical_url":"https://amberframework.org/docs/v1.5/cli/exec","markdown_url":"https://amberframework.org/docs/v1.5/cli/exec.md","inherited":true,"content_markdown":"# Exec\n\nThe `exec` command allows you to run short pieces of code within the amber application scope. Code can be specified in the following three ways:\n\n1. _Inline mode:_ inline code specified in the command line as a string argument. `amber exec User.first`\n2. _Editor mode:_ a terminal-based code editor is opened and the resulting code is executed once you save and exit. `amber exec`\n3. _File mode:_ Code within an existing .cr file is copied to a tmp file for editing and run once editor is closed. `amber exec scripts/stuff.cr`\n\n## Usage\n\nHere is a list of the commands available:\n\n```text\namber exec [OPTIONS] [CODE | FILE_PATH]\n\nArguments:\n  FILE_PATH  Path to a .cr file to run in the application scope (file mode)\n  CODE       String containing valid crystal code to run in the application scope (inline mode)\n\n  Note: specifying no argument activates editor mode\n\nOptions:\n  -e, --editor       Prefered Editor: [vim, nano, pico, etc] (default: vim)\n  -b, --back [n]     Load the nth previous script (only valid in editor mode)\n```\n\n## Inline Mode\n\nThe `exec` command in inline mode allows you to execute a line of code \\(or several with semicolons\\) with your application code and environment loaded.\n\nInline mode can be helpful in executing one-liners on a remote production machine, or to quickly execute some code locally.\n\n### Examples:\n\n```text\n# Wrapping command in single quotes\n$ amber exec 'User.find_by(:email, \"test@example.com\")'\n# Escaping quotes\n$ amber exec \"User.find_by(:email, \\\"test@example.com\\\")\"\n# Multiple lines of code (using semi-colons)\n$ amber exec 'u = User.find(1); u.email if u;'\n```\n\n### Full Example:\n\nIn 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. `amber exec 'Article.find(1)'` 1. Set the article's `archived` flag to be `false` 1. `$ amber exec 'a = Article.find(1); (a.archived = false) if a; a.save if a;'`\n\n```text\n# Be patient, it takes a few seconds - this is compiling your entire app\n$ amber exec 'Article.find(1)'\n#<Article:0x10c5f6700 @errors=[], @id=1, @title=\"Groundbreaking Ideas\", @body=\"Just kidding, yet another self-important article.\", @archived=true, @created_at=2017-11-14 14:13:39 UTC, @updated_at=2017-11-14 14:34:10 UTC>\n# Those if statements are important, if you have a nilable type (Article | Nil)\n$ amber exec 'a = Article.find(1); (a.archived = false) if a; a.save if a;'\ntrue\n# Notice the update `@archived=false`\n$ amber exec 'Article.find(1)'\n#<Article:0x10e640700 @errors=[], @id=1, @title=\"Groundbreaking Ideas\", @body=\"Just kidding, yet another self-important article.\", @archived=false, @created_at=2017-11-14 14:13:39 UTC, @updated_at=2017-11-14 14:35:06 UTC>\n```\n\nThe history of your code, and the resulting output is saved in a `./tmp` directory:\n\n```text\n$ ls -l\n1510698868787_console.cr\n1510698868787_console_result.log\n1510698901965_console.cr\n1510698901965_console_result.log\n1510698912435_console.cr\n1510698912435_console_result.log\n\n# Review the output of your actions\n$ cat ./tmp/*result.log\n#<Article:0x10c5f6700 @errors=[], @id=1, @title=\"Groundbreaking Ideas\", @body=\"Just kidding, yet another self-important article.\", @archived=true, @created_at=2017-11-14 14:13:39 UTC, @updated_at=2017-11-14 14:34:10 UTC>\ntrue\n#<Article:0x10e640700 @errors=[], @id=1, @title=\"Groundbreaking Ideas\", @body=\"Just kidding, yet another self-important article.\", @archived=false, @created_at=2017-11-14 14:13:39 UTC, @updated_at=2017-11-14 14:35:06 UTC>\n```\n\n## Editor Mode\n\nEditor mode is activated by entering `amber 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 amber to run the code. Specifying a value for the `--back` option allows you to go _n_ scripts back and edit+run that script.\n\n### Example\n\n```text\n$ amber exec -e nano -b 1\n(nano opens the previous script that was edited + run in editor mode)\n(output of the script is printed to the terminal once you save + quit)\n```\n\n## File Mode\n\nFile mode is invoked in a similar to inline mode and it is executed in editor mode \\(opening a terminal editor\\).\n\nThe invocation, just pass a path to a crystal file, rather than a string to execute - `amber exec filename.cr`\n\nThe argument must be a string ending in .cr, and must correspond with a file that exists on the file system, within the current project.\n\nThe file then opens in edit mode. The changes you make will **not** modify the original source file, but they will be used when `amber exec` runs the code and saved as a `./tmp/{timestamp}_console.cr` file.\n\n### Example\n\n```crystal\n# ./cool_stuff.cr\nstatement = \"cool stuff happened\"\nemoticon = \"^_^\"\nputs \"#{statement} #{emoticon}\"\n```\n\n```text\n$ amber exec cool_script.cr\n# vim or another editor opens\n# make edits if necessary\n# save the contents\ncool stuff happened ^_^\n```"}