Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Enable loading of multiple mocks
Browse files Browse the repository at this point in the history
Make running multiple mocks at once easier by allowing user to provide multiple paths to the `--data` flag. This is compatible with both managed processes and foreground processes.

- Remove deprecated flags from legacy export: `--all`, `--index`, `--name`
- Legacy export run all the mocks by default
- make `--data`, `--pname`, `--port` and `--hostname` flags multiple, in order to provide list of mocks
- Dockerize command now supports copying multiple mocks and exposing multiple ports
- Update server logs to show the mock name to be able to filter logs by mock
Closes #57
  • Loading branch information
Guillaume committed Feb 25, 2022
1 parent 9cee016 commit 86545ac
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 434 deletions.
33 changes: 14 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $ mockoon-cli start --data https://domain.com/your-environment-file.json

> **Use a legacy export file**
>
> While we recommend using the method above to launch your mocks with the CLI, you can still use Mockoon's [legacy export files](https://mockoon.com/docs/latest/mockoon-data-files/import-export-mockoon-format/) and the dedicated flags `--index`, `--name` or `--all`.
> While we recommend using the method above to launch your mocks with the CLI, you can still use Mockoon's [legacy export files](https://mockoon.com/docs/latest/mockoon-data-files/import-export-mockoon-format/).
### Use an OpenAPI specification file

Expand Down Expand Up @@ -111,9 +111,10 @@ Mockoon's CLI has been tested on Node.js versions 12, 14, 15 and 16.

### `mockoon-cli start`

Starts a mock API from a Mockoon's environment file.
Starts one (or more) mock API from Mockoon's environment file(s).

The process will be created by default with the name and port of the Mockoon's environment. You can override these values by using the `--port` and `--pname` flags.
`--data`, `--port`, `--pname` and `--hostname` flags support multiple entries to run multiple mock APIs at once (see examples below).

Using the `--daemon-off` flag will keep the CLI in the foreground. The mock API process will not be [managed by PM2](#pm2). When running as a blocking process, all the logs are sent to both stdout (console) and the [usual files](logs).

Expand All @@ -124,27 +125,22 @@ USAGE
$ mockoon-cli start
OPTIONS
-d, --data=data [required] Path or URL to your Mockoon file
-N, --pname=pname Override process name
-p, --port=port Override environment's port
-l, --hostname=0.0.0.0 Override default listening hostname (0.0.0.0)
-d, --data [required] Path(s) or URL(s) to your Mockoon file(s)
-N, --pname Override process(es) name(s)
-p, --port Override environment(s) port(s)
-l, --hostname=0.0.0.0 Override default listening hostname(s) (0.0.0.0)
-t, --log-transaction Log the full HTTP transaction (request and response)
-r, --repair If the data file seems too old, or an invalid Mockoon file, migrate/repair without prompting
-D, --daemon-off Keep the CLI in the foreground and do not manage the process with PM2
-a, --all [deprecated] Run all environments in the legacy export file
-i, --index=index [deprecated] Select by environment's index in the legacy export file
-n, --name=name [deprecated] Seelct by environment name in the legacy export file
-h, --help Show CLI help
EXAMPLES
$ mockoon-cli start --data ~/data.json
$ mockoon-cli start --data ~/data1.json ~/data2.json --port 3000 3001 --pname mock1 mock2
$ mockoon-cli start --data https://file-server/data.json
$ mockoon-cli start --data ~/data.json --pname "proc1"
$ mockoon-cli start --data ~/data.json --daemon-off
$ mockoon-cli start --data ~/data.json --log-transaction
$ mockoon-cli start --data ~/data.json --all
$ mockoon-cli start --data ~/data.json --name "Mock environment"
$ mockoon-cli start --data ~/data.json --index 0
```

### `mockoon-cli list [ID]`
Expand Down Expand Up @@ -194,10 +190,10 @@ EXAMPLE

### `mockoon-cli dockerize`

Generates a Dockerfile used to build a self-contained image of a mock API. After building the image, no additional parameters will be needed when running the container.
Generates a Dockerfile used to build a self-contained image of one or more mock API. After building the image, no additional parameters will be needed when running the container.
This command takes similar flags as the [`start` command](#mockoon-start).

Please note that this command will extract your Mockoon environment from the file you provide and put it side by side with the generated Dockerfile. Both files are required in order to build the image.
Please note that this command will copy your Mockoon environment from the file you provide and put it side by side with the generated Dockerfile. Both files are required in order to build the image.

For more information on how to build the image: [Using the dockerize command](#using-the-dockerize-command)

Expand All @@ -206,17 +202,16 @@ USAGE
$ mockoon-cli dockerize
OPTIONS
-d, --data=data [required] Path or URL to your Mockoon file
-p, --port=port Override environment's port
-d, --data [required] Path or URL to your Mockoon file
-p, --port Override environment's port
-o, --output [required] Generated Dockerfile path and name (e.g. `./Dockerfile`)
-t, --log-transaction Log the full HTTP transaction (request and response)
-r, --repair If the data file seems too old, or an invalid Mockoon file, migrate/repair without prompting
-i, --index=index [deprecated] Select by environment's index in the legacy export file
-n, --name=name [deprecated] Select by environment name in the legacy export file
-h, --help Show CLI help
EXAMPLES
$ mockoon-cli dockerize --data ~/data.json --output ./Dockerfile
$ mockoon-cli dockerize --data ~/data1.json ~/data2.json --output ./Dockerfile
$ mockoon-cli dockerize --data https://file-server/data.json --output ./Dockerfile
```

Expand Down Expand Up @@ -327,7 +322,7 @@ The `transaction` model can be found [here](https://github.com/mockoon/commons/b

## PM2

Mockoon CLI uses [PM2](https://pm2.keymetrics.io/) to start, stop or list the running mock APIs. Therefore, you can directly use PM2 commands to manage the processes.
Mockoon CLI uses [PM2](https://pm2.keymetrics.io/) to start, stop or list the running mock APIs when you are not using the `--daemon-off` flag. Therefore, you can directly use PM2 commands to manage the processes.

## Mockoon's documentation

Expand Down
76 changes: 45 additions & 31 deletions src/commands/dockerize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import { Config } from '../config';
import { commonFlags, startFlags } from '../constants/command.constants';
import { DOCKER_TEMPLATE } from '../constants/docker.constants';
import { Messages } from '../constants/messages.constants';
import { parseDataFile, prepareData } from '../libs/data';
import { portIsValid, promptEnvironmentChoice } from '../libs/utils';
import { parseDataFiles, prepareEnvironment } from '../libs/data';
import { portIsValid } from '../libs/utils';

export default class Dockerize extends Command {
public static description =
'Create a Dockerfile to build a self-contained image of a mock API';
'Create a Dockerfile to build a self-contained image of one or more mock API';

public static examples = [
'$ mockoon-cli dockerize --data ~/data.json --output ./Dockerfile',
'$ mockoon-cli dockerize --data ~/data1.json ~/data2.json --output ./Dockerfile',
'$ mockoon-cli dockerize --data https://file-server/data.json --output ./Dockerfile'
];

Expand All @@ -31,38 +32,44 @@ export default class Dockerize extends Command {
};

public async run(): Promise<void> {
let { flags: userFlags } = this.parse(Dockerize);
const { flags: userFlags } = this.parse(Dockerize);
const resolvedDockerfilePath = pathResolve(userFlags.output);
const dockerfilePath: ParsedPath = pathParse(resolvedDockerfilePath);

let environmentInfo: { name: any; port: any; dataFile: string };
const parsedEnvironments = await parseDataFiles(userFlags.data);
userFlags.data = parsedEnvironments.filePaths;

const environments = await parseDataFile(userFlags.data);

userFlags = await promptEnvironmentChoice(userFlags, environments);
const environmentsInfo: { name: any; port: any; dataFile: string }[] = [];

try {
environmentInfo = await prepareData({
environments,
options: {
index: userFlags.index,
name: userFlags.name,
port: userFlags.port
},
dockerfileDir: dockerfilePath.dir,
repair: userFlags.repair
});
} catch (error: any) {
this.error(error.message);
}
if (!portIsValid(environmentInfo.port)) {
this.error(format(Messages.CLI.PORT_IS_NOT_VALID, environmentInfo.port));
}
for (
let envIndex = 0;
envIndex < parsedEnvironments.environments.length;
envIndex++
) {
const environmentInfo = await prepareEnvironment({
environment: parsedEnvironments.environments[envIndex],
userOptions: {
port: userFlags.port[envIndex]
},
dockerfileDir: dockerfilePath.dir,
repair: userFlags.repair
});

environmentsInfo.push(environmentInfo);

if (!portIsValid(environmentInfo.port)) {
this.error(
format(Messages.CLI.PORT_IS_NOT_VALID, environmentInfo.port)
);
}
}

try {
const dockerFile = mustacheRender(DOCKER_TEMPLATE, {
port: environmentInfo.port,
filePath: pathParse(environmentInfo.dataFile).base,
ports: environmentsInfo.map((environmentInfo) => environmentInfo.port),
filePaths: environmentsInfo.map(
(environmentInfo) => pathParse(environmentInfo.dataFile).base
),
version: Config.version,
// passing more args to the dockerfile template, only make sense for log transaction yet as other flags are immediately used during the file creation and data preparation
args: userFlags['log-transaction'] ? ', "--log-transaction"' : ''
Expand All @@ -76,10 +83,17 @@ export default class Dockerize extends Command {
this.log(
Messages.CLI.DOCKERIZE_BUILD_COMMAND,
dockerfilePath.dir,
environmentInfo.name,
environmentInfo.port,
environmentInfo.port,
environmentInfo.name
environmentsInfo.length > 1
? 'mockoon-mocks'
: environmentsInfo[0].name,
environmentsInfo.reduce(
(portsString, environmentInfo) =>
`${portsString ? portsString + ' ' : portsString}-p ${
environmentInfo.port
}:${environmentInfo.port}`,
''
),
environmentsInfo.length > 1 ? 'mockoon-mocks' : environmentsInfo[0].name
);
} catch (error: any) {
this.error(error.message);
Expand Down
Loading

0 comments on commit 86545ac

Please sign in to comment.