Skip to content

Commit 8bef1ff

Browse files
author
LoneWandererProductions
committed
Bring back some features
1 parent fe2853d commit 8bef1ff

4 files changed

Lines changed: 108 additions & 6 deletions

File tree

CoreBuilder/CommandFactory.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
using CoreBuilder.Development;
10+
using CoreBuilder.Extensions;
1011
using CoreBuilder.FileManager;
1112
using CoreBuilder.Interface;
1213
using CoreBuilder.Rules;
@@ -99,6 +100,20 @@ public static IReadOnlyList<ICommand> GetCommands(string userspace)
99100
.ToList();
100101
}
101102

103+
/// <summary>
104+
/// Gets the extensions.
105+
/// </summary>
106+
/// <returns>All Extensions</returns>
107+
public static IReadOnlyList<ICommandExtension> GetExtensions()
108+
{
109+
ICommandExtension[] modules =
110+
{
111+
new WhoAmIExtension()
112+
};
113+
114+
return modules;
115+
}
116+
102117
/// <summary>
103118
/// Gets all analyzers.
104119
/// </summary>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: CoreBuilder.Extensions
4+
* FILE: WhoAmIExtension.cs
5+
* PURPOSE: Extension for WhoAmI to return individual parameters like "ip" or "hostname". Specially designed for scripting.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System;
10+
using System.Linq;
11+
using System.Net.NetworkInformation;
12+
using Weaver.Interfaces;
13+
using Weaver.Messages;
14+
15+
namespace CoreBuilder.Extensions
16+
{
17+
/// <inheritdoc />
18+
/// <summary>
19+
/// Extension for WhoAmI command to fetch individual parameters
20+
/// </summary>
21+
public sealed class WhoAmIExtension : ICommandExtension
22+
{
23+
/// <inheritdoc />
24+
public string Name => "Who";
25+
26+
/// <inheritdoc />
27+
public string Description => "Returns specific information of WhoAmI command (e.g., ip, hostname, username), you can join them or return a single one.";
28+
29+
/// <inheritdoc />
30+
public string Namespace => "System";
31+
32+
/// <inheritdoc />
33+
public CommandResult Invoke(ICommand command, string[] args, Func<string[], CommandResult> executor)
34+
{
35+
if (args.Length == 0)
36+
{
37+
return CommandResult.Fail("No parameter specified. Example: whoami().who(ip,hostname) or whoami().who(ip)");
38+
}
39+
40+
try
41+
{
42+
var hostname = Environment.MachineName;
43+
var username = Environment.UserName;
44+
var domain = Environment.UserDomainName;
45+
46+
var ips = NetworkInterface
47+
.GetAllNetworkInterfaces()
48+
.Where(n => n.OperationalStatus == OperationalStatus.Up)
49+
.SelectMany(n => n.GetIPProperties().UnicastAddresses)
50+
.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
51+
.Select(a => a.Address.ToString())
52+
.Distinct()
53+
.ToList();
54+
55+
string ipsJoined = ips.Any() ? string.Join(", ", ips) : "None";
56+
57+
// Build a list of lines for requested fields
58+
var lines = args.Select(arg => arg.ToLowerInvariant())
59+
.Select(field =>
60+
{
61+
return field switch
62+
{
63+
"hostname" => $"Hostname: {hostname}",
64+
"username" => $"Username: {username}",
65+
"domain" => $"Domain: {domain}",
66+
"ip" => $"IP: {ipsJoined}",
67+
"os" => $"OS: {Environment.OSVersion}",
68+
"64bitos" => $"64-bit OS: {Environment.Is64BitOperatingSystem}",
69+
"64bitprocess" => $"64-bit Process: {Environment.Is64BitProcess}",
70+
"processorcount" => $"Processor Count: {Environment.ProcessorCount}",
71+
"clrversion" => $"CLR Version: {Environment.Version}",
72+
_ => $"Unknown parameter: {field}"
73+
};
74+
}).ToArray();
75+
76+
// Join lines into a single message
77+
string message = string.Join(Environment.NewLine, lines);
78+
79+
return CommandResult.Ok(message: message);
80+
}
81+
catch (Exception ex)
82+
{
83+
return CommandResult.Fail($"WhoExtension failed: {ex.Message}");
84+
}
85+
}
86+
}
87+
}

CoreBuilder/WhoAmI.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

9-
// ReSharper disable UnusedType.Global
10-
119
using System;
1210
using System.Linq;
1311
using System.Net.NetworkInformation;
@@ -19,7 +17,7 @@ namespace CoreBuilder
1917
{
2018
/// <inheritdoc />
2119
/// <summary>
22-
/// Displays local machine identity (hostname, user, IP addresses).
20+
/// Displays local machine identity (hostname, user, IP addresses) and allows extensions to fetch individual properties.
2321
/// </summary>
2422
public sealed class WhoAmI : ICommand
2523
{

Lector/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
using CoreBuilder;
10+
using CoreBuilder.Extensions;
1011
using Weaver;
1112

1213
namespace Lector
@@ -61,10 +62,11 @@ private static void Main(string[] args)
6162
/// <param name="weave">The weave.</param>
6263
private static void RegisterApps(Weave weave)
6364
{
64-
var modules = CommandFactory.GetCommands();
65-
66-
foreach (var module in modules)
65+
foreach (var module in CommandFactory.GetCommands())
6766
weave.Register(module);
67+
68+
foreach (var ext in CommandFactory.GetExtensions())
69+
weave.RegisterExtension(ext);
6870
}
6971
}
7072
}

0 commit comments

Comments
 (0)