namespace OpenNest.Server;
/// Startup settings read from the command line and environment.
public static class ServerOptions
{
public const string DatabaseVariable = "OPENNEST_DB";
///
/// The database path: --database=<path> or --database <path>, then
/// OPENNEST_DB, then ./data/nests.db. A --database without a path,
/// or given twice, fails startup instead of silently storing nests elsewhere.
///
public static string DatabasePath(IReadOnlyList args, string? environmentPath)
{
string? path = null;
for (var i = 0; i < args.Count; i++)
{
string value;
if (args[i] == "--database")
{
if (i + 1 >= args.Count || args[i + 1].StartsWith("--", StringComparison.Ordinal))
throw new ArgumentException("--database requires a path.", nameof(args));
value = args[++i];
}
else if (args[i].StartsWith("--database=", StringComparison.Ordinal))
{
value = args[i]["--database=".Length..];
}
else
{
continue;
}
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("--database requires a path.", nameof(args));
if (path is not null)
throw new ArgumentException("--database was given more than once.", nameof(args));
path = value;
}
return path ?? environmentPath ?? Path.Combine("data", "nests.db");
}
}