Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/generator-cli/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ Further it is also possible to configure generators, for example:
If `openapi-generator-cli generate` is called without further arguments, then the configuration
is automatically used to generate your code. 🎉

### Config file location

By default, `openapi-generator-cli` reads and writes *openapitools.json* in the current working directory.
To use a config file somewhere else (e.g. in a monorepo with a shared configuration), pass a global option
before the command or set an environment variable:

```sh
# global option (place it before the command)
openapi-generator-cli -c /path/to/openapitools.json generate -i spec.yaml -g ruby

# the long form works as well and is equivalent to the existing --openapitools option
openapi-generator-cli --config /path/to/openapitools.json version-manager list

# via environment variable, e.g. in CI
OPENAPI_GENERATOR_CLI_CONFIG=/path/to/openapitools.json openapi-generator-cli generate
```

The config file location is resolved in the following order:

1. the global option `-c, --config <path>` or `--openapitools <openapitools.json>`
2. the `OPENAPI_GENERATOR_CLI_CONFIG` environment variable
3. *./openapitools.json* (default)

Relative paths are resolved against the current working directory. When an explicit path is given
(via option or environment variable) but the file does not exist, the CLI exits with an error instead
of silently creating a new configuration file. When neither the option nor the environment variable is
set, the default *./openapitools.json* is used (and created on first usage), just like before.

`-c` and `--config` must be placed **before** the command name — afterwards they belong to the
invoked command itself. For example, the config file in `openapi-generator-cli generate -c generator-config.yaml`
is the generator configuration read by the Java generator, not the *openapitools.json* location.


##### Available placeholders

Expand Down
64 changes: 64 additions & 0 deletions apps/generator-cli/src/app/app.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ describe('AppModule', () => {
describe.each([
['the separate option value', ['--openapitools', 'x.json']],
['the inline option value', ['--openapitools=x.json']],
['the separate -c option value', ['-c', 'x.json']],
['the separate --config option value', ['--config', 'x.json']],
['the inline --config option value', ['--config=x.json']],
['the attached -c option value', ['-cx.json']],
])('version-manager resolve with %s', (_, configArgs) => {
beforeEach(async () => {
process.argv = [
Expand Down Expand Up @@ -191,6 +195,66 @@ describe('AppModule', () => {
});
});

describe('a command is invoked with its own -c/--config option', () => {
beforeEach(async () => {
process.argv = [
'foo',
'baz',
'generate',
'-i',
'spec.yaml',
'-c',
'gen-cfg.yaml',
];
programMock.parse.mockImplementation(() => undefined);
versionManagerServiceMock.getSelectedVersion.mockReturnValue('1.2.3');
await fixture.onApplicationBootstrap();
});

it('hands the command arguments untouched to commander', () => {
expect(programMock.parse).toHaveBeenCalledWith([
'foo',
'baz',
'generate',
'-i',
'spec.yaml',
'-c',
'gen-cfg.yaml',
]);
});
});

describe.each([
['the separate -c option value', ['-c', 'x.json']],
['the separate --config option value', ['--config', 'x.json']],
['the inline --config option value', ['--config=x.json']],
['the attached -c option value', ['-cx.json']],
])('a global -c/--config option is used as %s before the command', (_, configArgs) => {
beforeEach(async () => {
process.argv = [
'foo',
'baz',
...configArgs,
'version-manager',
'resolve',
'latest',
];
programMock.parse.mockImplementation(() => undefined);
versionManagerServiceMock.getSelectedVersion.mockReturnValue('1.2.3');
await fixture.onApplicationBootstrap();
});

it('hands commander an argv without the config option, so the command is dispatched', () => {
expect(programMock.parse).toHaveBeenCalledWith([
'foo',
'baz',
'version-manager',
'resolve',
'latest',
]);
});
});

describe('a custom generator is provided', () => {
beforeEach(async () => {
process.argv = [
Expand Down
38 changes: 35 additions & 3 deletions apps/generator-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { COMMANDER_PROGRAM, LOGGER } from './constants';
import { VersionManagerController } from './controllers/version-manager.controller';
import {
ConfigService,
extractGlobalConfigArgs,
} from './services/config.service';
import {
GeneratorService,
PassThroughService,
UIService,
Expand Down Expand Up @@ -49,6 +52,21 @@ if (hasHttpsProxyEnvs) {
.option(
'--openapitools <openapitools.json>',
'Use the specified openapi-generator-cli configuration file',
)
// The global `-c, --config <path>` option is intentionally NOT
// registered with commander: commander would consume it from the whole
// argv — including after the command name — which would swallow the
// Java generator's own `generate -c, --config <config file>` option.
// Instead, it is only honored BEFORE the command name by ConfigService
// (see extractGlobalConfigArgs) and stripped from the argv handed to
// commander below. It is listed here so it still shows up in the help.
.addHelpText(
'after',
'\nGlobal config option (must be placed before the command name):\n\n' +
' -c, --config <path>\n' +
' Use the specified openapi-generator-cli configuration file (alternative to --openapitools, also available via the ' +
ConfigService.envConfigVariable +
' environment variable)\n',
),
},
{ provide: LOGGER, useValue: console },
Expand Down Expand Up @@ -86,7 +104,13 @@ export class AppModule implements OnApplicationBootstrap {
if (!isVersionManagerResolveCommand) {
await this.passThroughService.init();
}
this.program.parse(process.argv);
// Remove the global -c/--config options from the argv handed to commander:
// they are not registered (see above), and an unrecognized option before
// the command name would make commander treat the command itself as an
// unknown argument and skip its dispatch.
this.program.parse(
extractGlobalConfigArgs(process.argv).argvWithoutGlobalConfigArgs,
);
};

private isVersionManagerResolveCommand() {
Expand All @@ -96,12 +120,20 @@ export class AppModule implements OnApplicationBootstrap {
while (commandIndex < args.length) {
const arg = args[commandIndex];

if (arg === '--openapitools') {
if (
arg === '--openapitools' ||
arg === '--config' ||
arg === '-c'
) {
commandIndex += 2;
continue;
}

if (arg.startsWith('--openapitools=')) {
if (
arg.startsWith('--openapitools=') ||
arg.startsWith('--config=') ||
(arg.startsWith('-c') && arg.length > 2)
) {
commandIndex += 1;
continue;
}
Expand Down
Loading
Loading