|
| 1 | +# TIM CLI |
| 2 | + |
| 3 | +This folder contains the TIM command line interface (CLI). The CLI is used to create and manage the active TIM instance. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +TIM CLI requires Python 3.6 or higher. |
| 8 | +No further external dependencies are required to run the CLI, |
| 9 | +however installing and running TIM may require additional software. |
| 10 | +The CLI informs of any missing dependencies automatically. |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +The easiest way to use the CLI is to use the shell bootstrap script (`tim`) located in TIM's root directory. |
| 15 | + |
| 16 | +You can always pass `--help` to any of the commands to view the help page. For example: |
| 17 | + |
| 18 | +```bash |
| 19 | +# Assuming we're in the TIM root directory (the parent folder of this README) |
| 20 | +$ ./tim --help |
| 21 | +usage: tim [-h] [--verbose] {dc,js,npmi,restart,run,setup,test,up,dev,pg,tool} ... |
| 22 | + |
| 23 | +Manage the current TIM instance |
| 24 | + |
| 25 | +options: |
| 26 | + -h, --help show this help message and exit |
| 27 | + --verbose, -v Enable verbose logging |
| 28 | + |
| 29 | +commands: |
| 30 | + Available commands |
| 31 | + |
| 32 | + {dc,js,npmi,restart,run,setup,test,up,dev,pg,tool} |
| 33 | + Additional help |
| 34 | + dc Run a docker-compose command on TIM containers |
| 35 | + js Compile JavaScript files for production mode |
| 36 | + npmi Run `npm install` to install TIM dependencies. |
| 37 | + restart Restart all TIM containers |
| 38 | + run Run a command in a TIM container |
| 39 | + setup Set up the TIM instance |
| 40 | + test Run unit tests |
| 41 | + up (Re)create all containers and start TIM |
| 42 | + dev Commands for developing TIM locally |
| 43 | + pg Commands for managing the PostgreSQL database |
| 44 | + tool Various helper commands for managing and developing TIM |
| 45 | +``` |
| 46 | + |
| 47 | +Alternatively, you can invoke the CLI directly using Python: |
| 48 | + |
| 49 | +``` |
| 50 | +python3 -m cli.tim |
| 51 | +``` |
| 52 | + |
| 53 | +> **NOTE**: TIM CLI assumes that the current working directory is the TIM root directory. |
| 54 | +> Therefore, if you are running the CLI manually from a different directory, you must first change to the TIM root directory. |
| 55 | +> It is *recommended* to use the helper `tim` script as it automatically sets the current working directory correctly. |
| 56 | +
|
| 57 | + |
| 58 | +## Development |
| 59 | + |
| 60 | +TIM CLI is a collection of TIM scripts that are located in the [`commands? ](commands) directory. |
| 61 | + |
| 62 | +For example, invoking `tim dc` will simply run the [`commands/setup.py`](commands/setup.py) script. |
| 63 | + |
| 64 | +To add a new script, create a Python file in the `commands` directory. Boilerplate: |
| 65 | + |
| 66 | +```python |
| 67 | +from argparse import ArgumentParser |
| 68 | + |
| 69 | +from cli.util.logging import log_info |
| 70 | + |
| 71 | +info = {"help": "Prints a message to the console"} |
| 72 | + |
| 73 | +class Arguments: |
| 74 | + message: str |
| 75 | + |
| 76 | +def cmd(args: Arguments) -> None: |
| 77 | + log_info(args.message) |
| 78 | + |
| 79 | + |
| 80 | +def init(parser: ArgumentParser) -> None: |
| 81 | + parser.add_argument( |
| 82 | + "message", |
| 83 | + help="The message to print", |
| 84 | + ) |
| 85 | +``` |
| 86 | + |
| 87 | +Notes: |
| 88 | + |
| 89 | +* The contents of `info` dict will be passed to [`ArgumentParser.add_subparsers`](https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_subparsers). Use the variable to provide any help info. |
| 90 | +* `cmd` method *is required* for every command script. It will be called by argparse with the [`Namespace`](https://docs.python.org/3/library/argparse.html#argparse.Namespace) object as parameter. |
| 91 | +* `init` method *is optional* for every command script. It will be called with the [`ArgumentParser`](https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser) instance as an argument. Use it to specify the arguments and the behaviour. |
| 92 | +* `class Arguments` is not required, but it is useful for strong typing. |
| 93 | +* **Don't use Python 3.7+ features!** The CLI tool is targeted to run on Python 3.6. |
| 94 | + |
| 95 | +### Helper methods |
| 96 | + |
| 97 | +The TIM CLI provides some helpers to help with scripts: |
| 98 | + |
| 99 | +* `cli.util.proc.run_cmd` - Runs an external command |
| 100 | +* `cli.util.errors.CLIError` - Raising this error will automatically log the error message and exit with a non-zero error code |
| 101 | +* `cli.config.loader.get_config` - Get the current TIM configuration (`tim.conf`) |
| 102 | +* `cli.docker.run.run_compose` - Run a docker-compose command |
| 103 | +* `cli.docker.run.run_docker` - Run a docker command |
| 104 | +* `cli.npm.run.run_npm` - Run a npm command |
| 105 | +* `cli.util.logging.log_*'` - Logs messages with various levels to the console |
| 106 | + * Note: `log_debug` messages will be visible only if the user passes the `--verbose` flag to TIM CLI |
| 107 | + |
| 108 | +### Updating configuration file |
| 109 | + |
| 110 | +TIM CLI allows to automatically upgrade the configuration file via a migration logic. |
| 111 | + |
| 112 | +If you need to update the configuration file, do the following: |
| 113 | + |
| 114 | +1. Update default values in [`config/default_config.py`](config/default_config.py) if needed |
| 115 | +2. Increment `CURRENT_REVISION` in [`config/default_config.py`](config/default_config.py) |
| 116 | +3. Create a migration function in [`config/migrations.py`](config/migrations.py) (check `_migrate_variables` for example) |
| 117 | + * The function receives the old configuration as a parameter and modifies it |
| 118 | +4. Add the migration function to `MIGRATIONS` list in [`config/migrations.py`](config/migrations.py) |
0 commit comments