Skip to content

Commit 3302674

Browse files
sergicalclaude
andcommitted
refactor: extract shared SortDirection and parseSort to arg-parsing
Moves the duplicated SortDirection type, VALID_SORT_DIRECTIONS constant, and parseSort function from log/list.ts and trace/logs.ts into the shared arg-parsing module. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3af1de9 commit 3302674

3 files changed

Lines changed: 30 additions & 36 deletions

File tree

src/commands/log/list.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import * as Sentry from "@sentry/node-core/light";
1111
import type { SentryContext } from "../../context.js";
1212
import { listLogs, listTraceLogs } from "../../lib/api-client.js";
13-
import { validateLimit } from "../../lib/arg-parsing.js";
13+
import {
14+
parseSort,
15+
type SortDirection,
16+
validateLimit,
17+
} from "../../lib/arg-parsing.js";
1418
import { AuthError, stringifyUnknown } from "../../lib/errors.js";
1519
import {
1620
buildLogRowCells,
@@ -41,9 +45,6 @@ import {
4145
} from "../../lib/trace-target.js";
4246
import { getUpdateNotification } from "../../lib/version-check.js";
4347

44-
/** Sort direction for log output */
45-
type SortDirection = "newest" | "oldest";
46-
4748
type ListFlags = {
4849
readonly limit: number;
4950
readonly query?: string;
@@ -101,20 +102,6 @@ function parseLimit(value: string): number {
101102
return validateLimit(value, MIN_LIMIT, MAX_LIMIT);
102103
}
103104

104-
/** Valid sort direction values */
105-
const VALID_SORT_DIRECTIONS: readonly SortDirection[] = ["newest", "oldest"];
106-
107-
/**
108-
* Parse --sort flag value.
109-
* @throws Error if value is not "newest" or "oldest"
110-
*/
111-
function parseSort(value: string): SortDirection {
112-
if (!VALID_SORT_DIRECTIONS.includes(value as SortDirection)) {
113-
throw new Error(`--sort must be "newest" or "oldest", got "${value}"`);
114-
}
115-
return value as SortDirection;
116-
}
117-
118105
/**
119106
* Parse --follow flag value.
120107
* Supports: -f (empty string → default interval), -f 10 (explicit interval)

src/commands/trace/logs.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
import type { SentryContext } from "../../context.js";
88
import { listTraceLogs } from "../../lib/api-client.js";
9-
import { validateLimit } from "../../lib/arg-parsing.js";
9+
import {
10+
parseSort,
11+
type SortDirection,
12+
validateLimit,
13+
} from "../../lib/arg-parsing.js";
1014
import { openInBrowser } from "../../lib/browser.js";
1115
import { buildCommand } from "../../lib/command.js";
1216
import { filterFields } from "../../lib/formatters/json.js";
@@ -25,9 +29,6 @@ import {
2529
warnIfNormalized,
2630
} from "../../lib/trace-target.js";
2731

28-
/** Sort direction for log output */
29-
type SortDirection = "newest" | "oldest";
30-
3132
type LogsFlags = {
3233
readonly json: boolean;
3334
readonly web: boolean;
@@ -91,20 +92,6 @@ function parseLimit(value: string): number {
9192
return validateLimit(value, 1, MAX_LIMIT);
9293
}
9394

94-
/** Valid sort direction values */
95-
const VALID_SORT_DIRECTIONS: readonly SortDirection[] = ["newest", "oldest"];
96-
97-
/**
98-
* Parse --sort flag value.
99-
* @throws Error if value is not "newest" or "oldest"
100-
*/
101-
function parseSort(value: string): SortDirection {
102-
if (!VALID_SORT_DIRECTIONS.includes(value as SortDirection)) {
103-
throw new Error(`--sort must be "newest" or "oldest", got "${value}"`);
104-
}
105-
return value as SortDirection;
106-
}
107-
10895
export const logsCommand = buildCommand({
10996
docs: {
11097
brief: "View logs associated with a trace",

src/lib/arg-parsing.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ export function validateLimit(value: string, min = 1, max = 1000): number {
261261
return num;
262262
}
263263

264+
// ---------------------------------------------------------------------------
265+
// Sort direction parsing (shared by log list, trace logs)
266+
// ---------------------------------------------------------------------------
267+
268+
/** Sort direction for log output */
269+
export type SortDirection = "newest" | "oldest";
270+
271+
const VALID_SORT_DIRECTIONS: readonly SortDirection[] = ["newest", "oldest"];
272+
273+
/**
274+
* Parse --sort flag value.
275+
* @throws Error if value is not "newest" or "oldest"
276+
*/
277+
export function parseSort(value: string): SortDirection {
278+
if (!VALID_SORT_DIRECTIONS.includes(value as SortDirection)) {
279+
throw new Error(`--sort must be "newest" or "oldest", got "${value}"`);
280+
}
281+
return value as SortDirection;
282+
}
283+
264284
/** Default span depth when no value is provided */
265285
const DEFAULT_SPAN_DEPTH = 3;
266286

0 commit comments

Comments
 (0)