feat: load application and repository configuration from env and TOML
This commit is contained in:
commit
ae8680d275
17 files changed
+301
No files matched your search
@@ -0,0 +1,52 @@
|
||||
using DotNetEnv;
|
||||
|
||||
namespace ForgejoReleaseSync.Services;
|
||||
|
||||
public class AppConfig
|
||||
{
|
||||
public required string ForgejoInstacne { get; set; }
|
||||
public required string ForgejoToken { get; set; }
|
||||
public required ushort SyncConcurrency { get; set; }
|
||||
public required string RepositoryConfigFilePath { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public static class AppConfigLoader
|
||||
{
|
||||
public static AppConfig LoadConfigFromEnv()
|
||||
{
|
||||
Env.Load();
|
||||
|
||||
var forgejoInstance = Environment.GetEnvironmentVariable("FORGEJO_INSTANCE");
|
||||
var forgejoToken = Environment.GetEnvironmentVariable("FORGEJO_TOKEN");
|
||||
var syncConcurrency = Environment.GetEnvironmentVariable("SYNC_CONCURRENCY");
|
||||
var repoConfigFilePath = Environment.GetEnvironmentVariable("REPO_CONFIG_FILE");
|
||||
|
||||
if (string.IsNullOrEmpty(forgejoInstance))
|
||||
throw new Exception("FORGEJO_INSTANCE environment variable is required");
|
||||
if (!string.IsNullOrEmpty(forgejoToken) && forgejoInstance.EndsWith('/'))
|
||||
forgejoInstance = forgejoInstance[..^1];
|
||||
|
||||
if (string.IsNullOrEmpty(forgejoToken))
|
||||
throw new Exception("FORGEJO_TOKEN environment variable is required");
|
||||
|
||||
ushort syncConcurrencyParsed = 3;
|
||||
if (!string.IsNullOrEmpty(syncConcurrency))
|
||||
ushort.TryParse(syncConcurrency, out syncConcurrencyParsed);
|
||||
if (syncConcurrencyParsed > 10)
|
||||
throw new ArgumentOutOfRangeException(nameof(syncConcurrencyParsed),
|
||||
"Sync concurrency cannot be greater than 10");
|
||||
|
||||
if (string.IsNullOrEmpty(repoConfigFilePath))
|
||||
throw new Exception("REPO_CONFIG_FILE environment variable is required");
|
||||
|
||||
|
||||
return new AppConfig
|
||||
{
|
||||
ForgejoInstacne = forgejoInstance,
|
||||
ForgejoToken = forgejoToken,
|
||||
SyncConcurrency = syncConcurrencyParsed,
|
||||
RepositoryConfigFilePath = repoConfigFilePath
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user