namespace OpenNest.Server;
/// Startup settings read from the command line and environment.
public static class ServerOptions
{
public const string DatabaseVariable = "OPENNEST_DB";
public const string UpdatesVariable = "OPENNEST_UPDATES";
///
/// True unless OPENNEST_UPDATES is off: the server mirrors the desktop
/// app's update feed from GitHub (see ).
///
public static bool UpdatesEnabled(string? value) =>
!string.Equals(value?.Trim(), "off", StringComparison.OrdinalIgnoreCase);
/// The mirrored update feed lives in an updates directory beside the database.
public static string UpdatesPath(string databasePath) =>
Path.Combine(Path.GetDirectoryName(Path.GetFullPath(databasePath))!, "updates");
///
/// 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");
}
}