diff --git a/openapi.yaml b/openapi.yaml index 0b520ba..dd75b6b 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -919,6 +919,9 @@ components: items: $ref: "#/components/schemas/EnvironmentSimpleResponse" description: The environments of the test target + required: + - id + - app TestTargetUpdateRequest: type: object properties: diff --git a/src/cli.ts b/src/cli.ts index dbaf521..63dd2e0 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,6 +20,7 @@ import { promptUser, resolveTestTargetId } from "./helpers"; import { runDebugtopus } from "./debugtopus"; import { startPrivateLocationWorker, stopPLW } from "./plw"; +import { getTestTargets, listTestTargets } from "./tools/test-targets"; const createCommandWithCommonOptions = (command: string): Command => { return program @@ -30,6 +31,26 @@ const createCommandWithCommonOptions = (command: string): Command => { const splitter = (value: string): string[] => value.split(/,| |\|/); const toJSON = (value: string): object => JSON.parse(value); +const selectTestTarget = async (): Promise => { + const testTargets = await getTestTargets(); + await listTestTargets({}); + + if (testTargets.length === 1) { + console.log(`Only one test target found, using it: ${testTargets[0].app} (${testTargets[0].id})`); + return testTargets[0].id; + } + + const testTargetIndex = await promptUser( + "Enter number of the test target you want to use (optional, press Enter to skip): " + ); + const testTargetId = testTargets[Number.parseInt(testTargetIndex) - 1].id; + if (!testTargetId) { + console.log("❌ could not find a test target with the index you provided"); + process.exit(1); + } + + return testTargetId; +} const testTargetIdOption = new Option("-t, --test-target-id [id]", "Test target ID, if not provided will use the test target id from the config"); @@ -83,12 +104,14 @@ export const buildCmd = (): Command => { process.exit(1); } } - let testTargetId; - if (!options.testTargetId) { - testTargetId = await promptUser( - "Enter test target id (optional, press Enter to skip): " - ); + // saving here to be able to use the api key for the test targets + const newApiKeyConfig = { + ...existingConfig, + apiKey: options.apiKey ?? apiKey, } + await saveConfig(newApiKeyConfig); + + const testTargetId = await selectTestTarget(); const newConfig: Config = { ...existingConfig, @@ -109,6 +132,20 @@ export const buildCmd = (): Command => { } ); + program + .command("switch-test-target") + .description("Switch to a different test target") + .action(async () => { + const testTargetId = await selectTestTarget(); + const existingConfig = await loadConfig(); + const newConfig: Config = { + ...existingConfig, + testTargetId, + }; + await saveConfig(newConfig); + console.log(`✨ Switched to test target: ${testTargetId}`); + }); + createCommandWithCommonOptions("debug") .description("run test cases against local build") .requiredOption("-u, --url ", "url the tests should run against") @@ -322,5 +359,9 @@ export const buildCmd = (): Command => { void listTestCases({ ...options, status: "ENABLED" }); }); + createCommandWithCommonOptions("list-test-targets") + .description("List all test targets") + .action(listTestTargets); + return program; }; diff --git a/src/tools/test-targets.ts b/src/tools/test-targets.ts new file mode 100644 index 0000000..eec6c0e --- /dev/null +++ b/src/tools/test-targets.ts @@ -0,0 +1,32 @@ +import { client, handleError, ListOptions, logJson } from "./client"; + +export const getTestTargets = async () => { + const { data, error } = await client.GET("/apiKey/v2/test-targets"); + + handleError(error); + + if (!data) { + throw Error("No test targets found"); + } + + return data; +}; + +export const listTestTargets = async (options: ListOptions): Promise => { + const testTargets = await getTestTargets(); + + if (options.json) { + logJson(testTargets); + return; + } + + console.log("Test Targets:"); + testTargets.forEach((testTarget, idx) => { + const idxString = `${idx + 1}. `.padEnd( + testTargets.length.toString().length + 2, + ); + const paddingString = " ".repeat(idxString.length); + console.log(`${idxString}ID: ${testTarget.id}`); + console.log(`${paddingString}App: ${testTarget.app}`); + }); +};