Open History to see the last commit
1import * as Console from "effect/Console";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as References from "effect/References";
7import { Argument, Command, GlobalFlag } from "effect/cli";
8import * as CliError from "effect/cli/CliError";
9
10import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
11import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
12import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
13
14const describePolicy = (
15 policy: ManagedPolicy.ManagedPolicy["Service"],
16 loaded: Effect.Success<ReturnType<typeof ManagedPolicy.loadedPolicy>>,
17) => {
18 const { document } = policy;
19 if (document === undefined || loaded === null) {
20 return "No managed policy on this machine. Every setting belongs to the user.\n";
21 }
22 const settings = Object.entries(document.settings ?? {});
23 const lines = [
24 `Managed by ${document.organization} (${policy.path}, fingerprint ${loaded.fingerprint}).`,
25 ...(document.supportUrl === undefined ? [] : [`Support: ${document.supportUrl}`]),
26 "",
27 settings.length === 0 ? "No managed settings." : "Settings:",
28 ...settings.map(
29 ([path, setting]) =>
30 ` ${path} = ${JSON.stringify(setting.value)} (${setting.locked ? "locked" : "default"})`,
31 ),
32 ];
33 if (document.limits !== undefined) {
34 lines.push("", "Limits:", JSON.stringify(document.limits, null, 2));
35 }
36 return `${lines.join("\n")}\n`;
37};
38
39/** Whether the server running from this T3 home loaded the installed policy, which waits for a restart. */
40export const describeRunningServer = Effect.fn("cli.policy.describeRunningServer")(function* (
41 statePath: string,
42 installed: Effect.Success<ReturnType<typeof ManagedPolicy.loadedPolicy>>,
43) {
44 const state = yield* readPersistedServerRuntimeState(statePath);
45 if (Option.isNone(state) || !isProcessAlive(state.value.pid)) {
46 return "No T3 Code server is running from this T3 home. The installed policy applies when one starts.";
47 }
48 const { pid, startedAt, managedPolicy: loaded } = state.value;
49 const server = `The running server (pid ${pid}, started ${startedAt})`;
50 if (loaded === undefined) {
51 return `${server} does not report which policy it loaded. Restart it to apply the installed policy.`;
52 }
53 if ((loaded?.fingerprint ?? null) === (installed?.fingerprint ?? null)) {
54 return loaded === null
55 ? `${server} runs without a policy, as installed.`
56 : `${server} loaded this policy.`;
57 }
58 const what =
59 loaded === null
60 ? "no policy"
61 : `${loaded.organization}'s policy from ${loaded.source} (fingerprint ${loaded.fingerprint})`;
62 return `${server} loaded ${what}. Restart T3 Code to apply the installed policy.`;
63});
64
65const showCommand = Command.make("show", projectLocationFlags).pipe(
66 Command.withDescription(
67 "Show the policy installed on this machine, and whether the running server loaded it.",
68 ),
69 Command.withHandler(
70 Effect.fn("cli.policy.show")(function* (flags) {
71 // The command prints the policy itself; the server's startup log line would repeat it.
72 const policy = yield* ManagedPolicy.ManagedPolicy.pipe(
73 Effect.provide(
74 ManagedPolicy.layer.pipe(
75 Layer.provide(Layer.succeed(References.MinimumLogLevel, "Warn")),
76 ),
77 ),
78 );
79 const installed = yield* ManagedPolicy.loadedPolicy(policy);
80 yield* Console.log(describePolicy(policy, installed));
81 const config = yield* resolveCliAuthConfig(flags, yield* GlobalFlag.LogLevel);
82 yield* Console.log(yield* describeRunningServer(config.serverRuntimeStatePath, installed));
83 }),
84 ),
85);
86
87const checkCommand = Command.make("check", {
88 file: Argument.String("file").pipe(Argument.withDescription("Policy file to validate.")),
89}).pipe(
90 Command.withDescription("Validate a policy file before deploying it."),
91 Command.withHandler(
92 Effect.fn("cli.policy.check")(function* ({ file }) {
93 // Startup treats a missing file as no policy; a draft that is not there checks nothing.
94 if (!(yield* (yield* FileSystem.FileSystem).exists(file))) {
95 return yield* new CliError.UserError({
96 cause: file,
97 userMessage: `No policy file at ${file}.`,
98 });
99 }
100 const policy = yield* ManagedPolicy.load(file);
101 yield* Console.log(describePolicy(policy, yield* ManagedPolicy.loadedPolicy(policy)));
102 }),
103 ),
104);
105
106export const policyCommand = Command.make("policy").pipe(
107 Command.withDescription("Inspect the administrator's managed policy."),
108 Command.withSubcommands([showCommand, checkCommand]),
109);