|
| 1 | +import { Either, Match } from "effect" |
| 2 | + |
| 3 | +import { |
| 4 | + type ParseError, |
| 5 | + type SessionGistBackupCommand, |
| 6 | + type SessionGistCommand, |
| 7 | + type SessionGistDownloadCommand, |
| 8 | + type SessionGistListCommand, |
| 9 | + type SessionGistViewCommand |
| 10 | +} from "@effect-template/lib/core/domain" |
| 11 | + |
| 12 | +import { parsePositiveInt, parseProjectDirWithOptions, splitSubcommand } from "./parser-shared.js" |
| 13 | + |
| 14 | +// CHANGE: parse session backup commands for backup/list/view/download |
| 15 | +// WHY: enables CLI access to session backup repository functionality |
| 16 | +// QUOTE(ТЗ): "иметь возможность возвращаться ко всем старым сессиям с агентами" |
| 17 | +// REF: issue-143 |
| 18 | +// PURITY: CORE |
| 19 | +// EFFECT: Either<SessionGistCommand, ParseError> |
| 20 | +// INVARIANT: all subcommands are deterministically parsed |
| 21 | +// COMPLEXITY: O(n) where n = |args| |
| 22 | + |
| 23 | +const defaultLimit = 20 |
| 24 | +const defaultOutputDir = "./.session-restore" |
| 25 | + |
| 26 | +const missingSnapshotRefError: ParseError = { _tag: "MissingRequiredOption", option: "snapshot-ref" } |
| 27 | + |
| 28 | +const extractSnapshotRef = (args: ReadonlyArray<string>): string | null => { |
| 29 | + const snapshotRef = args[0] |
| 30 | + return snapshotRef && !snapshotRef.startsWith("-") ? snapshotRef : null |
| 31 | +} |
| 32 | + |
| 33 | +const parseBackup = ( |
| 34 | + args: ReadonlyArray<string> |
| 35 | +): Either.Either<SessionGistBackupCommand, ParseError> => |
| 36 | + Either.map(parseProjectDirWithOptions(args), ({ projectDir, raw }) => ({ |
| 37 | + _tag: "SessionGistBackup", |
| 38 | + projectDir, |
| 39 | + prNumber: raw.prNumber ? Number.parseInt(raw.prNumber, 10) : null, |
| 40 | + repo: raw.repo ?? null, |
| 41 | + postComment: raw.noComment !== true |
| 42 | + })) |
| 43 | + |
| 44 | +const parseList = ( |
| 45 | + args: ReadonlyArray<string> |
| 46 | +): Either.Either<SessionGistListCommand, ParseError> => |
| 47 | + Either.gen(function*(_) { |
| 48 | + const { raw } = yield* _(parseProjectDirWithOptions(args)) |
| 49 | + const limit = raw.limit |
| 50 | + ? yield* _(parsePositiveInt("--limit", raw.limit)) |
| 51 | + : defaultLimit |
| 52 | + return { |
| 53 | + _tag: "SessionGistList", |
| 54 | + limit, |
| 55 | + repo: raw.repo ?? null |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | +const parseView = ( |
| 60 | + args: ReadonlyArray<string> |
| 61 | +): Either.Either<SessionGistViewCommand, ParseError> => { |
| 62 | + const snapshotRef = extractSnapshotRef(args) |
| 63 | + return snapshotRef |
| 64 | + ? Either.right({ _tag: "SessionGistView", snapshotRef }) |
| 65 | + : Either.left(missingSnapshotRefError) |
| 66 | +} |
| 67 | + |
| 68 | +const parseDownload = ( |
| 69 | + args: ReadonlyArray<string> |
| 70 | +): Either.Either<SessionGistDownloadCommand, ParseError> => { |
| 71 | + const snapshotRef = extractSnapshotRef(args) |
| 72 | + if (!snapshotRef) { |
| 73 | + return Either.left(missingSnapshotRefError) |
| 74 | + } |
| 75 | + return Either.map(parseProjectDirWithOptions(args.slice(1)), ({ raw }) => ({ |
| 76 | + _tag: "SessionGistDownload", |
| 77 | + snapshotRef, |
| 78 | + outputDir: raw.output ?? defaultOutputDir |
| 79 | + })) |
| 80 | +} |
| 81 | + |
| 82 | +const unknownActionError = (action: string): ParseError => ({ |
| 83 | + _tag: "InvalidOption", |
| 84 | + option: "session-gists", |
| 85 | + reason: `unknown action ${action}` |
| 86 | +}) |
| 87 | + |
| 88 | +export const parseSessionGists = ( |
| 89 | + args: ReadonlyArray<string> |
| 90 | +): Either.Either<SessionGistCommand, ParseError> => { |
| 91 | + const { rest, subcommand } = splitSubcommand(args) |
| 92 | + if (subcommand === null) { |
| 93 | + return parseList(args) |
| 94 | + } |
| 95 | + |
| 96 | + return Match.value(subcommand).pipe( |
| 97 | + Match.when("backup", () => parseBackup(rest)), |
| 98 | + Match.when("list", () => parseList(rest)), |
| 99 | + Match.when("view", () => parseView(rest)), |
| 100 | + Match.when("download", () => parseDownload(rest)), |
| 101 | + Match.orElse(() => Either.left(unknownActionError(subcommand))) |
| 102 | + ) |
| 103 | +} |
0 commit comments