-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
similar to `npx convex self-host import`, export, and run, we can support env variables through the self-host CLI GitOrigin-RevId: 381168f4e0a2f367108d60ca2184b819c726e3c0
- Loading branch information
Showing
3 changed files
with
274 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import chalk from "chalk"; | ||
import { | ||
Context, | ||
logFailure, | ||
logFinishedStep, | ||
logMessage, | ||
logOutput, | ||
} from "../../bundler/context.js"; | ||
import { runSystemQuery } from "./run.js"; | ||
import { deploymentFetch, logAndHandleFetchError } from "./utils/utils.js"; | ||
|
||
export async function envSetInDeployment( | ||
ctx: Context, | ||
deployment: { | ||
deploymentUrl: string; | ||
adminKey: string; | ||
deploymentNotice: string; | ||
}, | ||
originalName: string, | ||
originalValue: string | undefined, | ||
) { | ||
const [name, value] = await allowEqualsSyntax( | ||
ctx, | ||
originalName, | ||
originalValue, | ||
); | ||
await callUpdateEnvironmentVariables(ctx, deployment, [{ name, value }]); | ||
const formatted = /\s/.test(value) ? `"${value}"` : value; | ||
logFinishedStep( | ||
ctx, | ||
`Successfully set ${chalk.bold(name)} to ${chalk.bold(formatted)}${deployment.deploymentNotice}`, | ||
); | ||
} | ||
|
||
async function allowEqualsSyntax( | ||
ctx: Context, | ||
name: string, | ||
value: string | undefined, | ||
) { | ||
if (value === undefined) { | ||
if (/^[a-zA-Z][a-zA-Z0-9_]+=/.test(name)) { | ||
return name.split("=", 2); | ||
} else { | ||
return await ctx.crash({ | ||
exitCode: 1, | ||
errorType: "fatal", | ||
printedMessage: "error: missing required argument 'value'", | ||
}); | ||
} | ||
} | ||
return [name, value]; | ||
} | ||
|
||
export async function envGetInDeployment( | ||
ctx: Context, | ||
deployment: { | ||
deploymentUrl: string; | ||
adminKey: string; | ||
}, | ||
name: string, | ||
) { | ||
const envVar = (await runSystemQuery(ctx, { | ||
...deployment, | ||
functionName: "_system/cli/queryEnvironmentVariables:get", | ||
componentPath: undefined, | ||
args: { name }, | ||
})) as EnvVar | null; | ||
if (envVar === null) { | ||
logFailure(ctx, `Environment variable "${name}" not found.`); | ||
return; | ||
} | ||
logOutput(ctx, `${envVar.value}`); | ||
} | ||
|
||
export async function envRemoveInDeployment( | ||
ctx: Context, | ||
deployment: { | ||
deploymentUrl: string; | ||
adminKey: string; | ||
deploymentNotice: string; | ||
}, | ||
name: string, | ||
) { | ||
await callUpdateEnvironmentVariables(ctx, deployment, [{ name }]); | ||
logFinishedStep( | ||
ctx, | ||
`Successfully unset ${chalk.bold(name)}${deployment.deploymentNotice}`, | ||
); | ||
} | ||
|
||
export async function envListInDeployment( | ||
ctx: Context, | ||
deployment: { | ||
deploymentUrl: string; | ||
adminKey: string; | ||
}, | ||
) { | ||
const envs = (await runSystemQuery(ctx, { | ||
...deployment, | ||
functionName: "_system/cli/queryEnvironmentVariables", | ||
componentPath: undefined, | ||
args: {}, | ||
})) as EnvVar[]; | ||
if (envs.length === 0) { | ||
logMessage(ctx, "No environment variables set."); | ||
return; | ||
} | ||
for (const { name, value } of envs) { | ||
logOutput(ctx, `${name}=${value}`); | ||
} | ||
} | ||
|
||
type EnvVarChange = { | ||
name: string; | ||
value?: string; | ||
}; | ||
|
||
type EnvVar = { | ||
name: string; | ||
value: string; | ||
}; | ||
|
||
async function callUpdateEnvironmentVariables( | ||
ctx: Context, | ||
deployment: { | ||
deploymentUrl: string; | ||
adminKey: string; | ||
deploymentNotice: string; | ||
}, | ||
changes: EnvVarChange[], | ||
) { | ||
const fetch = deploymentFetch(ctx, deployment); | ||
try { | ||
await fetch("/api/update_environment_variables", { | ||
body: JSON.stringify({ changes }), | ||
method: "POST", | ||
}); | ||
} catch (e) { | ||
return await logAndHandleFetchError(ctx, e); | ||
} | ||
} |
Oops, something went wrong.