-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDockerMachine.hx
80 lines (68 loc) · 2.51 KB
/
DockerMachine.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
/*
* 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 DockerMachine {
public static function register() {
Clink.argMatcher("docker-machine").setDelayedInitializer(registerNow);
}
static function registerNow(parser:ArgMatcher, commandWord:String) {
parser
.addFlags(["-D", "--debug"])
.addFlag("--storage-path", Suggest.dirs)
.addFlag("--tls-ca-cert", Suggest.files)
.addFlag("--tls-ca-key", Suggest.files)
.addFlag("--tls-client-cert", Suggest.files)
.addFlag("--tls-client-key", Suggest.files)
.addFlag("--github-api-token", "")
.addFlag("--native-ssh", "")
.addFlag("--bugsnag-api-token", "")
.addFlags(["-h", "--help"])
.addFlags(["-v", "--version"])
.addArg(suggestCommands)
.noFiles()
.setClassifier(colorizeCommands);
}
static var COMMANDS_CACHE = new 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 suggestCommands():LuaArray<String> {
if (COMMANDS_CACHE.length() == 0) {
COMMANDS_CACHE = extractCommandsFromHelp("docker-machine --help");
}
return COMMANDS_CACHE;
}
static function colorizeCommands(argIndex:Int, word:String, wordIndex:Int, lineState:LineState, classifications:WordClassifications) {
if (argIndex == 1) {
suggestCommands(); // trigger cache population
if (COMMANDS_CACHE.contains(word)) {
classifications.classifyWord(wordIndex, ARGUMENT);
}
}
}
}