Skip to content

Commit

Permalink
(chocolatey#72) Add ability to include sources from config
Browse files Browse the repository at this point in the history
This adds the --include-sources-from-config switch which will append
any sources in the config to manually specified sources (via --source).
It checks if the source already exists before appending, which prevents
duplication when the manually specified sources and the sources from
config are combined.
  • Loading branch information
TheCakeIsNaOH committed Jul 1, 2023
1 parent d836138 commit a74edc7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public void Should_add_short_version_of_password_to_the_option_set()
{
_optionSet.Contains("p").Should().BeTrue();
}

[Fact]
public void Should_add_include_sources_from_config_to_the_option_set()
{
_optionSet.Contains("include-sources-from-config").Should().BeTrue();
}
}

public class When_handling_validation : ChocolateyInfoCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ public void Should_add_short_version_of_skip_hooks_to_the_option_set()
{
_optionSet.Contains("skiphooks").Should().BeTrue();
}

[Fact]
public void Should_add_include_sources_from_config_to_the_option_set()
{
_optionSet.Contains("include-sources-from-config").Should().BeTrue();
}
}

public class When_handling_additional_argument_parsing : ChocolateyInstallCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public void Should_add_ignore_pinned_to_the_option_set()
{
_optionSet.Contains("ignore-pinned").Should().BeTrue();
}

[Fact]
public void Should_add_include_sources_from_config_to_the_option_set()
{
_optionSet.Contains("include-sources-from-config").Should().BeTrue();
}
}

public class When_noop_is_called : ChocolateyOutdatedCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ public void Should_add_short_version_of_skip_hooks_to_the_option_set()
{
_optionSet.Contains("skiphooks").Should().BeTrue();
}

[Fact]
public void Should_add_include_sources_from_config_to_the_option_set()
{
_optionSet.Contains("include-sources-from-config").Should().BeTrue();
}
}

public class When_handling_additional_argument_parsing : ChocolateyUpgradeCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public override void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConf
configuration.Features.UsePackageRepositoryOptimizations = false;
}
})
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 2.2.0+",
option => configuration.IncludeMachineSources = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfi
"Skip hooks - Do not run hook scripts. Available in 1.2.0+",
option => configuration.SkipHookScripts = option != null
)
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 2.2.0+",
option => configuration.IncludeMachineSources = option != null
)
;

//todo: #770 package name can be a url / installertype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfi
configuration.Features.UsePackageRepositoryOptimizations = false;
}
})
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 2.2.0+",
option => configuration.IncludeMachineSources = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfi
configuration.Features.UsePackageRepositoryOptimizations = false;
}
})
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 2.2.0+",
option => configuration.IncludeMachineSources = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfi
"Skip hooks - Do not run hook scripts. Available in 1.2.0+",
option => configuration.SkipHookScripts = option != null
)
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 2.2.0+",
option => configuration.IncludeMachineSources = option != null
)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private void AppendOutput(StringBuilder propertyValues, string append)
public string Sources { get; set; }

public string SourceType { get; set; }
public bool IncludeMachineSources { get; set; }

// top level commands

Expand Down
25 changes: 25 additions & 0 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ private ICommand FindCommand(ChocolateyConfiguration config, Container container
}

SetSourceType(config, container);
IncludeMachineSources(config);

// guaranteed that all settings are set.
EnvironmentSettings.SetEnvironmentVariables(config);

Expand Down Expand Up @@ -127,6 +129,29 @@ private void SetSourceType(ChocolateyConfiguration config, Container container)
this.Log().Debug(() => "The source '{0}' evaluated to a '{1}' source type".FormatWith(config.Sources, sourceType));
}

private void IncludeMachineSources(ChocolateyConfiguration config)
{
if (config.IncludeMachineSources)
{
if (config.SourceType != SourceTypes.NORMAL)
{
this.Log().Warn("Not including sources from config because {0} is an alternate source".format_with(config.Sources));
}
else
{
foreach (var machineSource in config.MachineSources.or_empty_list_if_null())
{
if (!config.Sources.contains(machineSource.Key))
{
config.Sources += ";" + machineSource.Key;
}
}

this.Log().Debug("Including sources from config");
}
}
}

public void FailOnMissingOrInvalidLicenseIfFeatureSet(ChocolateyConfiguration config)
{
if (!config.Features.FailOnInvalidOrMissingLicense ||
Expand Down

0 comments on commit a74edc7

Please sign in to comment.