File size: 2,255 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {
  appendGatewayLifecycleAuditLog,
  type GatewayLifecycleAuditSource,
} from "../../daemon/restart-logs.js";
import { createGatewayLifecycleMutationReporter } from "../../daemon/service-mutation.js";
/** Gateway lifecycle audit helpers shared by managed and unmanaged CLI paths. */
import type {
  GatewayLifecycleMutation,
  GatewayLifecycleMutationMode,
} from "../../daemon/service-types.js";
import { isTerminalInteractive } from "../terminal-interactivity.js";

type GatewayLifecycleAction = "start" | "stop" | "restart";

export function appendGatewayLifecycleAudit(params: {
  action: GatewayLifecycleAction;
  source: GatewayLifecycleAuditSource;
  mode: GatewayLifecycleMutationMode;
  pid?: number;
  env?: NodeJS.ProcessEnv;
}): void {
  appendGatewayLifecycleAuditLog(params.env ?? process.env, {
    action: params.action,
    source: params.source,
    mode: params.mode,
    ...(params.pid === undefined ? {} : { pid: params.pid }),
    interactive: isTerminalInteractive(),
  });
}

export function createGatewayLifecycleMutationAudit(params: {
  action: GatewayLifecycleAction;
  source?: GatewayLifecycleAuditSource;
  env?: NodeJS.ProcessEnv;
}): (mutation: GatewayLifecycleMutation) => void {
  const reportMutation = createGatewayLifecycleMutationReporter((mutation) => {
    appendGatewayLifecycleAudit({
      action: params.action,
      source: params.source ?? "cli",
      mode: mutation.mode,
      ...(params.env === undefined ? {} : { env: params.env }),
    });
  });
  return (mutation) => reportMutation(mutation.mode);
}

export function createServiceLifecycleMutationAudit(params: {
  serviceNoun: string;
  action: GatewayLifecycleAction;
}): ((mutation: GatewayLifecycleMutation) => void) | undefined {
  return params.serviceNoun === "Gateway"
    ? createGatewayLifecycleMutationAudit({ action: params.action })
    : undefined;
}

export function appendServiceLifecycleRepairAudit(params: {
  serviceNoun: string;
  action: "start" | "restart";
  pid?: number;
}): void {
  if (params.serviceNoun !== "Gateway") {
    return;
  }
  appendGatewayLifecycleAudit({
    action: params.action,
    source: "cli",
    mode: "service-repair",
    ...(params.pid === undefined ? {} : { pid: params.pid }),
  });
}