Using commander to make it easier to deal with arguments and options
This commit is contained in:
@@ -7,12 +7,13 @@
|
||||
"@aws-sdk/client-secrets-manager": "^3.238.0",
|
||||
"@trigger.dev/tsconfig": "workspace:*",
|
||||
"@types/node": "^18.11.9",
|
||||
"commander": "^9.4.1",
|
||||
"internal-catalog": "workspace:*",
|
||||
"tiny-invariant": "^1.2.0",
|
||||
"tsx": "^3.12.0",
|
||||
"zod": "^3.20.2"
|
||||
},
|
||||
"scripts": {
|
||||
"cli": "tsx src/index.ts"
|
||||
"cli": "tsx src/index.ts update"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Command } from "commander";
|
||||
import * as fs from "node:fs";
|
||||
import util from "node:util";
|
||||
const exec = util.promisify(require("child_process").exec);
|
||||
import { z } from "zod";
|
||||
import invariant from "tiny-invariant";
|
||||
import { getCatalog } from "internal-catalog";
|
||||
@@ -13,69 +12,70 @@ const client = new SecretsManagerClient({
|
||||
region: "us-east-1",
|
||||
});
|
||||
|
||||
async function run() {
|
||||
const args = process.argv.slice(2);
|
||||
const [integrationFilePath] = args;
|
||||
const program = new Command();
|
||||
|
||||
if (!integrationFilePath) {
|
||||
console.error(
|
||||
"Missing integration file path.",
|
||||
"You probably want to pass in: ../../apps/webapp/integrations.yml"
|
||||
);
|
||||
return;
|
||||
}
|
||||
program
|
||||
.command("update")
|
||||
.description("Update the catalog")
|
||||
.argument(
|
||||
"<integration_file_path>",
|
||||
"The file path to the integration file. Probably ../../apps/webapp/integrations.yml"
|
||||
)
|
||||
.option("-e, --environment <environment>", "The environment to update")
|
||||
.action(
|
||||
async (
|
||||
integration_file_path: string,
|
||||
options: { environment?: string }
|
||||
) => {
|
||||
if (!integration_file_path) {
|
||||
console.error(
|
||||
"Missing integration file path.",
|
||||
"You probably want to pass in: ../../apps/webapp/integrations.yml"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let environment = args[1];
|
||||
if (!environment) {
|
||||
environment = "development";
|
||||
}
|
||||
const environment = options.environment ?? "development";
|
||||
|
||||
console.log(`Environment: ${environment}`);
|
||||
const file = fs.readFileSync(integration_file_path, "utf8");
|
||||
const catalog = getCatalog(file);
|
||||
|
||||
const file = fs.readFileSync(integrationFilePath, "utf8");
|
||||
const catalog = getCatalog(file);
|
||||
const promises = catalog.map(async (integration) => {
|
||||
const environmentClientId =
|
||||
integration.environments[environment!].oauth.client_id;
|
||||
const secretId = `integrations/${integration.slug}/${environmentClientId}`;
|
||||
try {
|
||||
console.log(`Finding secret for id: ${secretId}`);
|
||||
|
||||
const promises = catalog.map(async (integration) => {
|
||||
const environmentClientId =
|
||||
integration.environments[environment].oauth.client_id;
|
||||
const secretId = `integrations/${integration.slug}/${environmentClientId}`;
|
||||
try {
|
||||
console.log(`Finding secret for id: ${secretId}`);
|
||||
const response = await client.send(
|
||||
new GetSecretValueCommand({
|
||||
SecretId: secretId,
|
||||
VersionStage: "AWSCURRENT", // VersionStage defaults to AWSCURRENT if unspecified
|
||||
})
|
||||
);
|
||||
|
||||
const response = await client.send(
|
||||
new GetSecretValueCommand({
|
||||
SecretId: secretId,
|
||||
VersionStage: "AWSCURRENT", // VersionStage defaults to AWSCURRENT if unspecified
|
||||
})
|
||||
);
|
||||
const secretData = response.SecretString;
|
||||
invariant(secretData, `Secret data is missing: ${secretId}`);
|
||||
const secretObject = JSON.parse(secretData);
|
||||
const parsed = z
|
||||
.object({
|
||||
client_secret: z.string(),
|
||||
})
|
||||
.parse(secretObject);
|
||||
|
||||
const secretData = response.SecretString;
|
||||
invariant(secretData, `Secret data is missing: ${secretId}`);
|
||||
const secretObject = JSON.parse(secretData);
|
||||
const parsed = z
|
||||
.object({
|
||||
client_secret: z.string(),
|
||||
})
|
||||
.parse(secretObject);
|
||||
console.log(`Found secret for id: ${secretId}`);
|
||||
} catch (error) {
|
||||
// For a list of exceptions thrown, see
|
||||
// https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
const { stdout, stderr } = await exec(
|
||||
`npx pizzly config:create ${integration.slug} ${
|
||||
integration.slug
|
||||
} ${environmentClientId} ${
|
||||
parsed.client_secret
|
||||
} "${integration.scopes.join(",")}"`
|
||||
);
|
||||
console.log("stdout:", stdout);
|
||||
console.log("stderr:", stderr);
|
||||
} catch (error) {
|
||||
// For a list of exceptions thrown, see
|
||||
// https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
|
||||
throw error;
|
||||
await Promise.all(promises);
|
||||
console.log(`Added ${promises.length} secrets`);
|
||||
}
|
||||
});
|
||||
);
|
||||
|
||||
await Promise.all(promises);
|
||||
console.log(`Added ${promises.length} secrets`);
|
||||
}
|
||||
async function getConfig() {}
|
||||
|
||||
run();
|
||||
program.parseAsync(process.argv);
|
||||
|
||||
Generated
+7
@@ -515,6 +515,7 @@ importers:
|
||||
'@aws-sdk/client-secrets-manager': ^3.238.0
|
||||
'@trigger.dev/tsconfig': workspace:*
|
||||
'@types/node': ^18.11.9
|
||||
commander: ^9.4.1
|
||||
internal-catalog: workspace:*
|
||||
tiny-invariant: ^1.2.0
|
||||
tsx: ^3.12.0
|
||||
@@ -523,6 +524,7 @@ importers:
|
||||
'@aws-sdk/client-secrets-manager': 3.238.0
|
||||
'@trigger.dev/tsconfig': link:../../config-packages/tsconfig
|
||||
'@types/node': 18.11.15
|
||||
commander: 9.4.1
|
||||
internal-catalog: link:../internal-catalog
|
||||
tiny-invariant: 1.3.1
|
||||
tsx: 3.12.1
|
||||
@@ -6286,6 +6288,11 @@ packages:
|
||||
engines: {node: '>= 6'}
|
||||
dev: true
|
||||
|
||||
/commander/9.4.1:
|
||||
resolution: {integrity: sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==}
|
||||
engines: {node: ^12.20.0 || >=14}
|
||||
dev: true
|
||||
|
||||
/common-tags/1.8.2:
|
||||
resolution: {integrity: sha512-gk/Z852D2Wtb//0I+kRFNKKE9dIIVirjoqPoA1wJU+XePVXZfGeBpk45+A1rKO4Q43prqWBNY/MiIeRLbPWUaA==}
|
||||
engines: {node: '>=4.0.0'}
|
||||
|
||||
Reference in New Issue
Block a user