Prints default ArgsUniform values. Applies discord IDs instead of names.

This commit is contained in:
Ben 2024-05-16 14:05:58 +02:00
parent b3771cce32
commit 7d9dcb263d
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
6 changed files with 71 additions and 50 deletions

View File

@ -61,7 +61,7 @@ namespace ArgsUniform
if (missingRequired.Any()) if (missingRequired.Any())
{ {
PrintResults(result, uniformProperties); PrintResults(printResult,result, uniformProperties);
Print(""); Print("");
foreach (var missing in missingRequired) foreach (var missing in missingRequired)
{ {
@ -75,34 +75,36 @@ namespace ArgsUniform
Environment.Exit(1); Environment.Exit(1);
} }
if (printResult) PrintResults(printResult, result, uniformProperties);
{
PrintResults(result, uniformProperties);
}
return result; return result;
} }
private void PrintResults(T result, PropertyInfo[] uniformProperties)
{
Print("");
foreach (var p in uniformProperties)
{
Print($"\t{p.Name} = {p.GetValue(result)}");
}
Print("");
}
public void PrintHelp() public void PrintHelp()
{ {
Print(""); Print("");
PrintAligned("CLI option:", "(short)", "Environment variable:", "Description"); PrintAligned("CLI option:", "(short)", "Environment variable:", "Description", "(default)");
var attrs = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(UniformAttribute), false).Length == 1).Select(p => p.GetCustomAttribute<UniformAttribute>()).Where(a => a != null).ToArray(); var props = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(UniformAttribute), false).Length == 1).ToArray();
foreach (var attr in attrs) foreach (var prop in props)
{ {
var a = attr!; var a = prop.GetCustomAttribute<UniformAttribute>();
var optional = !a.Required ? " *" : ""; if (a != null)
PrintAligned($"--{a.Arg}=...", $"({a.ArgShort})", a.EnvVar, a.Description + optional); {
var optional = !a.Required ? " (optional)" : "";
var def = assigner.DescribeDefaultFor(prop);
PrintAligned($"--{a.Arg}=...", $"({a.ArgShort})", a.EnvVar, a.Description + optional, $"({def})");
}
}
Print("");
}
private void PrintResults(bool printResult, T result, PropertyInfo[] uniformProperties)
{
if (!printResult) return;
Print("");
foreach (var p in uniformProperties)
{
Print($"\t{p.Name} = {p.GetValue(result)}");
} }
Print(""); Print("");
} }
@ -112,7 +114,7 @@ namespace ArgsUniform
Console.WriteLine(msg); Console.WriteLine(msg);
} }
private void PrintAligned(string cli, string s, string env, string desc) private void PrintAligned(string cli, string s, string env, string desc, string def)
{ {
Console.CursorLeft = cliStart; Console.CursorLeft = cliStart;
Console.Write(cli); Console.Write(cli);
@ -121,7 +123,8 @@ namespace ArgsUniform
Console.CursorLeft = envStart; Console.CursorLeft = envStart;
Console.Write(env); Console.Write(env);
Console.CursorLeft = descStart; Console.CursorLeft = descStart;
Console.Write(desc + Environment.NewLine); Console.Write(desc + " ");
Console.Write(def + Environment.NewLine);
} }
} }
} }

View File

@ -23,20 +23,38 @@ namespace ArgsUniform
return false; return false;
} }
public string DescribeDefaultFor(PropertyInfo property)
{
var obj = Activator.CreateInstance<T>();
var defaultValue = GetDefaultValue(obj, property);
if (defaultValue == null) return "";
if (defaultValue is string str)
{
return "\"" + str + "\"";
}
return defaultValue.ToString() ?? string.Empty;
}
private object? GetDefaultValue(T result, PropertyInfo uniformProperty)
{
// Get value from object's static initializer if it's there.
var currentValue = uniformProperty.GetValue(result);
if (currentValue != null) return currentValue;
// Get value from defaults-provider object if it's there.
if (defaultsProvider == null) return null;
var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name);
if (defaultProperty == null) return null;
return defaultProperty.GetValue(defaultsProvider);
}
private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty) private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty)
{ {
var currentValue = uniformProperty.GetValue(result); var defaultValue = GetDefaultValue(result, uniformProperty);
var isEmptryString = (currentValue as string) == string.Empty; var isEmptryString = (defaultValue as string) == string.Empty;
if (currentValue != GetDefaultValueForType(uniformProperty.PropertyType) && !isEmptryString) return true; if (defaultValue != null && defaultValue != GetDefaultValueForType(uniformProperty.PropertyType) && !isEmptryString)
if (defaultsProvider == null) return false;
var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name);
if (defaultProperty == null) return false;
var value = defaultProperty.GetValue(defaultsProvider);
if (value != null)
{ {
return Assign(result, uniformProperty, value); return Assign(result, uniformProperty, defaultValue);
} }
return false; return false;
} }

View File

@ -24,7 +24,7 @@ namespace BiblioTech
public bool IsAdminChannel(IChannel channel) public bool IsAdminChannel(IChannel channel)
{ {
return channel.Name == Program.Config.AdminChannelName; return channel.Id == Program.Config.AdminChannelId;
} }
public ISocketMessageChannel GetAdminChannel() public ISocketMessageChannel GetAdminChannel()
@ -45,7 +45,7 @@ namespace BiblioTech
private void UpdateAdminIds() private void UpdateAdminIds()
{ {
lastUpdate = DateTime.UtcNow; lastUpdate = DateTime.UtcNow;
var adminRole = guild.Roles.Single(r => r.Name == Program.Config.AdminRoleName); var adminRole = guild.Roles.Single(r => r.Id == Program.Config.AdminRoleId);
adminIds = adminRole.Members.Select(m => m.Id).ToArray(); adminIds = adminRole.Members.Select(m => m.Id).ToArray();
} }
} }

View File

@ -22,7 +22,7 @@ namespace BiblioTech
private async Task Client_Ready() private async Task Client_Ready()
{ {
var guild = client.Guilds.Single(g => g.Name == Program.Config.ServerName); var guild = client.Guilds.Single(g => g.Id == Program.Config.ServerId);
Program.AdminChecker.SetGuild(guild); Program.AdminChecker.SetGuild(guild);
Program.Log.Log($"Initializing for guild: '{guild.Name}'"); Program.Log.Log($"Initializing for guild: '{guild.Name}'");

View File

@ -10,7 +10,7 @@ namespace BiblioTech
[Uniform("server-id", "sn", "SERVERID", true, "ID of the Discord server")] [Uniform("server-id", "sn", "SERVERID", true, "ID of the Discord server")]
public ulong ServerId { get; set; } public ulong ServerId { get; set; }
[Uniform("datapath", "dp", "DATAPATH", false, "Root path where all data files will be saved.")] [Uniform("datapath", "dp", "DATAPATH", true, "Root path where all data files will be saved.")]
public string DataPath { get; set; } = "datapath"; public string DataPath { get; set; } = "datapath";
[Uniform("admin-role-id", "a", "ADMINROLEID", true, "ID of the Discord server admin role")] [Uniform("admin-role-id", "a", "ADMINROLEID", true, "ID of the Discord server admin role")]
@ -23,15 +23,15 @@ namespace BiblioTech
public ulong RewardsChannelId { get; set; } public ulong RewardsChannelId { get; set; }
[Uniform("chain-events-channel-id", "cc", "CHAINEVENTSCHANNELID", false, "ID of the Discord server channel where chain events will be posted.")] [Uniform("chain-events-channel-id", "cc", "CHAINEVENTSCHANNELID", false, "ID of the Discord server channel where chain events will be posted.")]
public ulong ChainEventsChannelID { get; set; } public ulong ChainEventsChannelId { get; set; }
[Uniform("reward-api-port", "rp", "REWARDAPIPORT", false, "TCP listen port for the reward API.")] [Uniform("reward-api-port", "rp", "REWARDAPIPORT", true, "TCP listen port for the reward API.")]
public int RewardApiPort { get; set; } = 31080; public int RewardApiPort { get; set; } = 31080;
[Uniform("send-eth", "se", "SENDETH", false, "Amount of Eth send by the mint command. Default: 10.")] [Uniform("send-eth", "se", "SENDETH", true, "Amount of Eth send by the mint command.")]
public int SendEth { get; set; } = 10; public int SendEth { get; set; } = 10;
[Uniform("mint-tt", "mt", "MINTTT", false, "Amount of TestTokens minted by the mint command. Default: 1073741824")] [Uniform("mint-tt", "mt", "MINTTT", true, "Amount of TestTokens minted by the mint command.")]
public int MintTT { get; set; } = 1073741824; public int MintTT { get; set; } = 1073741824;
public string EndpointsPath public string EndpointsPath

View File

@ -16,8 +16,8 @@ namespace BiblioTech.Rewards
{ {
this.client = client; this.client = client;
rewardsChannel = GetChannel(Program.Config.RewardsChannelName); rewardsChannel = GetChannel(Program.Config.RewardsChannelId);
eventsChannel = GetChannel(Program.Config.ChainEventsChannelName); eventsChannel = GetChannel(Program.Config.ChainEventsChannelId);
} }
public async Task GiveRewards(GiveRewardsCommand rewards) public async Task GiveRewards(GiveRewardsCommand rewards)
@ -45,10 +45,10 @@ namespace BiblioTech.Rewards
await context.ProcessGiveRewardsCommand(LookUpUsers(rewards)); await context.ProcessGiveRewardsCommand(LookUpUsers(rewards));
} }
private SocketTextChannel? GetChannel(string name) private SocketTextChannel? GetChannel(ulong id)
{ {
if (string.IsNullOrEmpty(name)) return null; if (id == 0) return null;
return GetGuild().TextChannels.SingleOrDefault(c => c.Name == name); return GetGuild().TextChannels.SingleOrDefault(c => c.Id == id);
} }
private async Task ProcessChainEvents(string[] eventsOverview) private async Task ProcessChainEvents(string[] eventsOverview)
@ -147,11 +147,11 @@ namespace BiblioTech.Rewards
private SocketGuild GetGuild() private SocketGuild GetGuild()
{ {
var guild = client.Guilds.SingleOrDefault(g => g.Name == Program.Config.ServerName); var guild = client.Guilds.SingleOrDefault(g => g.Id == Program.Config.ServerId);
if (guild == null) if (guild == null)
{ {
throw new Exception($"Unable to find guild by name: '{Program.Config.ServerName}'. " + throw new Exception($"Unable to find guild by id: '{Program.Config.ServerId}'. " +
$"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name))}]"); $"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name + " (" + g.Id + ")"))}]");
} }
return guild; return guild;
} }