Open History to see the last commit
1import { BadArgument, SystemError } from "effect/PlatformError";
2import * as Stream from "effect/Stream";
3import {
4 collectRedactedSecrets,
5 redactionMarker,
6 redactStream,
7 redactText,
8} from "../Util/Redaction.ts";
9import type { CommandProps } from "./Command.ts";
10
11export interface CommandRedactor {
12 readonly redact: (value: string) => string;
13 readonly stream: <E, R>(stream: Stream.Stream<string, E, R>) => Stream.Stream<string, E, R>;
14}
15
16/** Redacts the `Redacted` env values of a command from its output and errors. */
17export const makeCommandRedactor = (env: CommandProps["env"]): CommandRedactor => {
18 const secrets = collectRedactedSecrets(env ?? {});
19 const marker = redactionMarker(secrets, "[REDACTED]");
20 return {
21 redact: (value) => redactText(value, secrets, marker),
22 stream: (stream) => redactStream(stream, secrets, marker),
23 };
24};
25
26export const redactPlatformReason = (
27 reason: BadArgument | SystemError,
28 redactor: CommandRedactor,
29): BadArgument | SystemError => {
30 if (reason instanceof BadArgument) {
31 return new BadArgument({
32 module: redactor.redact(reason.module),
33 method: redactor.redact(reason.method),
34 description:
35 reason.description === undefined ? undefined : redactor.redact(reason.description),
36 });
37 }
38
39 return new SystemError({
40 _tag: reason._tag,
41 module: redactor.redact(reason.module),
42 method: redactor.redact(reason.method),
43 description: reason.description === undefined ? undefined : redactor.redact(reason.description),
44 syscall: reason.syscall === undefined ? undefined : redactor.redact(reason.syscall),
45 pathOrDescriptor:
46 typeof reason.pathOrDescriptor === "string"
47 ? redactor.redact(reason.pathOrDescriptor)
48 : reason.pathOrDescriptor,
49 });
50};