forked from wubbl0rz/VmChamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanCommand.cs
44 lines (33 loc) · 1.14 KB
/
CleanCommand.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 CleanCommand : Command
{
private readonly AppConfig _appConfig;
public CleanCommand(AppConfig appConfig) : base("clean", "delete all vms and images")
{
_appConfig = appConfig;
this.AddAlias("purge");
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}) and 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);
}
Directory.Delete(appConfig.CacheDir, true);
});
}
}