Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/cli/src/commands/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ export async function readJsonObject(
return parsed as Record<string, unknown>;
}

/**
* Keep only the writable keys of a parsed `--file` body, dropping the output-only
* fields (`name`, `*Eligible`, `dataSourceId`, …) the API rejects in a PATCH
* `updateMask` — so a body saved from a `get` can be re-applied as-is. `fields` is
* constrained to keys of `T`, so the allowlist can't drift from the return type.
* Shared by the accounts / regions / datasources write commands; mirrors how
* `toProductInput` strips output-only product data.
*/
export function pick<T>(obj: Record<string, unknown>, fields: readonly (keyof T & string)[]): T {
const out: Record<string, unknown> = {};
for (const key of fields) {
if (key in obj) out[key] = obj[key];
}
return out as T;
}

/** A feed file that couldn't be read or parsed as a product input. */
export interface FileLoadFailure {
file: string;
Expand Down
16 changes: 1 addition & 15 deletions packages/cli/src/commands/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
type CustomerService,
} from "@gmc-cli/api";
import { contextFrom, wantsJson } from "../context.js";
import { clientFor, resolveAccount, line, readJsonObject } from "./_shared.js";
import { clientFor, resolveAccount, line, readJsonObject, pick } from "./_shared.js";

function accountIdOf(account: Account): string {
return account.accountId ?? account.name.replace(/^accounts\//, "");
Expand Down Expand Up @@ -128,20 +128,6 @@ const AUTOFEED_FIELDS = [
"enableProducts",
] as const satisfies readonly (keyof AutofeedSettingsInput)[];

/**
* Keep only the writable keys of a parsed `--file` body, dropping output-only fields
* the API rejects in a PATCH `updateMask`. Mirrors `pickWritable` in `regions.ts`, so a
* body saved from `accounts get`/`info` can be re-applied as-is. `fields` is constrained
* to keys of `T`, so the field list can't drift from the return type.
*/
function pick<T>(obj: Record<string, unknown>, fields: readonly (keyof T & string)[]): T {
const out: Record<string, unknown> = {};
for (const key of fields) {
if (key in obj) out[key] = obj[key];
}
return out as T;
}

function renderHomepage(homepage: Homepage): void {
line("URI", homepage.uri ?? "—");
line("Claimed", homepage.claimed ? "yes" : "no");
Expand Down
20 changes: 5 additions & 15 deletions packages/cli/src/commands/datasources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type PrimaryProductDataSource,
} from "@gmc-cli/api";
import { contextFrom, wantsJson } from "../context.js";
import { clientFor, resolveAccount, readJsonObject, line } from "./_shared.js";
import { clientFor, resolveAccount, readJsonObject, line, pick } from "./_shared.js";

interface CreateFlags {
name?: string;
Expand Down Expand Up @@ -175,19 +175,6 @@ const DATASOURCE_WRITABLE_FIELDS = [
"fileInput",
] as const satisfies readonly (keyof DataSource)[];

/**
* Keep only the writable keys of a parsed `--file` body, dropping the output-only
* `name`/`dataSourceId`/`input` the API rejects in a PATCH `updateMask`. Mirrors
* `pickWritable` in `regions.ts`, so a body saved from `datasources get` re-applies cleanly.
*/
function pickWritable(obj: Record<string, unknown>): DataSource {
const out: Record<string, unknown> = {};
for (const key of DATASOURCE_WRITABLE_FIELDS) {
if (key in obj) out[key] = obj[key];
}
return out as DataSource;
}

function dataSourceType(ds: DataSource): string {
if (ds.primaryProductDataSource) return "primary product";
if (ds.supplementalProductDataSource) return "supplemental";
Expand Down Expand Up @@ -347,7 +334,10 @@ export function registerDataSourcesCommands(program: Command): void {
// `update <id> --name X` doesn't block on stdin in a non-TTY/CI context).
let body: DataSource = {};
if (opts.file || (opts.name === undefined && !process.stdin.isTTY)) {
body = pickWritable(await readJsonObject(opts.file, "data source"));
body = pick<DataSource>(
await readJsonObject(opts.file, "data source"),
DATASOURCE_WRITABLE_FIELDS,
);
}
if (opts.name !== undefined) body.displayName = opts.name;
if (Object.keys(body).length === 0) {
Expand Down
25 changes: 8 additions & 17 deletions packages/cli/src/commands/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type GeoTargetArea,
} from "@gmc-cli/api";
import { contextFrom, wantsJson } from "../context.js";
import { clientFor, resolveAccount, line, readJsonObject, parsePageSize } from "./_shared.js";
import { clientFor, resolveAccount, line, readJsonObject, parsePageSize, pick } from "./_shared.js";

interface RegionWriteOpts {
displayName?: string;
Expand Down Expand Up @@ -69,21 +69,12 @@ function parseGeotargetIds(raw: string): GeoTargetArea {
}

/** The writable Region fields — everything else (`name`, the `*Eligible` flags) is output-only. */
const WRITABLE_FIELDS = ["displayName", "postalCodeArea", "geotargetArea", "radiusArea"] as const;

/**
* Keep only the writable fields of a parsed `--file` body. A body saved from
* `regions get` carries output-only `name` / `*Eligible` flags, which the API rejects
* in a PATCH `updateMask` (and shouldn't be sent on a write) — allowlisting drops them,
* mirroring how `toProductInput` strips output-only product data.
*/
function pickWritable(obj: Record<string, unknown>): RegionInput {
const out: Record<string, unknown> = {};
for (const key of WRITABLE_FIELDS) {
if (key in obj) out[key] = obj[key];
}
return out as RegionInput;
}
const WRITABLE_FIELDS = [
"displayName",
"postalCodeArea",
"geotargetArea",
"radiusArea",
] as const satisfies readonly (keyof RegionInput)[];

/**
* Build a RegionInput from `--file` (JSON base) overlaid with the convenience flags.
Expand All @@ -92,7 +83,7 @@ function pickWritable(obj: Record<string, unknown>): RegionInput {
*/
async function buildRegionInput(opts: RegionWriteOpts, requireArea: boolean): Promise<RegionInput> {
const input: RegionInput = opts.file
? pickWritable(await readJsonObject(opts.file, "region"))
? pick<RegionInput>(await readJsonObject(opts.file, "region"), WRITABLE_FIELDS)
: {};
if (opts.regionCode && !opts.postalCodes) {
throw new UsageError(
Expand Down