diff --git a/src/lib/images/list.ts b/src/lib/images/list.ts index 83c13a1..c6c2113 100644 --- a/src/lib/images/list.ts +++ b/src/lib/images/list.ts @@ -6,6 +6,7 @@ import ora from "ora"; import { apiClient } from "../../apiClient.ts"; import { logAndQuit } from "../../helpers/errors.ts"; import { formatDate } from "../../helpers/format-time.ts"; +import { getDefaultWorkspace } from "./utils.ts"; const list = new Command("list") .alias("ls") @@ -28,9 +29,12 @@ Examples:\n ) .action(async (options) => { const client = await apiClient(); + const workspace = await getDefaultWorkspace(); const spinner = ora("Fetching images...").start(); - const { data: result, response } = await client.GET("/v2/images"); + const { data: result, response } = await client.GET("/v2/images", { + params: { query: { workspace } }, + }); spinner.stop(); if (!response.ok || !result) { diff --git a/src/lib/images/upload.ts b/src/lib/images/upload.ts index deb2a41..36f17e2 100644 --- a/src/lib/images/upload.ts +++ b/src/lib/images/upload.ts @@ -12,6 +12,7 @@ import cliSpinners from "cli-spinners"; import ora, { type Ora } from "ora"; import { getAuthToken, loadConfig } from "../../helpers/config.ts"; import { logAndQuit } from "../../helpers/errors.ts"; +import { getDefaultWorkspace } from "./utils.ts"; async function readChunk( filePath: string, @@ -84,11 +85,13 @@ const upload = new Command("upload") preparingSpinner = ora(`Preparing upload for ${name}...`).start(); + const workspace = await getDefaultWorkspace(); + // Create image via v2 API const startResponse = await fetch(`${config.api_url}/v2/images`, { method: "POST", headers: apiHeaders, - body: JSON.stringify({ name }), + body: JSON.stringify({ name, workspace }), }); if (!startResponse.ok) { diff --git a/src/lib/images/utils.ts b/src/lib/images/utils.ts new file mode 100644 index 0000000..a43076a --- /dev/null +++ b/src/lib/images/utils.ts @@ -0,0 +1,18 @@ +import { apiClient } from "../../apiClient.ts"; +import { loadConfig, saveConfig } from "../../helpers/config.ts"; + +export async function getDefaultWorkspace(): Promise { + const config = await loadConfig(); + let accountId = config.account_id; + if (!accountId) { + const client = await apiClient(); + const { data } = await client.GET("/v0/me"); + if (data?.id) { + await saveConfig({ ...config, account_id: data.id }); + accountId = data.id; + } else { + throw new Error("Could not determine account ID. Run 'sf login' first."); + } + } + return `sfc:workspace:${accountId}:default`; +} diff --git a/src/schema.ts b/src/schema.ts index 7b06c12..d567aa8 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -2414,6 +2414,8 @@ export interface components { }; vmorch_StartUploadRequest: { name: components["schemas"]["vmorch_Name"]; + /** @description Workspace URN (e.g. sfc:workspace:{account_id}:default). */ + workspace?: string; }; /** * Format: int64 @@ -4988,6 +4990,8 @@ export interface operations { starting_after?: components["schemas"]["vmorch_ImagesCursor"]; /** @description Cursor for backward pagination. */ ending_before?: components["schemas"]["vmorch_ImagesCursor"]; + /** @description Workspace URN (e.g. sfc:workspace:{account_id}:default). */ + workspace?: string; }; header?: never; path?: never;