nicer arg parsing

This commit is contained in:
benbierens 2023-06-26 14:44:21 +02:00
parent 868553f27d
commit c91d9cc7dc
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
5 changed files with 58 additions and 97 deletions

View File

@ -13,7 +13,7 @@ namespace ArgsUniform
private const int descStart = 80; private const int descStart = 80;
public ArgsUniform(params string[] args) public ArgsUniform(params string[] args)
: this(null!, args) : this(new IEnv.Env(), args)
{ {
} }
@ -46,13 +46,16 @@ namespace ArgsUniform
{ {
if (!UniformAssign(result, attr, uniformProperty) && attr.Required) if (!UniformAssign(result, attr, uniformProperty) && attr.Required)
{ {
missingRequired.Add(uniformProperty); {
missingRequired.Add(uniformProperty);
}
} }
} }
} }
if (missingRequired.Any()) if (missingRequired.Any())
{ {
PrintResults(result, uniformProperties);
Print(""); Print("");
foreach (var missing in missingRequired) foreach (var missing in missingRequired)
{ {
@ -68,17 +71,22 @@ namespace ArgsUniform
if (printResult) if (printResult)
{ {
Print(""); PrintResults(result, uniformProperties);
foreach (var p in uniformProperties)
{
Print($"\t{p.Name} = {p.GetValue(result)}");
}
Print("");
} }
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("");
@ -110,6 +118,12 @@ namespace ArgsUniform
Console.Write(desc + Environment.NewLine); Console.Write(desc + Environment.NewLine);
} }
private object GetDefaultValue(Type t)
{
if (t.IsValueType) return Activator.CreateInstance(t)!;
return null!;
}
private bool UniformAssign(T result, UniformAttribute attr, PropertyInfo uniformProperty) private bool UniformAssign(T result, UniformAttribute attr, PropertyInfo uniformProperty)
{ {
if (AssignFromArgsIfAble(result, attr, uniformProperty)) return true; if (AssignFromArgsIfAble(result, attr, uniformProperty)) return true;
@ -120,16 +134,19 @@ namespace ArgsUniform
private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty) private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty)
{ {
if (defaultsProvider == null) return true; var currentValue = uniformProperty.GetValue(result);
var isEmptryString = (currentValue as string) == string.Empty;
if (currentValue != GetDefaultValue(uniformProperty.PropertyType) && !isEmptryString) return true;
if (defaultsProvider == null) return false;
var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name); var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name);
if (defaultProperty == null) return true; if (defaultProperty == null) return false;
var value = defaultProperty.GetValue(defaultsProvider); var value = defaultProperty.GetValue(defaultsProvider);
if (value != null) if (value != null)
{ {
Assign(result, uniformProperty, value); return Assign(result, uniformProperty, value);
return true;
} }
return false; return false;
} }
@ -139,8 +156,7 @@ namespace ArgsUniform
var e = env.GetEnvVarOrDefault(attr.EnvVar, string.Empty); var e = env.GetEnvVarOrDefault(attr.EnvVar, string.Empty);
if (!string.IsNullOrEmpty(e)) if (!string.IsNullOrEmpty(e))
{ {
Assign(result, uniformProperty, e); return Assign(result, uniformProperty, e);
return true;
} }
return false; return false;
} }
@ -150,14 +166,12 @@ namespace ArgsUniform
var fromArg = GetFromArgs(attr.Arg); var fromArg = GetFromArgs(attr.Arg);
if (fromArg != null) if (fromArg != null)
{ {
Assign(result, uniformProperty, fromArg); return Assign(result, uniformProperty, fromArg);
return true;
} }
var fromShort = GetFromArgs(attr.ArgShort); var fromShort = GetFromArgs(attr.ArgShort);
if (fromShort != null) if (fromShort != null)
{ {
Assign(result, uniformProperty, fromShort); return Assign(result, uniformProperty, fromShort);
return true;
} }
return false; return false;
} }

View File

@ -1,91 +1,37 @@
using DistTestCore.Codex; using ArgsUniform;
using DistTestCore.Codex;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace ContinuousTests namespace ContinuousTests
{ {
public class Configuration public class Configuration
{ {
public string LogPath { get; set; } = string.Empty; [Uniform("log-path", "l", "LOGPATH", true, "Path where log files will be written.")]
public string DataPath { get; set; } = string.Empty; public string LogPath { get; set; } = "logs";
[Uniform("data-path", "d", "DATAPATH", true, "Path where temporary data files will be written.")]
public string DataPath { get; set; } = "data";
[Uniform("codex-deployment", "c", "CODEXDEPLOYMENT", true, "Path to codex-deployment JSON file.")]
public string CodexDeploymentJson { get; set; } = string.Empty;
[Uniform("keep", "k", "KEEP", false, "Set to '1' to retain logs of successful tests.")]
public bool KeepPassedTestLogs { get; set; } = false;
public CodexDeployment CodexDeployment { get; set; } = null!; public CodexDeployment CodexDeployment { get; set; } = null!;
public bool KeepPassedTestLogs { get; set; }
} }
public class ConfigLoader public class ConfigLoader
{ {
private const string filename = "config.json"; public Configuration Load(string[] args)
public Configuration Load()
{ {
var config = Read(); var uniformArgs = new ArgsUniform<Configuration>(args);
Validate(config); var result = uniformArgs.Parse();
return config;
}
private Configuration Read() result.CodexDeployment = ParseCodexDeploymentJson(result.CodexDeploymentJson);
{
if (File.Exists(filename))
{
var lines = File.ReadAllText(filename);
try
{
var result = JsonConvert.DeserializeObject<Configuration>(lines);
if (result != null) return result;
}
catch { }
}
var logPath = "logs";// Environment.GetEnvironmentVariable("LOGPATH"); return result;
var dataPath = "data";// Environment.GetEnvironmentVariable("DATAPATH");
var codexDeploymentJson = "C:\\Users\\Ben\\Desktop\\codex-deployment.json"; // Environment.GetEnvironmentVariable("CODEXDEPLOYMENT");
var keep = Environment.GetEnvironmentVariable("KEEPPASSEDTESTLOGS");
if (!string.IsNullOrEmpty(logPath) &&
!string.IsNullOrEmpty(dataPath) &&
!string.IsNullOrEmpty(codexDeploymentJson))
{
try
{
return new Configuration
{
LogPath = logPath,
DataPath = dataPath,
CodexDeployment = ParseCodexDeploymentJson(codexDeploymentJson),
KeepPassedTestLogs = keep == "1"
};
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex);
}
}
var nl = Environment.NewLine;
throw new Exception($"Unable to load configuration from '{filename}', and " +
"unable to load configuration from environment variables. " + nl +
"'LOGPATH' = Path where log files will be saved." + nl +
"'DATAPATH' = Path where temporary data files will be saved." + nl +
"'CODEXDEPLOYMENT' = Path to codex-deployment JSON file." + nl +
nl);
}
private void Validate(Configuration configuration)
{
if (string.IsNullOrEmpty(configuration.LogPath))
{
throw new Exception($"Invalid LogPath set: '{configuration.LogPath}'");
}
if (string.IsNullOrEmpty(configuration.DataPath))
{
throw new Exception($"Invalid DataPath set: '{configuration.DataPath}'");
}
if (configuration.CodexDeployment == null || !configuration.CodexDeployment.CodexContainers.Any())
{
throw new Exception("No Codex deployment found.");
}
} }
private CodexDeployment ParseCodexDeploymentJson(string filename) private CodexDeployment ParseCodexDeploymentJson(string filename)

View File

@ -9,9 +9,9 @@ namespace ContinuousTests
private readonly Configuration config; private readonly Configuration config;
private readonly StartupChecker startupChecker; private readonly StartupChecker startupChecker;
public ContinuousTestRunner() public ContinuousTestRunner(string[] args)
{ {
config = configLoader.Load(); config = configLoader.Load(args);
startupChecker = new StartupChecker(config); startupChecker = new StartupChecker(config);
} }

View File

@ -12,6 +12,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ArgsUniform\ArgsUniform.csproj" />
<ProjectReference Include="..\DistTestCore\DistTestCore.csproj" /> <ProjectReference Include="..\DistTestCore\DistTestCore.csproj" />
<ProjectReference Include="..\Logging\Logging.csproj" /> <ProjectReference Include="..\Logging\Logging.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -6,7 +6,7 @@ public class Program
{ {
Console.WriteLine("Codex Continous-Test-Runner."); Console.WriteLine("Codex Continous-Test-Runner.");
Console.WriteLine("Running..."); Console.WriteLine("Running...");
var runner = new ContinuousTestRunner(); var runner = new ContinuousTestRunner(args);
runner.Run(); runner.Run();
} }
} }