-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathdscl.ts
318 lines (310 loc) · 8.22 KB
/
dscl.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// https://stackoverflow.com/a/70994696/6183068
const satisfies =
<T>() =>
<U extends T>(u: U) =>
u;
const dsclOptions: Fig.Option[] = [
{
name: "-p",
description: "Prompt for password",
},
{
name: "-u",
description: "Authenticate as user",
args: {
name: "user",
description: "User to authenticate as",
},
},
{
name: "-P",
description: "Authenticate with password",
args: {
name: "password",
description: "Password to authenticate with",
},
},
{
name: "-f",
description: "Targeted local node database file path",
args: {
name: "file",
description: "File path",
},
},
{
name: "-raw",
description: "Don't strip off prefix from DirectoryService API constants",
},
{
name: "-plist",
description: "Print out record(s) or attribute(s) in XML plist format",
},
{
name: "-url",
description: "Print record attribute values in URL-style encoding",
},
{
name: "-q",
description: "Quiet - no interactive prompt",
},
];
const dsclOptionsWithArgs = new Set<string>(
dsclOptions
.filter((option) => option.args !== undefined)
.flatMap((option) => option.name)
);
const generateDsclPath: Fig.Generator = {
trigger: "/",
getQueryTerm: "/",
custom: async (tokens, executeShellCommand) => {
const lastToken = tokens[tokens.length - 1];
if (lastToken === "") {
return [];
}
// can't guarantee that dscl is the first token. If it's not found,
// this is -1, so searching for `datasource` will start from index 0
const dsclIndex = tokens.indexOf("dscl");
let datasource: string;
for (let i = dsclIndex + 1; i < tokens.length; i++) {
const token = tokens[i];
// skip if the current token is a flag
if (token.startsWith("-")) {
continue;
}
// skip if the previous token is a flag that takes an argument
const prev = tokens[i - 1];
if (dsclOptionsWithArgs.has(prev)) {
continue;
}
datasource = token;
break;
}
const lastSlashIndex = lastToken.lastIndexOf("/");
const path = lastToken.slice(0, lastSlashIndex < 0 ? 0 : lastSlashIndex);
const { stdout: lines } = await executeShellCommand({
command: "dscl",
args: [datasource, "-list", path ?? "/"],
});
return lines
.trim()
.split("\n")
.map((line) => ({
name: line,
icon: "📁",
priority: line[line.lastIndexOf("/") + 1] === "_" ? 49 : 50,
}));
},
};
const dsclArgs = satisfies<Record<string, Fig.Arg>>()({
path: {
name: "path",
description: "Path to the record",
generators: generateDsclPath,
},
key: {
name: "key",
description: "Directory Service record attribute type",
},
keys: {
name: "keys",
description: "Directory Service record attribute type",
isVariadic: true,
},
plistPath: {
name: "plist path",
description: "Path to the plist file",
},
valueIndex: {
name: "value index",
description: "Value index of the key",
},
val: {
name: "val",
description: "Value of the key",
},
vals: {
name: "vals",
description: "Value of the key",
isVariadic: true,
},
oldVal: {
name: "old val",
description: "Old value of the key",
},
newVal: {
name: "new val",
description: "New value of the key",
},
});
const arg = (
name: keyof typeof dsclArgs,
merge: Partial<Fig.Arg>
): Fig.Arg => ({
...dsclArgs[name],
...merge,
});
const dsclSubcommands: Fig.Subcommand = {
name: "dscl",
subcommands: [
{
name: ["read", "-read"],
description: "Prints a directory",
args: [
arg("path", { isOptional: true }),
arg("keys", { isOptional: true }),
],
},
{
name: ["readall", "-readall"],
description: "Prints all the records of a given type",
args: [
arg("path", { isOptional: true }),
arg("keys", { isOptional: true }),
],
},
{
name: ["readpl", "-readpl"],
description: "Prints the contents of plist_path",
args: [dsclArgs.path, dsclArgs.key, dsclArgs.plistPath],
},
{
name: ["readpli", "-readpli"],
description:
"Prints the contents of plist_path for the plist at value_index of the key",
args: [
dsclArgs.path,
dsclArgs.key,
dsclArgs.valueIndex,
dsclArgs.plistPath,
],
},
{
name: ["list", "-list", "ls", "-ls"],
description: "Lists the subdirectories of the given directory",
args: [dsclArgs.path, arg("key", { isOptional: true })],
},
{
name: ["search", "-search"],
description: "Searches for records that match a pattern",
args: [dsclArgs.path, dsclArgs.key, dsclArgs.val],
},
{
name: ["create", "-create", "mk", "-mk"],
description: "Creates a new record",
args: [
dsclArgs.path,
arg("key", { isOptional: true }),
arg("vals", { isOptional: true }),
],
},
{
name: ["createpl", "-createpl"],
description: "Creates a string, or array of strings at plist_path",
args: [
dsclArgs.path,
dsclArgs.key,
dsclArgs.plistPath,
dsclArgs.val,
arg("vals", { isOptional: true }),
],
},
{
name: ["createpli", "-createpli"],
description:
"Creates a string, or array of strings at plist_path for the plist at value_index of the key",
args: [
dsclArgs.path,
dsclArgs.key,
dsclArgs.valueIndex,
dsclArgs.plistPath,
dsclArgs.val,
arg("vals", { isOptional: true }),
],
},
{
name: ["append", "-append"],
description: "Appends one or more values to a property in a given record",
args: [dsclArgs.path, dsclArgs.key, dsclArgs.vals],
},
{
name: ["merge", "-merge"],
description:
"Appends one or more values to a property in a given directory if the property does not already have those values",
args: [dsclArgs.path, dsclArgs.key, dsclArgs.vals],
},
{
name: ["delete", "-delete", "rm", "-rm"],
description: "Delete a directory, property, or value",
args: [
dsclArgs.path,
arg("key", { isOptional: true }),
arg("vals", { isOptional: true }),
],
},
{
name: ["deletepl", "-deletepl"],
description: "Deletes a value in a plist",
args: [
dsclArgs.path,
dsclArgs.key,
dsclArgs.plistPath,
arg("vals", { isOptional: true }),
],
},
{
name: ["deletepli", "-deletepli"],
description: "Deletes a value for the plist at value_index of the key",
args: [
dsclArgs.path,
dsclArgs.key,
dsclArgs.valueIndex,
dsclArgs.plistPath,
arg("vals", { isOptional: true }),
],
},
{
name: ["change", "-change"],
description:
"Replaces the given old value in the list of values of the given key with the new value in the specified record",
args: [dsclArgs.path, dsclArgs.key, dsclArgs.oldVal, dsclArgs.newVal],
},
{
name: ["changei", "-changei"],
description:
"Replaces the value at the given index in the list of values of the given key with the new value in the specified record",
args: [dsclArgs.path, dsclArgs.key, dsclArgs.valueIndex, dsclArgs.newVal],
},
{
name: ["diff", "-diff"],
description:
"Compares the data from path1 and path2 looking at the specified keys (or all if no keys are specified)",
args: [dsclArgs.path, dsclArgs.path, arg("keys", { isOptional: true })],
},
{
name: ["passwd", "-passwd"],
description: "Changes the password of a user",
args: [
dsclArgs.path,
{
name: "new password",
description: "New password of the user",
isOptional: true,
},
],
},
],
};
const completionSpec: Fig.Spec = {
name: "dscl",
description: "Directory Service command line utility",
subcommands: dsclSubcommands.subcommands,
options: dsclOptions,
args: {
name: "datasource",
suggestions: [".", "..", "localhost", "localonly"],
isOptional: true,
loadSpec: dsclSubcommands,
},
};
export default completionSpec;