-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathdscacheutil.ts
135 lines (127 loc) · 3.32 KB
/
dscacheutil.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
const categoryKeysMapping = {
group: ["name", "gid"],
host: ["name", "ip_address"],
mount: ["name"],
protocol: ["name", "number"],
rpc: ["name", "number"],
service: ["name", "port"],
user: ["name", "uid"],
};
const categories = Array.from(Object.keys(categoryKeysMapping));
interface PostProcessQuery {
attributeKey: string;
}
const postProcessQuery =
(options: PostProcessQuery): Fig.Generator["postProcess"] =>
(out) => {
const { attributeKey } = options;
const values = new Set<string>();
out.split("\n\n").map((entry) => {
entry.split("\n").forEach((line) => {
const [key, value] = line.trim().split(": ");
if (key === attributeKey) {
values.add(value);
}
});
});
const suggestions = [];
values.forEach((value) => suggestions.push({ name: value }));
return suggestions;
};
const dscacheutilGenerators: Record<string, Fig.Generator> = {
keys: {
custom: async (tokens, executeShellCommand) => {
const category = tokens[tokens.length - 3];
if (!categories.includes(category)) {
return [];
}
return categoryKeysMapping[category].map((key: string) => ({
name: key,
}));
},
},
values: {
custom: async (tokens, executeShellCommand) => {
const category = tokens[tokens.length - 4];
const attributeKey = tokens[tokens.length - 2];
const pp = postProcessQuery({ attributeKey });
return pp(
(
await executeShellCommand({
command: "dscacheutil",
args: ["-q", category],
})
).stdout,
tokens
);
},
},
};
const completionSpec: Fig.Spec = {
name: "dscacheutil",
description: "Utility for managing the Directory Service cache",
subcommands: [
{
name: "-h",
description: "List the options for calling dscacheutil",
},
{
name: "-q",
description: "Query the Directory Service cache",
options: [
{
name: "-a",
description: "Attribute to query",
args: [
{
name: "attributeKey",
generators: dscacheutilGenerators.keys,
},
{
name: "attributeValue",
generators: dscacheutilGenerators.values,
},
],
},
],
args: {
name: "category",
description: "Category to query",
suggestions: categories,
},
},
{
name: "-cachedump",
description: "Get an overview of the cache by default",
options: [
{
name: "-buckets",
description: "Get an overview of the cache by default",
},
{
name: "-entries",
description: "Dump detailed information about cache entries",
args: {
name: "entry",
suggestions: categories,
},
},
],
},
{
name: "-configuration",
description:
"Get the current configuration information, such as the search policy and cache parameters",
},
{
name: "-flushcache",
description: "Flush the entire cache",
},
{
name: "-statistics",
description:
"Get statistics from the cache, including an overview an detailed call statistics",
},
],
};
export default completionSpec;