Add automatic input downloading

Since we no longer include input files with the solution, per AoC guidelines, this will enable other users to use this application (after specifying their session token) without manually grabbing all the appropriate download files.
This commit is contained in:
2024-12-01 11:13:56 -06:00
parent 8120a52ba0
commit 0359b86174
7 changed files with 184 additions and 42 deletions

65
src/Util/DotEnv.cs Normal file
View File

@ -0,0 +1,65 @@
namespace aoc2024.Util;
internal static class DotEnv
{
internal static string GetDotEnvContents(string fromPath = "")
{
if (string.IsNullOrEmpty(fromPath))
{
fromPath = Directory.GetCurrentDirectory();
}
try
{
var dir = new DirectoryInfo(fromPath);
while (dir != null)
{
var dotEnv = Path.Combine(dir.FullName, ".env");
if (File.Exists(dotEnv))
{
return dotEnv;
}
dir = dir.Parent;
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Exception searching for .env path from {fromPath}: {ex}");
}
return "";
}
internal static bool SetEnvironment(string fromPath = "")
{
var dotEnv = GetDotEnvContents(fromPath);
if (string.IsNullOrEmpty(dotEnv))
{
return false;
}
var lines = File.ReadAllLines(dotEnv);
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i].Trim();
if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#'))
{
continue;
}
var parts = line.Split('=');
if (parts.Length != 2)
{
Logger.LogErrorLine($"DotEnv file {dotEnv} line {i + 1} does not match expected `key=value` format. Line: {line}");
continue;
}
System.Diagnostics.Debug.WriteLine($"Setting environment variable `{parts[0]}` = `{parts[1]}`");
Environment.SetEnvironmentVariable(parts[0], parts[1]);
}
return true;
}
}