-
Notifications
You must be signed in to change notification settings - Fork 10
/
RestartCommand.cs
40 lines (31 loc) · 1.14 KB
/
RestartCommand.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
using System.CommandLine;
using System.CommandLine.Completions;
using Spectre.Console;
using VmChamp;
public class RestartCommand : Command
{
private readonly AppConfig _appConfig;
public RestartCommand(AppConfig appConfig) : base("restart", "force restarts a vm")
{
_appConfig = appConfig;
Argument<string> nameArgument = new("name",
() => appConfig.DefaultVmName,
"Name of the VM to restart.");
this.AddAlias("reboot");
this.AddAlias("reset");
this.AddArgument(nameArgument);
nameArgument.AddCompletions((ctx) =>
Helper.GetAllVmInDirectory(_appConfig.DataDir)
.Select(vmName => new CompletionItem(vmName ?? "")));
nameArgument.AddCompletions((ctx) =>
Helper.GetAllVmInDirectory(_appConfig.DataDir)
.Select(vmName => new CompletionItem(vmName ?? "")));
this.SetHandler((vmName) =>
{
using var libvirtConnection = LibvirtConnection.CreateForSession();
var vmId = Interop.virDomainLookupByName(libvirtConnection.NativePtr, vmName);
AnsiConsole.MarkupLine($"[yellow]🔄 Restart VM: {vmName}[/]");
Interop.virDomainReset(vmId);
}, nameArgument);
}
}