| 1 | // Package permissionpreset owns the three user-visible execution permission |
| 2 | // presets. Legacy approval-mode strings are accepted only at compatibility |
| 3 | // boundaries and are normalized before they reach the runtime. |
| 4 | package permissionpreset |
| 5 | |
| 6 | import "strings" |
| 7 | |
| 8 | type Preset string |
| 9 | |
| 10 | const ( |
| 11 | ReadOnly Preset = "read-only" |
| 12 | WorkspaceWrite Preset = "workspace-write" |
| 13 | DangerFullAccess Preset = "danger-full-access" |
| 14 | ) |
| 15 | |
| 16 | // Normalize maps persisted and cross-version values to a runtime preset. |
| 17 | // Legacy YOLO is deliberately migrated to workspace-write: older YOLO skipped |
| 18 | // prompts but did not mean that the filesystem sandbox was disabled. |
| 19 | func Normalize(value string) Preset { |
| 20 | switch strings.ToLower(strings.TrimSpace(value)) { |
| 21 | case string(ReadOnly), "readonly", "read_only", "ask": |
| 22 | return ReadOnly |
| 23 | case string(WorkspaceWrite), "workspace", "workspace_write", "auto", "yolo": |
| 24 | return WorkspaceWrite |
| 25 | case string(DangerFullAccess), "danger_full_access", "full", "full-access", "bypass": |
| 26 | return DangerFullAccess |
| 27 | default: |
| 28 | return ReadOnly |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // NormalizeDefault applies the zero-configuration default for newly-created |
| 33 | // sessions while keeping Normalize's conservative behavior for restored data. |
| 34 | func NormalizeDefault(value string) Preset { |
| 35 | if strings.TrimSpace(value) == "" { |
| 36 | return WorkspaceWrite |
| 37 | } |
| 38 | return Normalize(value) |
| 39 | } |
| 40 | |
| 41 | func Valid(value string) bool { |
| 42 | switch strings.ToLower(strings.TrimSpace(value)) { |
| 43 | case string(ReadOnly), string(WorkspaceWrite), string(DangerFullAccess): |
| 44 | return true |
| 45 | default: |
| 46 | return false |
| 47 | } |
| 48 | } |
| 49 |