@vortex.so/cli
Version:
CLI to interact with Vortex.
54 lines (51 loc) • 1.61 kB
JavaScript
import { execaCommand } from 'execa';
import 'node:process';
import c from 'chalk';
import 'figures';
import 'jiti';
import { Log } from '../../utils/log/index.mjs';
import { getManifest } from '../ship/ship.manifest.mjs';
const log = new Log("Vault");
async function getEnv(env) {
const loader = log.wait();
try {
const { name, handle, org } = getManifest("vortex.yaml");
if (!handle)
throw new Error("Missing handle.");
if (!org)
throw new Error("Missing org.");
loader.persist(
`Getting secrets for ${c.bold.green(env)} environment of ${c.bold.green(
name
)}...`
);
const getValues = await execaCommand(
`vault kv get -format=json vars/${org}/${handle}/${env}`,
{
shell: true,
env: { VAULT_ADDR: "https://vault.vortex.so" }
}
);
if (getValues.stderr)
loader.persist(c.gray(getValues.stderr));
const resp = JSON.parse(getValues.stdout);
const secrets = resp?.data?.data;
if (!secrets)
throw new Error("No secrets found.");
const variables = Object.keys(secrets).map((key) => `${key}="${secrets[key]}"`).join("\n");
const saveValues = await execaCommand(
`echo '${variables}' > .env.${env}`,
{ shell: true }
);
if (saveValues.stderr)
loader.persist(c.gray(saveValues.stderr));
const file = c.bold.green(`.env.${env}`);
loader.ok(`Secrets have been generated into ${file}!`);
} catch (error) {
loader.fail(c.gray(error?.message || "Something is failed."));
}
}
class Vault {
static getEnv = getEnv;
}
export { Vault, getEnv };