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;
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)
{
missingRequired.Add(uniformProperty);
{
missingRequired.Add(uniformProperty);
}
}
}
}
if (missingRequired.Any())
{
PrintResults(result, uniformProperties);
Print("");
foreach (var missing in missingRequired)
{
@ -68,17 +71,22 @@ namespace ArgsUniform
if (printResult)
{
Print("");
foreach (var p in uniformProperties)
{
Print($"\t{p.Name} = {p.GetValue(result)}");
}
Print("");
PrintResults(result, uniformProperties);
}
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()
{
Print("");
@ -110,6 +118,12 @@ namespace ArgsUniform
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)
{
if (AssignFromArgsIfAble(result, attr, uniformProperty)) return true;
@ -120,16 +134,19 @@ namespace ArgsUniform
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);
if (defaultProperty == null) return true;
if (defaultProperty == null) return false;
var value = defaultProperty.GetValue(defaultsProvider);
if (value != null)
{
Assign(result, uniformProperty, value);
return true;
return Assign(result, uniformProperty, value);
}
return false;
}
@ -139,8 +156,7 @@ namespace ArgsUniform
var e = env.GetEnvVarOrDefault(attr.EnvVar, string.Empty);
if (!string.IsNullOrEmpty(e))
{
Assign(result, uniformProperty, e);
return true;
return Assign(result, uniformProperty, e);
}
return false;
}
@ -150,14 +166,12 @@ namespace ArgsUniform
var fromArg = GetFromArgs(attr.Arg);
if (fromArg != null)
{
Assign(result, uniformProperty, fromArg);
return true;
return Assign(result, uniformProperty, fromArg);
}
var fromShort = GetFromArgs(attr.ArgShort);
if (fromShort != null)
{
Assign(result, uniformProperty, fromShort);
return true;
return Assign(result, uniformProperty, fromShort);
}
return false;
}

View File

@ -1,91 +1,37 @@
using DistTestCore.Codex;
using ArgsUniform;
using DistTestCore.Codex;
using Newtonsoft.Json;
namespace ContinuousTests
{
public class Configuration
{
public string LogPath { get; set; } = string.Empty;
public string DataPath { get; set; } = string.Empty;
[Uniform("log-path", "l", "LOGPATH", true, "Path where log files will be written.")]
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 bool KeepPassedTestLogs { get; set; }
}
public class ConfigLoader
{
private const string filename = "config.json";
public Configuration Load()
public Configuration Load(string[] args)
{
var config = Read();
var uniformArgs = new ArgsUniform<Configuration>(args);
Validate(config);
return config;
}
var result = uniformArgs.Parse();
private Configuration Read()
{
if (File.Exists(filename))
{
var lines = File.ReadAllText(filename);
try
{
var result = JsonConvert.DeserializeObject<Configuration>(lines);
if (result != null) return result;
}
catch { }
}
result.CodexDeployment = ParseCodexDeploymentJson(result.CodexDeploymentJson);
var logPath = "logs";// Environment.GetEnvironmentVariable("LOGPATH");
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.");
}
return result;
}
private CodexDeployment ParseCodexDeploymentJson(string filename)

View File

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

View File

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

View File

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