main.ts
This file
Path
apps/cli/src/main.ts
180 lines · 5.5 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–2why, one step back1import { Brand } from "@repo/brand";2import { ApprovalNeeded, MissingValue, Reported } from "@repo/protocol/cli";Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 3why, one step back3import { ExitCode } from "@repo/protocol/exit-code";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 4–21why, one step back4import { Unauthenticated } from "@repo/protocol/principal";5import { Refusal } from "@repo/protocol/refusal";6import { Console, Effect, Exit, Layer, Option, Runtime, Schema } from "effect";7import { CliConfig, CliError, Command, GlobalFlag } from "effect/cli";89import { credential } from "./credential-helper.ts";10import { CredentialsFile } from "./credentials.ts";11import { hook } from "./hook.ts";12import { refuseStrangeFlags } from "./local/env-set.ts";13import { holdStdout, Output, OutputLive } from "./output.ts";14import { CurrentMode, Policy } from "./policy.ts";15import {16 isTransportFailure,17 noPlatformOf,18 RemoteClient,19 transportMessage,20} from "./remote.ts";21import { commands, mcp, UnknownCommand } from "./surfaces.ts";Merge build/1104 into mainoscabrielbefore adoption82cc398
line 22why, one step back22import { cliVersion } from "./version.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 23–37why, one step back2324export const banner = [25 "Start here:",26 ` ${Brand.cli} asks list the open asks you could take`,27 ` ${Brand.cli} claim take one; you get a fork to push your answer to`,28 ` ${Brand.cli} check run the gate's checks locally before you push`,29].join("\n");3031const tree = [...commands, mcp, hook, credential];3233export const root = Command.make(Brand.cli).pipe(34 Command.withDescription(banner),35 Command.withSubcommands(tree)36);37Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 38why, one step back38const USAGE = ExitCode.usage;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 39–85why, one step back3940export class Misused extends Schema.TaggedError<Misused>()("Misused", {}) {41 override readonly [Runtime.errorExitCode] = USAGE;42 override readonly [Runtime.errorReported] = false;43}4445const isRefusal = Schema.is(Refusal);4647const isReported = Schema.is(Reported);4849const isUnauthenticated = Schema.is(Unauthenticated);5051const isSharedUsage = Schema.is(52 Schema.Union([ApprovalNeeded, MissingValue, UnknownCommand])53);5455const signIn = [56 "Unauthenticated: the platform does not know who you are",57 ` next: ${Brand.cli} login`,58 " retryable: no",59].join("\n");6061type Failure = Command.Error<typeof root> | CliError.CliError;6263const isUsage = (failure: Failure) =>64 isSharedUsage(failure) || Runtime.getErrorExitCode(failure) === USAGE;6566const usageLine = (failure: Failure): Option.Option<string> => {67 if (CliError.isCliError(failure)) {68 return Option.some(69 Schema.is(CliError.ShowHelp)(failure)70 ? failure.errors.map((error) => error.message).join("; ")71 : failure.message72 );73 }7475 if (isUsage(failure) || Schema.isSchemaError(failure)) {76 return Option.some(failure.message);77 }7879 return isTransportFailure(failure)80 ? Option.map(noPlatformOf(failure), ({ message }) => message)81 : Option.none();82};8384const noteFor = (failure: Failure) => {85 if (isUnauthenticated(failure)) {Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 86why, one step back86 return { exitCode: ExitCode.noPerm, note: signIn };Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 87–89why, one step back87 }8889 if (isTransportFailure(failure)) {Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 90–93why, one step back90 return {91 exitCode: ExitCode.unavailable,92 note: `could not reach the platform: ${transportMessage(failure)}`,93 };Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 94–95why, one step back94 }95Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 96why, one step back96 return { exitCode: ExitCode.failed, note: String(failure) };Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 97–98why, one step back97};98Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 99–108why, one step back99const printed = (failure: Failure): Option.Option<Reported> => {100 if (isReported(failure)) {101 return Option.some(failure);102 }103104 return isRefusal(failure)105 ? Option.some(Reported.of([failure]))106 : Option.none();107};108Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 109–113why, one step back109const settle = (110 failure: Failure111): Effect.Effect<never, Reported | Misused, Output> =>112 Option.match(usageLine(failure), {113 onNone: () =>Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 114–123why, one step back114 Option.match(printed(failure), {115 onNone: () => {116 const { exitCode, note } = noteFor(failure);117118 return Output.use((output) => output.note(note)).pipe(119 Effect.andThen(Effect.fail(new Reported({ exitCode })))120 );121 },122 onSome: Effect.fail,123 }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 124–132why, one step back124 onSome: (line) =>125 Output.use((output) => output.note(line)).pipe(126 Effect.andThen(Effect.fail(new Misused()))127 ),128 });129130const isMisuse = (failure: Failure) =>131 Schema.is(CliError.ShowHelp)(failure) && failure.errors.length > 0;132Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 133why, one step back133/** The wizard prompts, so only a person at a terminal gets it. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 134–139why, one step back134const builtInsFor = (policy: Option.Option<Policy["Service"]>) =>135 Option.exists(policy, ({ mode }) => mode !== "human")136 ? GlobalFlag.BuiltIns.filter((flag) => flag !== GlobalFlag.Wizard)137 : GlobalFlag.BuiltIns;138139/**Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
lines 140–141why, one step back140 * A defect is not caught: the runtime prints it. `env set`'s words are screened141 * before effect/cli parses them, so a value typed where a flag goes is never printed back.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 142–152why, one step back142 */143export const program = (argv: readonly string[]) =>144 Effect.gen(function* () {145 yield* refuseStrangeFlags(argv);146147 const policy = yield* Effect.serviceOption(Policy);148 const stdout = holdStdout(yield* Console.Console);149150 const exit = yield* Effect.exit(151 Command.runWith(root.pipe(Command.provideEffectDiscard(stdout.open)), {152 renderErrors: false,Merge build/1104 into mainoscabrielbefore adoption82cc398
line 153why, one step back153 version: cliVersion,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 154–180why, one step back154 })(argv).pipe(155 Effect.provideService(156 CliConfig.CliConfig,157 CliConfig.make({ builtIns: builtInsFor(policy) })158 ),159 Effect.provideService(Console.Console, stdout.console)160 )161 );162163 if (!Option.exists(Exit.findErrorOption(exit), isMisuse)) {164 yield* stdout.release;165 }166167 return yield* exit;168 }).pipe(169 Effect.catchTag("ShowHelp", (help) =>170 help.errors.length === 0 ? Effect.void : Effect.fail(help)171 ),172 Effect.matchEffect({173 onFailure: (failure) => settle(failure),174 onSuccess: () => Effect.void,175 })176 );177178export const CliServices = Layer.mergeAll(OutputLive, RemoteClient).pipe(179 Layer.provideMerge(Layer.mergeAll(CurrentMode, CredentialsFile))180);