Repository navigation
Expand file tree
/
Copy pathresolve-cli-options.ts
More file actions
74 lines (71 loc) · 2.58 KB
/
Copy pathresolve-cli-options.ts
File metadata and controls
74 lines (71 loc) · 2.58 KB
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
72
73
74
import type { CliFlags } from '@agent-device/contracts/command';
import {
assertSelectorHonorsDevicePin,
type DevicePin,
mergeDefinedFlags,
pickExplicitSelector,
resolveConfigBackedFlagDefaultsWithProvenance,
resolveDevicePin,
resolveSelectorOrigins,
type SelectorOrigins,
} from '@agent-device/command-surface';
import { finalizeParsedArgs, parseRawArgs } from './parser/args.ts';
import { resolveRemoteConfigDefaults } from './remote-config-flags.ts';
import { type EnvMap } from '@agent-device/kernel/source-value';
export type ResolvedCliOptions = ReturnType<typeof finalizeParsedArgs> & {
providedFlags: ReturnType<typeof parseRawArgs>['providedFlags'];
remoteConfigLeaseProvider: ReturnType<typeof resolveRemoteConfigDefaults>['leaseProvider'];
devicePin: DevicePin | undefined;
selectorOrigins: SelectorOrigins;
};
export function resolveCliOptions(
argv: string[],
options: {
cwd: string;
env: EnvMap;
strictFlags?: boolean;
},
): ResolvedCliOptions {
const rawParsed = parseRawArgs(argv);
const { env, cwd } = options;
const remoteConfig = shouldApplyRemoteConfigDefaults(rawParsed.command)
? resolveRemoteConfigDefaults({
remoteConfig: rawParsed.flags.remoteConfig,
cwd,
env,
})
: { flags: {} };
const configDefaults = resolveConfigBackedFlagDefaultsWithProvenance({
command: rawParsed.command,
cwd,
cliFlags: rawParsed.flags as CliFlags,
env,
});
const explicitFlagKeys = new Set(rawParsed.providedFlags.map((entry) => entry.key));
// The pin is enforced before the merge: once defaults and typed flags are one object, which
// selector the caller typed is gone, and a typed selector must fail rather than win (#2161).
const devicePin = resolveDevicePin(configDefaults.flags, configDefaults.provenance);
if (devicePin) {
assertSelectorHonorsDevicePin(
devicePin,
pickExplicitSelector(rawParsed.flags, explicitFlagKeys),
'cli',
);
}
const defaultFlags = mergeDefinedFlags(configDefaults.flags, remoteConfig.flags);
const finalized = finalizeParsedArgs(rawParsed, {
strictFlags: options?.strictFlags,
defaultFlags,
});
// The flags drop the profile's lease provider, which decides whether a session is required (#3931).
return {
...finalized,
providedFlags: rawParsed.providedFlags,
remoteConfigLeaseProvider: remoteConfig.leaseProvider,
devicePin,
selectorOrigins: resolveSelectorOrigins(finalized.flags, explicitFlagKeys, devicePin),
};
}
function shouldApplyRemoteConfigDefaults(command: string | null): boolean {
return command !== null;
}