-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDocker.hx
153 lines (134 loc) · 4.04 KB
/
Docker.hx
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
/*
* SPDX-FileCopyrightText: © Sebastian Thomschke and contributors
* SPDX-FileContributor: Sebastian Thomschke
* SPDX-License-Identifier: MIT
* SPDX-ArtifactOfProjectHomePage: https://github.com/sebthom/more-clink-completions
*/
package more_clink_completions.completions;
import clink.util.Suggest;
import clink.api.LineState;
import clink.util.LuaArray;
import clink.api.ArgMatcher;
import clink.api.Clink;
import more_clink_completions.util.Utils;
using clink.util.Strings;
class Docker {
public static function register() {
Clink.argMatcher("docker").setDelayedInitializer(registerNow);
}
static function registerNow(parser:ArgMatcher, commandWord:String) {
parser
.addFlags(["-h", "--help"])
.addFlag("--config", Suggest.dirs)
.addFlags(["-c", "--context"], Suggest.dirs)
.addFlags(["-D", "--debug"])
.addFlags(["-H", "--hosts"], "")
.addFlags(["-l", "--log-level"], ["debug", "info", "warn", "error", "fatal"])
.addFlag("-tls")
.addFlag("-tlscacert", Suggest.files)
.addFlag("-tlscert", Suggest.files)
.addFlag("-tlskey", Suggest.files)
.addFlag("-tlsverify")
.addFlags(["-v", "--version"])
.addArg(suggestMainCommands)
.addArg(suggestSubCommands)
.noFiles()
.setClassifier(colorizeCommands);
}
static final COMMANDS_CACHE = new Map<String, LuaArray<String>>();
static function extractCommandsFromHelp(helpCommand:String):LuaArray<String> {
final commands = new LuaArray<String>();
var isCommandsSection = false;
for (line in Utils.exec(helpCommand)) {
if (isCommandsSection) {
if (line == "")
isCommandsSection = false;
else {
final command = line.findMatch("^%s+([a-zA-Z-_]+)");
if (command != null)
commands.add(command);
}
} else if (line.hasMatch("Commands:"))
isCommandsSection = true;
}
return commands;
}
static function suggestMainCommands():LuaArray<String> {
var mainCommands = COMMANDS_CACHE[""];
if (mainCommands == null) {
mainCommands = extractCommandsFromHelp("docker --help");
if (mainCommands.length() > 0) {
COMMANDS_CACHE[""] = mainCommands;
}
}
return mainCommands;
}
static function suggestSubCommands(word:String, wordIndex:Int, lineState:LineState):LuaArray<String> {
suggestMainCommands(); // trigger cache population
final mainCommand = lineState.getWord(wordIndex - 1);
if (@:nullSafety(Off) !COMMANDS_CACHE[""].contains(mainCommand)) {
return [];
}
var subCommands = COMMANDS_CACHE[mainCommand];
if (subCommands == null) {
subCommands = COMMANDS_CACHE[mainCommand] = extractCommandsFromHelp('docker ${mainCommand} --help');
}
return subCommands;
}
static function colorizeCommands(argIndex:Int, word:String, wordIndex:Int, lineState:LineState, classifications:WordClassifications) {
if (argIndex > 2) return;
if (argIndex == 2) {
suggestMainCommands(); // trigger cache population
suggestSubCommands(word, wordIndex, lineState); // trigger cache population
if (COMMANDS_CACHE[word] != null) {
classifications.classifyWord(wordIndex, ARGUMENT);
}
}
}
}
/**
* see https://man7.org/linux/man-pages/man7/capabilities.7.html
*/
enum Capabilities {
AUTID_CONTROL;
AUDIT_READ;
AUDIT_WRITE;
BLOCL_SUSPEND;
BPF;
CHECKPOINT_RESTORE;
CHOWN;
DAC_OVERRIDE;
DAC_READ_SEARCH;
FOWNER;
FSETID;
IPC_LOCK;
IPC_OWNER;
KILL;
LEASE;
LINUX_IMMUTABLE;
MAC_ADMIN;
MAC_OVERRIDE;
MKNOD;
NET_ADMIN;
NET_BIND_SERVICE;
NET_BROADCAST;
NET_RAW;
PERFMON;
SETGID;
SETFCAP;
SETPCAP;
SETUID;
SYS_ADMIN;
SYS_BOOT;
SYS_CHROOT;
SYS_MODULE;
SYS_NICE;
SYS_PACCT;
SYS_PTRACE;
SYS_RAWIO;
SYS_RESOURCE;
SYS_TIME;
SYS_TTY_CONFIG;
SYSLOG;
WAKE_ALARM;
}