Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve inline documentation #172

Merged
merged 3 commits into from
Aug 20, 2024
Merged
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
8 changes: 5 additions & 3 deletions apps/cli/src/command/convert.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ class BaseConvertCommand extends BaseCommand {
super(name);
this.option(
'-f, --file <openapi-file-path>',
'OpenAPI configuration file path',
'OpenAPI specification file path',
).option('-o, --output <output-path>', 'output file path');
}
}

const OpenAPICommand = new BaseConvertCommand('openapi')
.description('Converting OpenAPI to ADC Configuration')
.description('Convert an OpenAPI specification to equivalent ADC configuration.\n\nLearn more at: https://docs.api7.ai/enterprise/reference/openapi-adc')
.summary('convert OpenAPI spec to ADC configuration')
.action(async () => {
const opts = OpenAPICommand.optsWithGlobals<ConvertOptions>();

Expand Down Expand Up @@ -92,5 +93,6 @@ const OpenAPICommand = new BaseConvertCommand('openapi')
});

export const ConvertCommand = new BaseCommand('convert')
.description('Convert other API spec to ADC configurations')
.description('Convert API definitions in other formats to equivalent ADC configuration.')
.summary('convert API definitions in other formats to ADC configuration')
.addCommand(OpenAPICommand);
6 changes: 4 additions & 2 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ export const DiffResourceTask = (

export const DiffCommand = new BackendCommand<DiffOptions>(
'diff',
'Show the difference between local and backend configurations',
'show differences between the local and the backend configurations',
'Compare the configuration in the specified file(s) with the backend configuration',
Copy link
Contributor

@kayx23 kayx23 Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only curious to know why the first sentence needs to be lower cased

I noticed all similar instances are changed to lower case

Copy link
Contributor Author

@pottekkat pottekkat Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default the library we use, commander, uses lower case when showing the summary of a command/flag, like the in-built help command/version flag.

I just followed the convention.

The alternative was to use custom help and version commands which seemed unnecessary.

I also don't have strong opinions on choosing one over the other.

)
.option(
'-f, --file <file-path>',
'The files you want to synchronize, can be set more than one.',
'file to compare',
(filePath, files: Array<string> = []) => files.concat(filePath),
)
.addOption(NoLintOption)
.addExample('adc diff -f adc.yaml')
.addExample('adc diff -f service-a.yaml -f service-b.yaml')
.handle(async (opts) => {
const backend = loadBackend(opts.backend, opts);
Expand Down
7 changes: 4 additions & 3 deletions apps/cli/src/command/dump.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ export const LoadRemoteConfigurationTask = ({

export const DumpCommand = new BackendCommand<DumpOptions>(
'dump',
'Dump configurations from the backend',
'save the configuration of the backend to a file',
'Save the configuration of the backend to the specified YAML file.',
)
.option(
'-o, --output <file-path>',
'Specify the file path where data is dumped from the backend',
'path of the file to save the configuration',
'adc.yaml',
)
.addExample('adc dump')
.addExample('adc dump -o other-name.yaml')
.addExample('adc dump -o service-configuration.yaml')
.handle(async (opts) => {
const backend = loadBackend(opts.backend, opts);
const tasks = new Listr<TaskContext, typeof SignaleRenderer>(
Expand Down
43 changes: 22 additions & 21 deletions apps/cli/src/command/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import qs from 'qs';
export class BaseCommand extends Command {
private exmaples: Array<string> = [];

constructor(name: string, description?: string) {
constructor(name: string, summary?: string, description?: string) {
super(name);

if (summary) this.summary(summary);
if (description) this.description(description);

// Add global flag - verbose
this.addOption(
new Option(
'--verbose <integer>',
'Override verbose logging levels, it supports 0: no logs, 1: basic logs, 2: debug logs',
'set the verbosity level for logs (0: no logs, 1: basic logs, 2: debug logs)',
)
.argParser((val) => {
const int = parseInt(val);
Expand All @@ -34,7 +35,7 @@ export class BaseCommand extends Command {
public addExample(exmaple: string) {
if (this.exmaples.length === 0)
this.addHelpText('after', () => {
return `\nExample:\n\n${this.exmaples
return `\nExamples:\n\n${this.exmaples
.map((example) => ` $ ${example}`)
.join('\n')}\n`;
});
Expand All @@ -45,8 +46,8 @@ export class BaseCommand extends Command {
}

export class BackendCommand<OPTS extends object = object> extends BaseCommand {
constructor(name: string, description?: string) {
super(name, description);
constructor(name: string, summary?: string, description?: string) {
super(name, summary, description);

this.addBackendOptions();
}
Expand Down Expand Up @@ -92,44 +93,44 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
};

this.addOption(
new Option('--backend <backend>', 'Type of backend to connect')
new Option('--backend <backend>', 'type of backend to connect to')
.env('ADC_BACKEND')
.choices(['apisix', 'api7ee'])
.default('apisix'),
)
.addOption(
new Option(
'--server <string>',
'HTTP address of backend. This value can also be set using the environment variable ADC_SERVER environment variable',
'HTTP address of the backend',
)
.env('ADC_SERVER')
.default('http://localhost:9180'),
)
.addOption(
new Option('--token <string>', 'Token used to access the backend').env(
new Option('--token <string>', 'token for ADC to connect to the backend').env(
'ADC_TOKEN',
),
)
.addOption(
new Option(
'--gateway-group <string>',
'Gateway group used to specify the gateway group to operate [API7EE backend only]',
'gateway group to operate on (only supported for "api7ee" backend)',
)
.env('ADC_GATEWAY_GROUP')
.default('default'),
)
.addOption(
new Option(
'--label-selector <selectors>',
'Filter for resource labels (e.g., labelKey=labelValue)',
'--label-selector <labelKey=labelValue>',
'filter resources by labels',
).argParser((val, previous: Record<string, string> = {}) =>
Object.assign(previous, qs.parse(val, { delimiter: ',' })),
),
)
.addOption(
new Option(
'--include-resource-type <string>',
'Filter for resource types, contains only the specified type',
'filter resources that only contains the specified type',
)
.conflicts('excludeResourceType')
.choices(Object.values(ADCSDK.ResourceType))
Expand All @@ -138,7 +139,7 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
.addOption(
new Option(
'--exclude-resource-type <string>',
'Filter for resource types, not contains only the specified type',
'filter resources that does not contain the specified type',
)
.conflicts('includeResourceType')
.choices(Object.values(ADCSDK.ResourceType))
Expand All @@ -147,7 +148,7 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
.addOption(
new Option(
'--timeout <duration>',
'Set a request timeout for the client to connect with Backend Admin API (in duration, e.g., 10s, 1h10m)',
'timeout for adc to connect with the backend (examples: 10s, 1h10m)',
)
.default(10000, '10s')
.argParser((value) => {
Expand All @@ -157,7 +158,7 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
.addOption(
new Option(
'--ca-cert-file <string>',
'Path to CA certificate for verifying the Backend Admin API',
'path to the CA certificate to verify the backend',
)
.env('ADC_CA_CERT_FILE')
.argParser((value) =>
Expand All @@ -170,38 +171,38 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
.addOption(
new Option(
'--tls-client-cert-file <string>',
'Path to Mutual TLS client certificate for verifying the Backend Admin API',
'path to the mutual TLS client certificate to verify the backend',
)
.env('ADC_TLS_CLIENT_CERT_FILE')
.argParser((value) =>
processCertificateFile(
value,
'The specified Mutual TLS client certificate file does not exist',
'The specified mutual TLS client certificate file does not exist',
),
),
)
.addOption(
new Option(
'--tls-client-key-file <string>',
'Path to Mutual TLS client key for verifying the Backend Admin API',
'path to the mutual TLS client key to verify the backend',
)
.env('ADC_TLS_CLIENT_KEY_FILE')
.argParser((value) =>
processCertificateFile(
value,
'The specified Mutual TLS client key file does not exist',
'The specified mutual TLS client key file does not exist',
),
),
)
.addOption(
new Option(
'--tls-skip-verify',
`Disable verification of Backend Admin API TLS certificate`,
`disable the verification of the backend TLS certificate`,
)
.env('ADC_TLS_SKIP_VERIFY')
.default(false),
);
}
}

export const NoLintOption = new Option('--no-lint', 'Disable lint check');
export const NoLintOption = new Option('--no-lint', 'disable lint check');
3 changes: 2 additions & 1 deletion apps/cli/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const setupCommands = (): Command => {
const program = new Command('adc');

program
.description('API Declarative CLI (ADC) is a utility to manage API7 Enterprise and Apache APISIX declaratively.\n\nLearn more at: https://docs.api7.ai/enterprise/reference/adc')
bzp2010 marked this conversation as resolved.
Show resolved Hide resolved
.configureHelp({ showGlobalOptions: true })
.passThroughOptions()
.version('0.12.1');
.version('0.12.1', '-v, --version', 'display ADC version');

program
.addCommand(PingCommand)
Expand Down
8 changes: 5 additions & 3 deletions apps/cli/src/command/lint.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export const LintTask = (): ListrTask<{ local: ADCSDK.Configuration }> => ({

export const LintCommand = new BaseCommand('lint')
.description(
'Check provided configuration files, local execution only, ensuring inputs meet ADC requirements',
'Lint the local configuration file(s) to ensure it meets ADC requirements.',
)
.summary('lint the local configuration')
.option(
'-f, --file <file-path...>',
'The files you want to synchronize, can be set more than one.',
'-f, --file <file-path>',
'file to lint',
(filePath, files: Array<string> = []) => files.concat(filePath),
)
.addExample('adc lint -f adc.yaml')
.addExample('adc lint -f service-a.yaml -f service-b.yaml')
.action(async () => {
const opts = LintCommand.optsWithGlobals();
Expand Down
7 changes: 4 additions & 3 deletions apps/cli/src/command/ping.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ type PingOptions = BackendOptions;

export const PingCommand = new BackendCommand<PingOptions>(
'ping',
'Verify connectivity with backend',
'check connectivity with the backend',
'Check connectivity with the configured backend.',
).handle(async (opts) => {
const backend = loadBackend(opts.backend, opts);

try {
await backend.ping();
console.log(chalk.green('Connected to backend successfully!'));
console.log(chalk.green(`Connected to the "${opts.backend}" backend successfully!`));
} catch (err) {
console.log(chalk.red(`Unable to connect to the backend, ${err}`));
console.log(chalk.red(`Unable to connect to the "${opts.backend}" backend. ${err}`));
process.exit(1);
}
});
6 changes: 4 additions & 2 deletions apps/cli/src/command/sync.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ type SyncOption = BackendOptions & {

export const SyncCommand = new BackendCommand<SyncOption>(
'sync',
'Sync local configurations to backend',
'sync the local configuration to the backend',
'Synchronize the configuration from the local file(s) to the backend.',
)
.option(
'-f, --file <file-path>',
'The files you want to synchronize, can be set more than one.',
'file to synchronize',
(filePath, files: Array<string> = []) => files.concat(filePath),
)
.addOption(NoLintOption)
.addExample('adc sync -f adc.yaml')
.addExample('adc sync -f service-a.yaml -f service-b.yaml')
.handle(async (opts) => {
const backend = loadBackend(opts.backend, opts);
Expand Down
2 changes: 1 addition & 1 deletion libs/backend-api7/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class BackendAPI7 implements ADCSDK.Backend {

const gatewayGroups = resp?.data?.list;
if (!gatewayGroups.length) {
throw Error(`Gateway group ${this.gatewayGroup} does not exist`);
throw Error(`Gateway group "${this.gatewayGroup}" does not exist`);
}
ctx.gatewayGroupId = this.gatewayGroupId = gatewayGroups[0].id;
},
Expand Down
Loading