-
Notifications
You must be signed in to change notification settings - Fork 10
/
VMCleanCommand.cs
44 lines (33 loc) · 1.27 KB
/
VMCleanCommand.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
using System.CommandLine;
using Spectre.Console;
using VmChamp;
public class VMCleanCommand : Command
{
private readonly AppConfig _appConfig;
public VMCleanCommand(AppConfig appConfig) : base("vmclean", "delete all vms without images")
{
_appConfig = appConfig;
this.AddAlias("vpurge");
this.AddAlias("vmc");
var allVmDirectories = Directory.GetDirectories(appConfig.DataDir);
this.SetHandler(() =>
{
if (allVmDirectories.Length == 0)
{
AnsiConsole.MarkupLine("[green]👀 No VMs found to delete.[/]");
}
AnsiConsole.MarkupLine($"[red]Going to delete all VMs ({allVmDirectories.Length}) without IMAGES[/]");
if (AnsiConsole.Ask<string>("Continue? (y/N)", "N").ToLower() != "y")
{
return;
}
using var libvirtConnection = LibvirtConnection.CreateForSession();
foreach (var vmDir in allVmDirectories)
{
var vmName = Path.GetFileName(vmDir);
var vmId = Interop.virDomainLookupByName(libvirtConnection.NativePtr, vmName);
Interop.DestroyVm(vmId, vmName, vmDir);
}
});
}
}