forked from wubbl0rz/VmChamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
103 lines (85 loc) · 2.65 KB
/
Program.cs
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
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Completions;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using Spectre.Console;
using VmChamp;
var appConfig = new AppConfig();
Directory.CreateDirectory(appConfig.CacheDir);
Directory.CreateDirectory(appConfig.DataDir);
var downloader = new Downloader(new DirectoryInfo(appConfig.CacheDir));
var rootCommand = new RootCommand();
rootCommand.AddCommand(new RunCommand(appConfig, downloader));
rootCommand.AddCommand(new CleanCommand(appConfig));
rootCommand.AddCommand(new VMCleanCommand(appConfig));
rootCommand.AddCommand(new RemoveCommand(appConfig));
rootCommand.AddCommand(new RestartCommand(appConfig));
rootCommand.AddCommand(new SshCommand(appConfig));
rootCommand.AddCommand(new ListCommand(appConfig));
rootCommand.AddCommand(new OsCommand(appConfig));
rootCommand.AddCommand(new KekwCommand());
var completionOption = new Option<string?>("--completion", "generate shell completion. (zsh or bash)");
rootCommand.AddOption(completionOption);
rootCommand.SetHandler((complete) =>
{
if (complete == "zsh")
{
var zsh = """
_VmChamp_zsh_complete()
{
local completions=("$(VmChamp '[complete]' "$words")");
_values = "${(ps:\n:)completions}"
}
compdef _VmChamp_zsh_complete VmChamp
compdef _VmChamp_zsh_complete vmchamp
""";
AnsiConsole.WriteLine(zsh);
}
else if (complete == "bash")
{
var bash = """
_vmchamp_bash_complete() {
COMPREPLY=()
local word=${COMP_WORDS[COMP_CWORD]}
local line=${COMP_LINE}
COMPREPLY=($(compgen -W '$(vmchamp '[complete]' "${line}")' -- ${word}))
return 0
}
complete -F _vmchamp_bash_complete vmchamp
""";
AnsiConsole.WriteLine(bash);
}
}, completionOption);
var builder = new CommandLineBuilder(rootCommand).UseDefaults();
var app = builder.Build();
if (args.Length == 0)
{
return await app.InvokeAsync("--help");
}
if (args.Length > 1 && args[0] == "[complete]")
{
var parseResult = app.Parse(args[1]);
var lastToken = parseResult.Tokens.LastOrDefault();
var completions = parseResult.GetCompletions()
.Where(completion => completion.Label is not "-?" and not "-h" and not "/?" and not "/h")
.ToArray();
if (completions.Length == 0)
{
Console.WriteLine(lastToken?.Value);
}
foreach (var completion in completions)
{
Console.WriteLine(completion.Label);
}
return 0;
}
if (!Interop.IsLibvirtInstalled())
{
AnsiConsole.MarkupLine("[red]Libvirt not found.[/]");
return 1;
}
return await app.InvokeAsync(args);