| 1 | /** |
| 2 | * Shared flag processing utilities to eliminate duplication between TUI and headless modes |
| 3 | */ |
| 4 | |
| 5 | import { PermissionMode } from "../permissions/types.js"; |
| 6 | |
| 7 | export interface PermissionOverrides { |
| 8 | allow?: string[]; |
| 9 | ask?: string[]; |
| 10 | exclude?: string[]; |
| 11 | mode?: PermissionMode; |
| 12 | } |
| 13 | |
| 14 | export interface ProcessedFlags { |
| 15 | mode?: PermissionMode; |
| 16 | permissionOverrides: PermissionOverrides; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Converts legacy readonly/auto flags to the new mode system |
| 21 | * This logic was previously duplicated in chat.ts lines 168-173 and 259-264 |
| 22 | */ |
| 23 | export function convertLegacyModeFlags( |
| 24 | readonly?: boolean, |
| 25 | auto?: boolean, |
| 26 | ): PermissionMode | undefined { |
| 27 | if (readonly && auto) { |
| 28 | throw new Error("Cannot use both --readonly and --auto flags together"); |
| 29 | } |
| 30 | |
| 31 | if (readonly) { |
| 32 | return "plan"; |
| 33 | } |
| 34 | |
| 35 | if (auto) { |
| 36 | return "auto"; |
| 37 | } |
| 38 | |
| 39 | return undefined; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Structures permission overrides for service initialization |
| 44 | * This logic was previously duplicated between TUI and headless paths |
| 45 | */ |
| 46 | export function buildPermissionOverrides( |
| 47 | allow?: string[], |
| 48 | ask?: string[], |
| 49 | exclude?: string[], |
| 50 | mode?: PermissionMode, |
| 51 | ): PermissionOverrides { |
| 52 | return { |
| 53 | allow, |
| 54 | ask, |
| 55 | exclude, |
| 56 | mode, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Processes all command line flags into a consistent structure |
| 62 | * Eliminates duplication and provides single source of truth for flag processing |
| 63 | */ |
| 64 | export function processCommandFlags(options: { |
| 65 | readonly?: boolean; |
| 66 | auto?: boolean; |
| 67 | allow?: string[]; |
| 68 | ask?: string[]; |
| 69 | exclude?: string[]; |
| 70 | }): ProcessedFlags { |
| 71 | // Convert legacy flags to mode |
| 72 | const mode = convertLegacyModeFlags(options.readonly, options.auto); |
| 73 | |
| 74 | // Build permission overrides |
| 75 | const permissionOverrides = buildPermissionOverrides( |
| 76 | options.allow, |
| 77 | options.ask, |
| 78 | options.exclude, |
| 79 | mode, |
| 80 | ); |
| 81 | |
| 82 | return { |
| 83 | mode, |
| 84 | permissionOverrides, |
| 85 | }; |
| 86 | } |