Elffuss BraidHub

kikocis/continue public

main · 5.4 KiBRawPermalink
1/**
2 * Centralized command line flag validation
3 * Provides consistent validation across all commands and proper error reporting
4 */
5
6import { safeStderr } from "../init.js";
7
8export interface ValidationOptions {
9 // Root command specific flags
10 print?: boolean;
11 format?: string;
12 silent?: boolean;
13
14 // Common flags
15 readonly?: boolean;
16 auto?: boolean;
17 config?: string;
18
19 // Session flags
20 resume?: boolean;
21 fork?: string;
22
23 // Permission flags
24 allow?: string[];
25 ask?: string[];
26 exclude?: string[];
27
28 // Command context
29 isRootCommand?: boolean;
30 commandName?: string;
31}
32
33export interface ValidationError {
34 message: string;
35 code: string;
36}
37
38export interface ValidationResult {
39 isValid: boolean;
40 errors: ValidationError[];
41}
42
43/**
44 * Validate that --format flag only works with -p/--print
45 */
46function validateFormatFlag(options: ValidationOptions): ValidationError[] {
47 const errors: ValidationError[] = [];
48
49 if (options.format && !options.print) {
50 errors.push({
51 code: "FORMAT_REQUIRES_PRINT",
52 message: "Error: --format flag can only be used with -p/--print flag",
53 });
54 }
55
56 return errors;
57}
58
59/**
60 * Validate that --silent flag only works with -p/--print
61 */
62function validateSilentFlag(options: ValidationOptions): ValidationError[] {
63 const errors: ValidationError[] = [];
64
65 if (options.silent && !options.print) {
66 errors.push({
67 code: "SILENT_REQUIRES_PRINT",
68 message: "Error: --silent flag can only be used with -p/--print flag",
69 });
70 }
71
72 return errors;
73}
74
75/**
76 * Validate format value is supported
77 */
78function validateFormatValue(options: ValidationOptions): ValidationError[] {
79 const errors: ValidationError[] = [];
80
81 if (options.format && options.format !== "json") {
82 errors.push({
83 code: "INVALID_FORMAT_VALUE",
84 message: "Error: --format currently only supports 'json'",
85 });
86 }
87
88 return errors;
89}
90
91/**
92 * Validate that --readonly and --auto are mutually exclusive
93 * This validation is now handled in flagProcessor.ts, but we keep it here for completeness
94 */
95function validateModeFlags(options: ValidationOptions): ValidationError[] {
96 const errors: ValidationError[] = [];
97
98 if (options.readonly && options.auto) {
99 errors.push({
100 code: "CONFLICTING_MODE_FLAGS",
101 message: "Error: Cannot use both --readonly and --auto flags together",
102 });
103 }
104
105 return errors;
106}
107
108/**
109 * Validate session flags
110 */
111function validateSessionFlags(options: ValidationOptions): ValidationError[] {
112 const errors: ValidationError[] = [];
113
114 if (options.resume && options.fork) {
115 errors.push({
116 code: "CONFLICTING_SESSION_FLAGS",
117 message: "Error: Cannot use both --resume and --fork flags together",
118 });
119 }
120
121 if (options.fork !== undefined && (!options.fork || !options.fork.trim())) {
122 errors.push({
123 code: "FORK_REQUIRES_SESSION_ID",
124 message: "Error: --fork requires a session ID (e.g., --fork abc123)",
125 });
126 }
127
128 return errors;
129}
130
131/**
132 * Validate config path exists (basic check)
133 */
134function validateConfigPath(options: ValidationOptions): ValidationError[] {
135 const errors: ValidationError[] = [];
136
137 // Note: We don't check file existence here since the config loader handles that
138 // and provides better error messages. This is just for basic validation.
139 if (options.config && typeof options.config !== "string") {
140 errors.push({
141 code: "INVALID_CONFIG_PATH",
142 message: "Error: --config must be a valid path",
143 });
144 }
145
146 return errors;
147}
148
149/**
150 * Validate permission flag combinations
151 */
152function validatePermissionFlags(
153 options: ValidationOptions,
154): ValidationError[] {
155 const errors: ValidationError[] = [];
156
157 // Check for empty permission arrays (likely user error)
158 if (options.allow?.some((tool) => !tool.trim())) {
159 errors.push({
160 code: "EMPTY_ALLOW_TOOL",
161 message: "Error: --allow requires a tool name (e.g., --allow readFile)",
162 });
163 }
164
165 if (options.ask?.some((tool) => !tool.trim())) {
166 errors.push({
167 code: "EMPTY_ASK_TOOL",
168 message: "Error: --ask requires a tool name (e.g., --ask writeFile)",
169 });
170 }
171
172 if (options.exclude?.some((tool) => !tool.trim())) {
173 errors.push({
174 code: "EMPTY_EXCLUDE_TOOL",
175 message: "Error: --exclude requires a tool name (e.g., --exclude Write)",
176 });
177 }
178
179 return errors;
180}
181
182/**
183 * Run all validation checks on the provided options
184 */
185export function validateFlags(options: ValidationOptions): ValidationResult {
186 const allErrors: ValidationError[] = [];
187
188 // Run all validation functions
189 const validators = [
190 validateFormatFlag,
191 validateSilentFlag,
192 validateFormatValue,
193 validateModeFlags,
194 validateSessionFlags,
195 validateConfigPath,
196 validatePermissionFlags,
197 ];
198
199 for (const validator of validators) {
200 allErrors.push(...validator(options));
201 }
202
203 return {
204 isValid: allErrors.length === 0,
205 errors: allErrors,
206 };
207}
208
209/**
210 * Display validation errors and exit process
211 * Provides consistent error formatting across all commands
212 */
213export function handleValidationErrors(errors: ValidationError[]): never {
214 for (const error of errors) {
215 safeStderr(error.message + "\n");
216 }
217
218 if (errors.length > 1) {
219 safeStderr(`\nFound ${errors.length} validation errors.\n`);
220 }
221
222 // Use async graceful exit without awaiting (we must satisfy never)
223 import("../util/exit.js").then(({ gracefulExit }) => gracefulExit(1));
224 throw new Error("Exiting due to validation errors");
225}