-
Notifications
You must be signed in to change notification settings - Fork 0
feat: added support for SDK #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { config } from "@/lib/config"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { logError } from "@/lib/error"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import http from "@/providers/enkryptify/httpClient"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Command } from "commander"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export function registerSdkCommand(program: Command): void { | ||||||||||||||||||||||||||||||||||||||||||||||||
| program | ||||||||||||||||||||||||||||||||||||||||||||||||
| .command("sdk") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .description("Run a command with a read-only Enkryptify SDK token") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .allowUnknownOption() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .allowExcessArguments() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .action(async (_options, cmd: Command) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const args = cmd.args as string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (args.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| logError("No command provided. Usage: ek sdk -- <command>"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // 1. Load project config (walks up from cwd) | ||||||||||||||||||||||||||||||||||||||||||||||||
| let setup; | ||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||
| setup = await config.getConfigure(process.cwd()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // getConfigure returns null if not found, doesn't throw | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!setup || setup.provider !== "enkryptify") { | ||||||||||||||||||||||||||||||||||||||||||||||||
| logError("No Enkryptify project configured in this directory. Run `ek configure` first."); | ||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // 2. Create scoped SDK token (read-only, single environment, 8h) | ||||||||||||||||||||||||||||||||||||||||||||||||
| let token: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { data } = await http.post<{ token: string }>( | ||||||||||||||||||||||||||||||||||||||||||||||||
| `/v1/workspace/${setup.workspace_slug}/tokens/cli`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| { environmentId: setup.environment_id }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| token = data.token; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| logError(error instanceof Error ? error.message : String(error)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // 3. Spawn child process with token injected | ||||||||||||||||||||||||||||||||||||||||||||||||
| const [bin, ...rest] = args; | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!bin) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| logError("No command provided. Usage: ek sdk -- <command>"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const proc = Bun.spawn([bin, ...rest], { | ||||||||||||||||||||||||||||||||||||||||||||||||
| env: { ...process.env, ENKRYPTIFY_TOKEN: token }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| stdin: "inherit", | ||||||||||||||||||||||||||||||||||||||||||||||||
| stdout: "inherit", | ||||||||||||||||||||||||||||||||||||||||||||||||
| stderr: "inherit", | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const exitCode = await proc.exited; | ||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(exitCode); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat -n src/cmd/sdk.tsRepository: Enkryptify/cli Length of output: 2784 Wrap The code at lines 52-60 lacks error handling for Proposed fix- const proc = Bun.spawn([bin, ...rest], {
- env: { ...process.env, ENKRYPTIFY_TOKEN: token },
- stdin: "inherit",
- stdout: "inherit",
- stderr: "inherit",
- });
-
- const exitCode = await proc.exited;
- process.exit(exitCode);
+ try {
+ const proc = Bun.spawn([bin, ...rest], {
+ env: { ...process.env, ENKRYPTIFY_TOKEN: token },
+ stdin: "inherit",
+ stdout: "inherit",
+ stderr: "inherit",
+ });
+
+ const exitCode = await proc.exited;
+ process.exit(exitCode);
+ } catch (error: unknown) {
+ logError(error instanceof Error ? error.message : String(error));
+ process.exit(1);
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate the loaded config fields and returned token before continuing.
setup.provider === "enkryptify"does not guaranteeworkspace_slugandenvironment_idexist, anddata.tokenis used without a runtime check. A stale/broken project config or a malformed 200 response can turn into/v1/workspace/undefined/...requests or a child process launched without credentials.🛡️ Proposed validation guard
As per coding guidelines: Validate all user inputs, and always throw descriptive errors with context.
🤖 Prompt for AI Agents