-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.ts
More file actions
152 lines (146 loc) · 5.26 KB
/
app.ts
File metadata and controls
152 lines (146 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// biome-ignore lint/performance/noNamespaceImport: Sentry SDK recommends namespace import
import * as Sentry from "@sentry/bun";
import {
type ApplicationText,
buildApplication,
buildRouteMap,
text_en,
UnexpectedPositionalError,
} from "@stricli/core";
import { apiCommand } from "./commands/api.js";
import { authRoute } from "./commands/auth/index.js";
import { whoamiCommand } from "./commands/auth/whoami.js";
import { cliRoute } from "./commands/cli/index.js";
import { eventRoute } from "./commands/event/index.js";
import { helpCommand } from "./commands/help.js";
import { initCommand } from "./commands/init.js";
import { issueRoute } from "./commands/issue/index.js";
import { listCommand as issueListCommand } from "./commands/issue/list.js";
import { logRoute } from "./commands/log/index.js";
import { listCommand as logListCommand } from "./commands/log/list.js";
import { orgRoute } from "./commands/org/index.js";
import { listCommand as orgListCommand } from "./commands/org/list.js";
import { projectRoute } from "./commands/project/index.js";
import { listCommand as projectListCommand } from "./commands/project/list.js";
import { repoRoute } from "./commands/repo/index.js";
import { listCommand as repoListCommand } from "./commands/repo/list.js";
import { teamRoute } from "./commands/team/index.js";
import { listCommand as teamListCommand } from "./commands/team/list.js";
import { traceRoute } from "./commands/trace/index.js";
import { listCommand as traceListCommand } from "./commands/trace/list.js";
import { CLI_VERSION } from "./lib/constants.js";
import {
AuthError,
CliError,
getExitCode,
stringifyUnknown,
} from "./lib/errors.js";
import { error as errorColor, warning } from "./lib/formatters/colors.js";
/**
* Plural alias → singular route name mapping.
* Used to suggest the correct command when users type e.g. `sentry projects view cli`.
*/
const PLURAL_TO_SINGULAR: Record<string, string> = {
issues: "issue",
orgs: "org",
projects: "project",
repos: "repo",
teams: "team",
logs: "log",
traces: "trace",
};
/** Top-level route map containing all CLI commands */
export const routes = buildRouteMap({
routes: {
help: helpCommand,
auth: authRoute,
cli: cliRoute,
org: orgRoute,
project: projectRoute,
repo: repoRoute,
team: teamRoute,
issue: issueRoute,
event: eventRoute,
log: logRoute,
trace: traceRoute,
init: initCommand,
api: apiCommand,
issues: issueListCommand,
orgs: orgListCommand,
projects: projectListCommand,
repos: repoListCommand,
teams: teamListCommand,
logs: logListCommand,
traces: traceListCommand,
whoami: whoamiCommand,
},
defaultCommand: "help",
docs: {
brief: "A gh-like CLI for Sentry",
fullDescription:
"sentry is a command-line interface for interacting with Sentry. " +
"It provides commands for authentication, viewing issues, and making API calls.",
},
});
/**
* Custom error formatting for CLI errors.
*
* - AuthError (not_authenticated): Re-thrown to allow auto-login flow in bin.ts
* - Other CliError subclasses: Show clean user-friendly message without stack trace
* - Other errors: Show stack trace for debugging unexpected issues
*/
const customText: ApplicationText = {
...text_en,
exceptionWhileParsingArguments: (
exc: unknown,
ansiColor: boolean
): string => {
// When a plural alias receives extra positional args (e.g. `sentry projects view cli`),
// Stricli throws UnexpectedPositionalError because the list command only accepts 1 arg.
// Detect this and suggest the singular form.
if (exc instanceof UnexpectedPositionalError) {
const args = process.argv.slice(2);
const firstArg = args[0];
if (firstArg && firstArg in PLURAL_TO_SINGULAR) {
const singular = PLURAL_TO_SINGULAR[firstArg];
const rest = args.slice(1).join(" ");
const hint = ansiColor
? warning(`\nDid you mean: sentry ${singular} ${rest}\n`)
: `\nDid you mean: sentry ${singular} ${rest}\n`;
return `${text_en.exceptionWhileParsingArguments(exc, ansiColor)}${hint}`;
}
}
return text_en.exceptionWhileParsingArguments(exc, ansiColor);
},
exceptionWhileRunningCommand: (exc: unknown, ansiColor: boolean): string => {
// Re-throw AuthError("not_authenticated") for auto-login flow in bin.ts
// Don't capture to Sentry - it's an expected state (user not logged in), not an error
if (exc instanceof AuthError && exc.reason === "not_authenticated") {
throw exc;
}
// Report command errors to Sentry. Stricli catches exceptions and doesn't
// re-throw, so we must capture here to get visibility into command failures.
Sentry.captureException(exc);
if (exc instanceof CliError) {
const prefix = ansiColor ? errorColor("Error:") : "Error:";
return `${prefix} ${exc.format()}`;
}
if (exc instanceof Error) {
return `Unexpected error: ${exc.stack ?? exc.message}`;
}
return `Unexpected error: ${stringifyUnknown(exc)}`;
},
};
export const app = buildApplication(routes, {
name: "sentry",
versionInfo: {
currentVersion: CLI_VERSION,
},
scanner: {
caseStyle: "allow-kebab-for-camel",
},
determineExitCode: getExitCode,
localization: {
loadText: () => customText,
},
});