Skip to content

Commit

Permalink
chore: Reorganize CLI commands for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
guilgaly committed Sep 4, 2024
1 parent fb477e8 commit 91f1a90
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 340 deletions.
30 changes: 30 additions & 0 deletions js/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Command } from "commander";

import {
bundleFileOption,
bundleFileOptionValue,
sourcesFolderOption,
sourcesFolderOptionValue,
typescriptOption,
typescriptOptionValueWithDefaults
} from "./options";
import { findSimulations } from "../simulations";
import { bundle } from "../bundle";

export default (program: Command): void => {
program
.command("build")
.description("Build Gatling simulations")
.addOption(sourcesFolderOption)
.addOption(bundleFileOption)
.addOption(typescriptOption)
.action(async (options) => {
const sourcesFolder: string = sourcesFolderOptionValue(options);
const bundleFile = bundleFileOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

await bundle({ sourcesFolder, bundleFile, typescript, simulations });
});
};
88 changes: 88 additions & 0 deletions js/cli/src/commands/enterpriseDeploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Command } from "commander";

import {
apiTokenOption,
apiTokenOptionValue,
bundleFileOption,
bundleFileOptionValue,
controlPlaneUrlOption,
controlPlaneUrlOptionValue,
gatlingHomeOption,
gatlingHomeOptionValueWithDefaults,
nonInteractiveOption,
nonInteractiveOptionValue,
packageDescriptorFilenameOption,
packageDescriptorFilenameOptionValue,
packageFileOption,
packageFileOptionValue,
resourcesFolderOption,
resourcesFolderOptionValue,
resultsFolderOption,
resultsFolderOptionValue,
sourcesFolderOption,
sourcesFolderOptionValue,
typescriptOption,
typescriptOptionValueWithDefaults,
urlOption,
urlOptionValue
} from "./options";
import { findSimulations } from "../simulations";
import { installGatlingJs } from "../dependencies";
import { bundle } from "../bundle";
import { enterpriseDeploy, enterprisePackage } from "../enterprise";

export default (program: Command): void => {
program
.command("enterprise-deploy")
.description("Deploy a package and configured simulations")
.addOption(sourcesFolderOption)
.addOption(resourcesFolderOption)
.addOption(bundleFileOption)
.addOption(resultsFolderOption)
.addOption(typescriptOption)
.addOption(gatlingHomeOption)
// Base
.addOption(urlOption)
.addOption(apiTokenOption)
// Plugin configuration
.addOption(controlPlaneUrlOption)
.addOption(nonInteractiveOption)
// Descriptor file
.addOption(packageDescriptorFilenameOption)
// Deployment info
.addOption(packageFileOption)
.action(async (options) => {
const sourcesFolder: string = sourcesFolderOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

const resourcesFolder: string = resourcesFolderOptionValue(options);
const bundleFile = bundleFileOptionValue(options);
const resultsFolder: string = resultsFolderOptionValue(options);
const gatlingHome = gatlingHomeOptionValueWithDefaults(options);
const url = urlOptionValue(options);
const apiToken = apiTokenOptionValue(options);
const controlPlaneUrl = controlPlaneUrlOptionValue(options);
const nonInteractive = nonInteractiveOptionValue(options);
const packageDescriptorFilename = packageDescriptorFilenameOptionValue(options);
const packageFile = packageFileOptionValue(options);

const { graalvmHome, jvmClasspath } = await installGatlingJs({ gatlingHome });
await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
await enterpriseDeploy({
graalvmHome,
jvmClasspath,
bundleFile,
resourcesFolder,
resultsFolder,
url,
apiToken,
controlPlaneUrl,
nonInteractive,
packageDescriptorFilename,
packageFile
});
});
};
41 changes: 41 additions & 0 deletions js/cli/src/commands/enterprisePackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Command } from "commander";

import {
bundleFileOption,
bundleFileOptionValue,
packageFileOption,
packageFileOptionValue,
resourcesFolderOption,
resourcesFolderOptionValue,
sourcesFolderOption,
sourcesFolderOptionValue,
typescriptOption,
typescriptOptionValueWithDefaults
} from "./options";
import { findSimulations } from "../simulations";
import { bundle } from "../bundle";
import { enterprisePackage } from "../enterprise";

export default (program: Command): void => {
program
.command("enterprise-package")
.description("Build Gatling simulations and package them for Gatling Enterprise")
.addOption(sourcesFolderOption)
.addOption(resourcesFolderOption)
.addOption(bundleFileOption)
.addOption(packageFileOption)
.addOption(typescriptOption)
.action(async (options) => {
const sourcesFolder: string = sourcesFolderOptionValue(options);
const resourcesFolder: string = resourcesFolderOptionValue(options);
const bundleFile = bundleFileOptionValue(options);
const packageFile = packageFileOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

await bundle({ sourcesFolder, bundleFile, typescript, simulations });

await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
});
};
113 changes: 113 additions & 0 deletions js/cli/src/commands/enterpriseStart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Command } from "commander";

import {
apiTokenOption,
apiTokenOptionValue,
bundleFileOption,
bundleFileOptionValue,
controlPlaneUrlOption,
controlPlaneUrlOptionValue,
enterpriseSimulationOption,
enterpriseSimulationOptionValue,
gatlingHomeOption,
gatlingHomeOptionValueWithDefaults,
nonInteractiveOption,
nonInteractiveOptionValue,
packageDescriptorFilenameOption,
packageDescriptorFilenameOptionValue,
packageFileOption,
packageFileOptionValue,
resourcesFolderOption,
resourcesFolderOptionValue,
resultsFolderOption,
resultsFolderOptionValue,
runDescriptionOption,
runDescriptionOptionValue,
runTitleOption,
runTitleOptionValue,
sourcesFolderOption,
sourcesFolderOptionValue,
typescriptOption,
typescriptOptionValueWithDefaults,
urlOption,
urlOptionValue,
waitForRunEndOption,
waitForRunEndOptionValue
} from "./options";
import { findSimulations } from "../simulations";
import { installGatlingJs } from "../dependencies";
import { bundle } from "../bundle";
import { enterprisePackage, enterpriseStart } from "../enterprise";

export default (program: Command): void => {
program
.command("enterprise-start")
.description("Start a simulation deployed with `enterprise-deploy`")
.addOption(sourcesFolderOption)
.addOption(resourcesFolderOption)
.addOption(bundleFileOption)
.addOption(resultsFolderOption)
.addOption(typescriptOption)
.addOption(gatlingHomeOption)
// Base
.addOption(urlOption)
.addOption(apiTokenOption)
// Plugin configuration
.addOption(controlPlaneUrlOption)
.addOption(nonInteractiveOption)
// Descriptor file
.addOption(packageDescriptorFilenameOption)
// Deployment info
.addOption(packageFileOption)
// Start
.addOption(enterpriseSimulationOption)
.addOption(runTitleOption)
.addOption(runDescriptionOption)
.addOption(waitForRunEndOption)
.action(async (options) => {
const sourcesFolder: string = sourcesFolderOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

const resourcesFolder: string = resourcesFolderOptionValue(options);
const bundleFile = bundleFileOptionValue(options);
const resultsFolder: string = resultsFolderOptionValue(options);
const gatlingHome = gatlingHomeOptionValueWithDefaults(options);
const url = urlOptionValue(options);
const apiToken = apiTokenOptionValue(options);
const controlPlaneUrl = controlPlaneUrlOptionValue(options);
const nonInteractive = nonInteractiveOptionValue(options);
const packageDescriptorFilename = packageDescriptorFilenameOptionValue(options);
const packageFile = packageFileOptionValue(options);
const enterpriseSimulation = enterpriseSimulationOptionValue(options);
const runTitle = runTitleOptionValue(options);
const runDescription = runDescriptionOptionValue(options);
const waitForRunEnd = waitForRunEndOptionValue(options);

if (nonInteractiveOptionValue(options) && enterpriseSimulation === undefined) {
throw new Error(`No simulation specified when using non-interactive mode`);
}

const { graalvmHome, jvmClasspath } = await installGatlingJs({ gatlingHome });
await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
await enterpriseStart({
graalvmHome,
jvmClasspath,
bundleFile,
resourcesFolder,
resultsFolder,
url,
apiToken,
controlPlaneUrl,
nonInteractive,
packageDescriptorFilename,
packageFile,
enterpriseSimulation,
runTitle,
runDescription,
waitForRunEnd
});
});
};
26 changes: 25 additions & 1 deletion js/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
export * from "./options";
import { Command } from "commander";

import registerInstallCommand from "./install";
import registerBuildCommand from "./build";
import registerRunOnlyCommand from "./runOnly";
import registerRunCommand from "./run";
import registerRecorderCommand from "./recorder";
import registerEnterprisePackageCommand from "./enterprisePackage";
import registerEnterpriseDeployCommand from "./enterpriseDeploy";
import registerEnterpriseStartCommand from "./enterpriseStart";
import { versions } from "../dependencies";

export const program: Command = new Command()
.name("gatling-js-cli")
.version(versions.gatling.jsAdapter)
.description("The Gatling Javascript run & packaging tool");

registerInstallCommand(program);
registerBuildCommand(program);
registerRunOnlyCommand(program);
registerRunCommand(program);
registerRecorderCommand(program);
registerEnterprisePackageCommand(program);
registerEnterpriseDeployCommand(program);
registerEnterpriseStartCommand(program);
19 changes: 19 additions & 0 deletions js/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Command } from "commander";

import { gatlingHomeOption, gatlingHomeOptionValueWithDefaults } from "./options";
import { installGatlingJs } from "../dependencies";
import { logger } from "../log";

export default (program: Command): void => {
program
.command("install")
.description("Install all required components and dependencies for Gatling")
.addOption(gatlingHomeOption)
.action(async (options) => {
const gatlingHome = gatlingHomeOptionValueWithDefaults(options);
const { graalvmHome, coursierBinary, jvmClasspath } = await installGatlingJs({ gatlingHome });
logger.info(`graalvmHome=${graalvmHome}`);
logger.info(`coursierBinary=${coursierBinary}`);
logger.info(`jvmClasspath=${jvmClasspath}`);
});
};
41 changes: 41 additions & 0 deletions js/cli/src/commands/recorder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Command } from "commander";

import {
gatlingHomeOption,
gatlingHomeOptionValueWithDefaults,
resourcesFolderOption,
resourcesFolderOptionValue,
sourcesFolderOption,
sourcesFolderOptionValue,
typescriptOption,
typescriptOptionValueWithDefaults
} from "./options";
import { findSimulations } from "../simulations";
import { installRecorder } from "../dependencies";
import { logger } from "../log";
import { runRecorder } from "../run";

export default (program: Command): void => {
program
.command("recorder")
.description("Run the Gatling recorder")
.addOption(gatlingHomeOption)
.addOption(sourcesFolderOption)
.addOption(typescriptOption)
.addOption(resourcesFolderOption)
.action(async (options) => {
const gatlingHome: string = gatlingHomeOptionValueWithDefaults(options);
const sourcesFolder: string = sourcesFolderOptionValue(options);
const resourcesFolder: string = resourcesFolderOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

const { graalvmHome, coursierBinary, jvmClasspath } = await installRecorder({ gatlingHome });
logger.debug(`graalvmHome=${graalvmHome}`);
logger.debug(`coursierBinary=${coursierBinary}`);
logger.debug(`jvmClasspath=${jvmClasspath}`);

await runRecorder({ graalvmHome, jvmClasspath, sourcesFolder, typescript, resourcesFolder });
});
};
Loading

0 comments on commit 91f1a90

Please sign in to comment.