|
| 1 | +# Potfile Scripting |
| 2 | + |
| 3 | +- Proposal: LE-002 |
| 4 | +- Status: **Draft** |
| 5 | +- Author: [**@vknabel**](https://github.com/vknabel) |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +This proposal adds some basic subcommands to the `lithia` CLI to allow user defined scripts to be executed. |
| 10 | + |
| 11 | +## Motivation |
| 12 | + |
| 13 | +Every project has specific jobs to be done. These jobs are often repetitive and can be automated. This proposal aims to provide simple access to these scripts. |
| 14 | + |
| 15 | +## Proposed Solution |
| 16 | + |
| 17 | +Within the `Potfile`, new commands that execute a specific Lithia file as a script can be defined using the `pots.cmds.script` function. Additional help context can be provided on the existing command. The command can be executed using `lithia [script]` or `lithia run [script]`. |
| 18 | + |
| 19 | +```lithia |
| 20 | +let testCmd = cmds.script "test", "cmd/test.lithia" |
| 21 | +testCmd.summary "runs all tests" |
| 22 | +testCmd.flag "verbose", "verbose logging" |
| 23 | +testCmd.env "LITHIA_TESTS", "1" |
| 24 | +``` |
| 25 | + |
| 26 | +Similar, the `pots.cmds.bin` function can be used to define a command that executes a binary. Under the hood `/usr/bin/env` will be used to lookup the binary. In case of `lithia` itself, the currently executed `lithia` binary will be used. |
| 27 | + |
| 28 | +```lithia |
| 29 | +cmds.bin "example", ["lithia", "test"] |
| 30 | +``` |
| 31 | + |
| 32 | +## Detailed Design |
| 33 | + |
| 34 | +The user adds the commands of their choice to their `Potfile`. |
| 35 | +The Lithia interpreter will now execute it and will proceed working on the internal store structure. |
| 36 | + |
| 37 | +The execution of the Potfile takes place in a sandboxed environment which whitelists possible external module imports to avoid direct access to the file system and other resources. It will also be time-restricted to at most one second using Go `context.Context`. |
| 38 | + |
| 39 | +To avoid privilege escalation, scripts declared in the `Potfile` with overridden `LITHIA_TIMEOUT` or `LITHIA_EXTERNAL_DEFINITIONS`, can only restrict the environment further. They cannot allow more access to the environment. |
| 40 | + |
| 41 | +## Changes to the Standard Library |
| 42 | + |
| 43 | +Adds a new module `pot.cmds`: |
| 44 | + |
| 45 | +- _func_ `script alias, path` - returns a `MutableCommand`. |
| 46 | +- _func_ `bin alias, path` - returns a `MutableCommand`. |
| 47 | +- _data_ `MutableCommand` - a mutable command that can be used to configure the command. |
| 48 | + - _func_ `summary text` - sets the summary text for the command. |
| 49 | + - _func_ `flag name, description` - adds a flag to the command. |
| 50 | + - _func_ `env name, value` - adds an environment variable to the command. |
| 51 | + |
| 52 | +Other non-final additions are internal and include: |
| 53 | + |
| 54 | +- _let_ `store` - a stateful application store which stores the commands and in the future more commands. |
| 55 | + |
| 56 | +## Alternatives Considered |
| 57 | + |
| 58 | +- `pot [script]`, `lithia run [script]` or `lithia pot [script]` |
| 59 | + |
| 60 | +## Acknowledgements |
| 61 | + |
| 62 | +`npm run [script]`, `yarn [script]`, `swift run [target]` and |
| 63 | +`archery [script]`. |
0 commit comments