-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLoader.cs
More file actions
51 lines (45 loc) · 1.78 KB
/
CommandLoader.cs
File metadata and controls
51 lines (45 loc) · 1.78 KB
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
using Spectre.Console;
namespace NShell.Shell.Commands;
/// <summary>
/// <c>CommandLoader</c> is responsible for loading custom and system commands.
/// </summary>
public static class CommandLoader
{
/// <summary>
/// Refresh commands list after each apt/apt-get install.
/// Prevent from installing a binary and don't find it if you don't reboot the shell.
/// </summary>
public static void RefreshCommands()
{
var paths = Environment.GetEnvironmentVariable("PATH")?.Split(':') ?? Array.Empty<string>();
var newCommands = new HashSet<string>();
foreach (var path in paths)
{
if (!Directory.Exists(path)) continue;
try
{
var files = Directory.GetFiles(path);
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
newCommands.Add(fileName);
}
}
catch (UnauthorizedAccessException ex)
{
AnsiConsole.MarkupLine($"[[[red]-[/]]] - Access denied to directory: {path}. Error: [bold yellow]{ex.Message}[/]");
}
catch (DirectoryNotFoundException ex)
{
AnsiConsole.MarkupLine($"[[[red]-[/]]] - Directory not found: {path}. Error: [bold yellow]{ex.Message}[/]");
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[[[red]-[/]]] - Error accessing directory {path}: [bold yellow]{ex.Message}[/]");
}
}
HashSet<string> availableCommands = newCommands;
AnsiConsole.MarkupLine($"[[[green]+[/]]] - Refreshed command list. Found {availableCommands.Count} new commands.");
}
}