From 64ba9c4950e56a3d5b68c1bd01024e55c1f9ad66 Mon Sep 17 00:00:00 2001 From: Rasmus Mikkelsen Date: Wed, 19 Jan 2022 21:05:46 +0100 Subject: [PATCH] Fix defaults (#63) --- RELEASE_NOTES.md | 3 ++ .../ExplicitTests/GitHubReleaseCookTests.cs | 1 + .../Helpers/TestEnvironmentVariables.cs | 2 + .../DotNetComposerServiceTests.cs | 1 + .../UnitTests/Commands/CommandFactoryTests.cs | 2 +- .../Ingredients/Gathers/GitHubGatherTests.cs | 2 +- .../Services/DestinationParserTests.cs | 4 +- .../Bake/Cooking/Composers/DotNetComposer.cs | 2 +- .../Gathers/DynamicDestinationGather.cs | 2 +- .../Ingredients/Gathers/GitHubGather.cs | 5 +- Source/Bake/Core/Credentials.cs | 6 ++- Source/Bake/Core/Defaults.cs | 46 ++++++++++++++++--- Source/Bake/Core/EnvironmentVariables.cs | 2 +- Source/Bake/Core/IDefaults.cs | 12 +++-- Source/Bake/Executor.cs | 8 +++- Source/Bake/Services/DestinationParser.cs | 2 +- 16 files changed, 76 insertions(+), 24 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8d773519..a39ac102 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -14,6 +14,9 @@ drop: - all ``` + * New: Now possible to change Bake internal defaults via environment + variables. More of these will be exposed in upcomming releases + * Fixed: Docker Hub push URL should just be the username # 0.6-beta diff --git a/Source/Bake.Tests/ExplicitTests/GitHubReleaseCookTests.cs b/Source/Bake.Tests/ExplicitTests/GitHubReleaseCookTests.cs index f8356dd3..34b39f17 100644 --- a/Source/Bake.Tests/ExplicitTests/GitHubReleaseCookTests.cs +++ b/Source/Bake.Tests/ExplicitTests/GitHubReleaseCookTests.cs @@ -91,6 +91,7 @@ protected override IServiceCollection Configure(IServiceCollection serviceCollec .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton(TestEnvironmentVariables.None) .AddSingleton(new TestEnvironmentVariables(new Dictionary { ["github_personal_token"] = GetToken(), diff --git a/Source/Bake.Tests/Helpers/TestEnvironmentVariables.cs b/Source/Bake.Tests/Helpers/TestEnvironmentVariables.cs index 67560d00..8d83caac 100644 --- a/Source/Bake.Tests/Helpers/TestEnvironmentVariables.cs +++ b/Source/Bake.Tests/Helpers/TestEnvironmentVariables.cs @@ -29,6 +29,8 @@ namespace Bake.Tests.Helpers { public class TestEnvironmentVariables : IEnvironmentVariables { + public static IEnvironmentVariables None { get; } = new TestEnvironmentVariables(new Dictionary()); + private readonly IReadOnlyDictionary _environmentVariables; public TestEnvironmentVariables( diff --git a/Source/Bake.Tests/IntegrationTests/ServiceTests/DotNetComposerServiceTests.cs b/Source/Bake.Tests/IntegrationTests/ServiceTests/DotNetComposerServiceTests.cs index 1d94ab8c..acfb05c2 100644 --- a/Source/Bake.Tests/IntegrationTests/ServiceTests/DotNetComposerServiceTests.cs +++ b/Source/Bake.Tests/IntegrationTests/ServiceTests/DotNetComposerServiceTests.cs @@ -63,6 +63,7 @@ protected override IServiceCollection Configure(IServiceCollection serviceCollec .AddTransient() .AddTransient() .AddTransient() + .AddSingleton(TestEnvironmentVariables.None) .AddTransient() .AddTransient() .AddTransient(); diff --git a/Source/Bake.Tests/UnitTests/Commands/CommandFactoryTests.cs b/Source/Bake.Tests/UnitTests/Commands/CommandFactoryTests.cs index 1276d408..aa665f5b 100644 --- a/Source/Bake.Tests/UnitTests/Commands/CommandFactoryTests.cs +++ b/Source/Bake.Tests/UnitTests/Commands/CommandFactoryTests.cs @@ -40,7 +40,7 @@ public class CommandFactoryTests : TestFor [SetUp] public void SetUp() { - Inject(new DestinationParser(new Defaults())); + Inject(new DestinationParser(new Defaults(TestEnvironmentVariables.None))); } [Command("A", "B")] diff --git a/Source/Bake.Tests/UnitTests/Ingredients/Gathers/GitHubGatherTests.cs b/Source/Bake.Tests/UnitTests/Ingredients/Gathers/GitHubGatherTests.cs index 3c7e0d43..4ad009b7 100644 --- a/Source/Bake.Tests/UnitTests/Ingredients/Gathers/GitHubGatherTests.cs +++ b/Source/Bake.Tests/UnitTests/Ingredients/Gathers/GitHubGatherTests.cs @@ -39,7 +39,7 @@ public class GitHubGatherTests : TestFor [SetUp] public void SetUp() { - Inject(new Defaults()); + Inject(new Defaults(TestEnvironmentVariables.None)); } [TestCase("https://github.com/rasmus/Bake.git", "rasmus", "Bake", "https://api.github.com/")] diff --git a/Source/Bake.Tests/UnitTests/Services/DestinationParserTests.cs b/Source/Bake.Tests/UnitTests/Services/DestinationParserTests.cs index 4cd9d295..e421ae12 100644 --- a/Source/Bake.Tests/UnitTests/Services/DestinationParserTests.cs +++ b/Source/Bake.Tests/UnitTests/Services/DestinationParserTests.cs @@ -36,7 +36,7 @@ public class DestinationParserTests : TestFor [SetUp] public void SetUp() { - Inject(new Defaults()); + Inject(new Defaults(TestEnvironmentVariables.None)); } [TestCase( @@ -58,7 +58,7 @@ public void NuGetRegistry( [TestCase( "container>rasmus", - "registry.hub.docker.com/rasmus/")] + "rasmus/")] [TestCase( "container>localhost:5000", "localhost:5000/")] diff --git a/Source/Bake/Cooking/Composers/DotNetComposer.cs b/Source/Bake/Cooking/Composers/DotNetComposer.cs index 0f4e206b..19b11a0a 100644 --- a/Source/Bake/Cooking/Composers/DotNetComposer.cs +++ b/Source/Bake/Cooking/Composers/DotNetComposer.cs @@ -284,7 +284,7 @@ private Recipe CreateRestoreRecipe( var gitHub = ingredients.GitHub; if (gitHub != null) { - sources.Add(_defaults.GitHubNuGetRegistry.AbsoluteUri.Replace("/OWNER/", $"/{gitHub.Owner}/")); + sources.Add(_defaults.GitHubNuGetRegistry.Replace("/OWNER/", $"/{gitHub.Owner}/")); } return new DotNetRestoreSolutionRecipe( diff --git a/Source/Bake/Cooking/Ingredients/Gathers/DynamicDestinationGather.cs b/Source/Bake/Cooking/Ingredients/Gathers/DynamicDestinationGather.cs index e7bc85f6..9b0e2d5a 100644 --- a/Source/Bake/Cooking/Ingredients/Gathers/DynamicDestinationGather.cs +++ b/Source/Bake/Cooking/Ingredients/Gathers/DynamicDestinationGather.cs @@ -153,7 +153,7 @@ private async Task ExtractNuGetDestinationAsync( { case Names.DynamicDestinations.GitHub: var gitHubInformation = await ingredients.GitHubTask; - var url = new Uri(_defaults.GitHubNuGetRegistry.AbsoluteUri.Replace("/OWNER/", $"/{gitHubInformation.Owner}/"), UriKind.Absolute); + var url = new Uri(_defaults.GitHubNuGetRegistry.Replace("/OWNER/", $"/{gitHubInformation.Owner}/"), UriKind.Absolute); var nugetRegistryDestination = new NuGetRegistryDestination(url); ingredients.Destinations.Add(nugetRegistryDestination); diff --git a/Source/Bake/Cooking/Ingredients/Gathers/GitHubGather.cs b/Source/Bake/Cooking/Ingredients/Gathers/GitHubGather.cs index 77e59b1d..af9fd04d 100644 --- a/Source/Bake/Cooking/Ingredients/Gathers/GitHubGather.cs +++ b/Source/Bake/Cooking/Ingredients/Gathers/GitHubGather.cs @@ -66,7 +66,8 @@ public async Task GatherAsync( } Uri apiUrl; - if (string.Equals(gitInformation.OriginUrl.Host, _defaults.GitHubUrl.Host, StringComparison.OrdinalIgnoreCase)) + var gitHubUrl = new Uri(_defaults.GitHubUrl); + if (string.Equals(gitInformation.OriginUrl.Host, gitHubUrl.Host, StringComparison.OrdinalIgnoreCase)) { apiUrl = GitHubApiUrl; _logger.LogInformation( @@ -115,7 +116,7 @@ public async Task GatherAsync( repo = repo[..lastIndex]; } - var url = new Uri($"{_defaults.GitHubUrl.Scheme}://{_defaults.GitHubUrl.Host}/{match.Groups["owner"].Value}/{repo}"); + var url = new Uri($"{gitHubUrl.Scheme}://{gitHubUrl.Host}/{match.Groups["owner"].Value}/{repo}"); ingredients.GitHub = new GitHubInformation( match.Groups["owner"].Value, repo, diff --git a/Source/Bake/Core/Credentials.cs b/Source/Bake/Core/Credentials.cs index 08de79c7..1992ff3a 100644 --- a/Source/Bake/Core/Credentials.cs +++ b/Source/Bake/Core/Credentials.cs @@ -71,12 +71,14 @@ public async Task TryGetNuGetApiKeyAsync( $"bake_credentials_nuget_{hostname}_apikey" }; - if (string.Equals(url.Host, _defaults.GitHubNuGetRegistry.Host, StringComparison.OrdinalIgnoreCase)) + var gitHubNuGetRegistry = new Uri(_defaults.GitHubNuGetRegistry, UriKind.Absolute); + if (string.Equals(url.Host, gitHubNuGetRegistry.Host, StringComparison.OrdinalIgnoreCase)) { possibilities.AddRange(GitHubTokenPossibilities); } - if (string.Equals(url.Host, _defaults.NuGetRegistry.Host, StringComparison.OrdinalIgnoreCase)) + var nuGetRegistry = new Uri(_defaults.NuGetRegistry, UriKind.Absolute); + if (string.Equals(url.Host, nuGetRegistry.Host, StringComparison.OrdinalIgnoreCase)) { possibilities.Add("nuget_apikey"); } diff --git a/Source/Bake/Core/Defaults.cs b/Source/Bake/Core/Defaults.cs index 021713a4..8937b6f8 100644 --- a/Source/Bake/Core/Defaults.cs +++ b/Source/Bake/Core/Defaults.cs @@ -20,18 +20,50 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; - // ReSharper disable StringLiteralTypo +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + namespace Bake.Core { public class Defaults : IDefaults { - public Uri GitHubUrl { get; } = new("https://github.com/", UriKind.Absolute); - public Uri GitHubNuGetRegistry { get; } = new("https://nuget.pkg.github.com/OWNER/index.json"); - public Uri NuGetRegistry { get; } = new("https://api.nuget.org/v3/index.json"); - public string DockerHubUserRegistry { get; } = new("registry.hub.docker.com/{USER}/"); - public string GitHubUserRegistry { get; } = new("ghcr.io/{USER}/"); + private readonly IEnvironmentVariables _environmentVariables; + + public string GitHubUrl { get; private set; } = "https://github.com/"; + public string GitHubNuGetRegistry { get; private set; } = "https://nuget.pkg.github.com/OWNER/index.json"; + public string NuGetRegistry { get; private set; } = "https://api.nuget.org/v3/index.json"; + public string DockerHubUserRegistry { get; private set; } = "{USER}/"; + public string GitHubUserRegistry { get; private set; } = "ghcr.io/{USER}/"; + + public Defaults( + IEnvironmentVariables environmentVariables) + { + _environmentVariables = environmentVariables; + } + + public async Task InitializeAsync( + CancellationToken cancellationToken) + { + var e = await _environmentVariables.GetAsync(cancellationToken); + + GitHubUrl = Get(e, "github_url", GitHubUrl); + GitHubNuGetRegistry = Get(e, "github_packages_nuget_url", GitHubNuGetRegistry); + GitHubUserRegistry = Get(e, "github_packages_container_url", GitHubUserRegistry); + NuGetRegistry = Get(e, "nuget_url", NuGetRegistry); + DockerHubUserRegistry = Get(e, "dockerhub_user_url", DockerHubUserRegistry); + } + + private static string Get( + IReadOnlyDictionary environmentVariables, + string name, + string defaultValue) + { + return environmentVariables.TryGetValue($"bake_defaults_{name}", out var v) && !string.IsNullOrEmpty(v) + ? v + : defaultValue; + } } } diff --git a/Source/Bake/Core/EnvironmentVariables.cs b/Source/Bake/Core/EnvironmentVariables.cs index 8c190126..d2418225 100644 --- a/Source/Bake/Core/EnvironmentVariables.cs +++ b/Source/Bake/Core/EnvironmentVariables.cs @@ -30,7 +30,7 @@ namespace Bake.Core { public class EnvironmentVariables : IEnvironmentVariables { - private readonly Lazy>> _environmentVariables = new Lazy>>( + private readonly Lazy>> _environmentVariables = new( GetEnvironmentVariablesAsync, LazyThreadSafetyMode.ExecutionAndPublication); diff --git a/Source/Bake/Core/IDefaults.cs b/Source/Bake/Core/IDefaults.cs index 838503d4..0321cce2 100644 --- a/Source/Bake/Core/IDefaults.cs +++ b/Source/Bake/Core/IDefaults.cs @@ -20,16 +20,20 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; +using System.Threading; +using System.Threading.Tasks; namespace Bake.Core { public interface IDefaults { - Uri GitHubUrl { get; } - Uri GitHubNuGetRegistry { get; } - Uri NuGetRegistry { get; } + string GitHubUrl { get; } + string GitHubNuGetRegistry { get; } + string NuGetRegistry { get; } string DockerHubUserRegistry { get; } string GitHubUserRegistry { get; } + + Task InitializeAsync( + CancellationToken cancellationToken); } } diff --git a/Source/Bake/Executor.cs b/Source/Bake/Executor.cs index 969b0377..a1b9532d 100644 --- a/Source/Bake/Executor.cs +++ b/Source/Bake/Executor.cs @@ -25,17 +25,21 @@ using System.Threading; using System.Threading.Tasks; using Bake.Commands; +using Bake.Core; namespace Bake { public class Executor : IExecutor { private readonly ICommandFactory _commandFactory; + private readonly IDefaults _defaults; public Executor( - ICommandFactory commandFactory) + ICommandFactory commandFactory, + IDefaults defaults) { _commandFactory = commandFactory; + _defaults = defaults; } public async Task ExecuteAsync( @@ -43,6 +47,8 @@ public async Task ExecuteAsync( IReadOnlyCollection commandTypes, CancellationToken cancellationToken) { + await _defaults.InitializeAsync(cancellationToken); + var app = _commandFactory.Create(commandTypes); return await app.ExecuteAsync(args, cancellationToken); diff --git a/Source/Bake/Services/DestinationParser.cs b/Source/Bake/Services/DestinationParser.cs index a6c1817b..8cf54214 100644 --- a/Source/Bake/Services/DestinationParser.cs +++ b/Source/Bake/Services/DestinationParser.cs @@ -64,7 +64,7 @@ public bool TryParse(string str, out Destination destination) case Names.ArtifactTypes.NuGet: if (parts.Length == 1) { - destination = new NuGetRegistryDestination(_defaults.NuGetRegistry); + destination = new NuGetRegistryDestination(new Uri(_defaults.NuGetRegistry, UriKind.Absolute)); return true; }