Skip to content

Commit 654963b

Browse files
authored
Fix environment variable processing. (#406) (#407)
1 parent 6adda0e commit 654963b

19 files changed

+53
-46
lines changed

src/Shared/DefaultHttpClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.CST.OpenSource
1212
public sealed class DefaultHttpClientFactory : IHttpClientFactory
1313
{
1414
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
15-
private string ENV_HTTPCLIENT_USER_AGENT = "microsoft_oss_gadget (https://github.com/microsoft/OSSGadget)";
15+
public string ENV_HTTPCLIENT_USER_AGENT { get; set; } = "microsoft_oss_gadget (https://github.com/microsoft/OSSGadget)";
1616

1717
public DefaultHttpClientFactory(string? userAgent = null)
1818
{

src/Shared/Helpers/EnvironmentHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ public class EnvironmentHelper
1616
/// <param name="targetObject"> Examine this object (using reflection) </param>
1717
public static void OverrideEnvironmentVariables(object targetObject)
1818
{
19-
foreach (FieldInfo fieldInfo in targetObject.GetType().GetFields(BindingFlags.Public))
19+
foreach (PropertyInfo propertyInfo in targetObject.GetType().GetProperties())
2020
{
21-
if (fieldInfo.FieldType == typeof(string) &&
22-
fieldInfo.Name.StartsWith("ENV_") &&
23-
fieldInfo.Name.Length > 4)
21+
if (propertyInfo.PropertyType == typeof(string) &&
22+
propertyInfo.Name.StartsWith("ENV_") &&
23+
propertyInfo.Name.Length > 4)
2424
{
25-
string? bareName = fieldInfo.Name[4..];
25+
string? bareName = propertyInfo.Name[4..];
2626

2727
string? value = Environment.GetEnvironmentVariable(bareName);
2828
if (value != null)
2929
{
30-
Logger.Debug("Assiging value of {0} to {1}", bareName, fieldInfo.Name);
31-
fieldInfo.SetValue(targetObject, value);
30+
Logger.Debug("Assiging value of {0} to {1}", bareName, propertyInfo.Name);
31+
propertyInfo.SetValue(targetObject, value);
3232
}
3333
}
3434
}

src/Shared/Metadata/BaseMetadataSource.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Microsoft.CST.OpenSource;
44

5+
using Helpers;
56
using System.Threading.Tasks;
67
using System.Text.Json;
78
using PackageUrl;
@@ -21,6 +22,7 @@ public abstract class BaseMetadataSource
2122

2223
public BaseMetadataSource()
2324
{
25+
EnvironmentHelper.OverrideEnvironmentVariables(this);
2426
ServiceProvider serviceProvider = new ServiceCollection()
2527
.AddHttpClient()
2628
.BuildServiceProvider();

src/Shared/Metadata/DepsDevMetadataSource.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ namespace Microsoft.CST.OpenSource;
1111

1212
public class DepsDevMetadataSource : BaseMetadataSource
1313
{
14-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
15-
public static string ENV_DEPS_DEV_ENDPOINT = "https://deps.dev/_";
14+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier",
15+
Justification = "Modified through reflection.")]
16+
public string ENV_DEPS_DEV_ENDPOINT { get; set; } = "https://deps.dev/_";
1617

1718
public static readonly List<string> VALID_TYPES = new List<string>() {
1819
"npm",

src/Shared/Metadata/LibrariesIoMetadataSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace Microsoft.CST.OpenSource;
1111
public class LibrariesIoMetadataSource : BaseMetadataSource
1212
{
1313
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
14-
public static string ENV_LIBRARIES_IO_ENDPOINT = "https://libraries.io/api";
15-
public static string? ENV_LIBRARIES_IO_API_KEY = null;
14+
public string ENV_LIBRARIES_IO_ENDPOINT { get; set; }= "https://libraries.io/api";
15+
public string? ENV_LIBRARIES_IO_API_KEY { get; set; }= null;
1616

1717
// Reload periodically from https://libraries.io/api/platforms
1818
// curl https://libraries.io/api/platforms | jq '.[].name' | sed 's/[A-Z]/\L&/g' | sed 's/$/,/g' | sort | sed '$ s/.$//'

src/Shared/OssGadgetLib.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.CST.OpenSource
1212
public abstract class OssGadgetLib
1313
{
1414
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
15-
protected static string ENV_HTTPCLIENT_USER_AGENT = "OSSDL";
15+
public string ENV_HTTPCLIENT_USER_AGENT { get; set; }= "OSSDL";
1616

1717
/// <summary>
1818
/// The <see cref="ProjectManagerFactory"/> to be used by classes that implement <see cref="OssGadgetLib"/>.

src/Shared/PackageManagers/CPANProjectManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ internal class CPANProjectManager : BaseProjectManager
2626
public override string ManagerType => Type;
2727

2828
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
29-
public string ENV_CPAN_BINARY_ENDPOINT = "https://cpan.metacpan.org";
29+
public string ENV_CPAN_BINARY_ENDPOINT { get; set; } = "https://cpan.metacpan.org";
3030

3131
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
32-
public string ENV_CPAN_ENDPOINT = "https://metacpan.org";
32+
public string ENV_CPAN_ENDPOINT { get; set; } = "https://metacpan.org";
3333

3434
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
35-
public string ENV_CPAN_API_ENDPOINT = "https://fastapi.metacpan.org";
35+
public string ENV_CPAN_API_ENDPOINT { get; set; } = "https://fastapi.metacpan.org";
3636

3737
public CPANProjectManager(IHttpClientFactory httpClientFactory, string destinationDirectory) : base(httpClientFactory, destinationDirectory)
3838
{

src/Shared/PackageManagers/CRANProjectManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class CRANProjectManager : BaseProjectManager
2424
public override string ManagerType => Type;
2525

2626
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
27-
public string ENV_CRAN_ENDPOINT = "https://cran.r-project.org";
27+
public string ENV_CRAN_ENDPOINT { get; set; } = "https://cran.r-project.org";
2828

2929
public CRANProjectManager(IHttpClientFactory httpClientFactory, string destinationDirectory) : base(httpClientFactory, destinationDirectory)
3030
{

src/Shared/PackageManagers/CargoProjectManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public class CargoProjectManager : TypedManager<IManagerPackageVersionMetadata,
3131
public override string ManagerType => Type;
3232

3333
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
34-
public string ENV_CARGO_ENDPOINT = "https://crates.io";
34+
public string ENV_CARGO_ENDPOINT { get; set; } = "https://crates.io";
3535

3636
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
37-
public string ENV_CARGO_ENDPOINT_STATIC = "https://static.crates.io";
37+
public string ENV_CARGO_ENDPOINT_STATIC { get; set; } = "https://static.crates.io";
3838

3939
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
40-
public string ENV_CARGO_INDEX_ENDPOINT = "https://raw.githubusercontent.com/rust-lang/crates.io-index/master";
40+
public string ENV_CARGO_INDEX_ENDPOINT { get; set; } = "https://raw.githubusercontent.com/rust-lang/crates.io-index/master";
4141

4242
public CargoProjectManager(
4343
string directory,

src/Shared/PackageManagers/CocoapodsProjectManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ internal class CocoapodsProjectManager : BaseProjectManager
2828
public override string ManagerType => Type;
2929

3030
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
31-
public string ENV_COCOAPODS_SPECS_ENDPOINT = "https://github.com/CocoaPods/Specs/tree/master";
31+
public string ENV_COCOAPODS_SPECS_ENDPOINT { get; set; } = "https://github.com/CocoaPods/Specs/tree/master";
3232

3333
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
34-
public string ENV_COCOAPODS_SPECS_RAW_ENDPOINT = "https://raw.githubusercontent.com/CocoaPods/Specs/master";
34+
public string ENV_COCOAPODS_SPECS_RAW_ENDPOINT { get; set; } = "https://raw.githubusercontent.com/CocoaPods/Specs/master";
3535

3636
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
37-
public string ENV_COCOAPODS_METADATA_ENDPOINT = "https://cocoapods.org";
37+
public string ENV_COCOAPODS_METADATA_ENDPOINT { get; set; } = "https://cocoapods.org";
3838

3939
public CocoapodsProjectManager(IHttpClientFactory httpClientFactory, string destinationDirectory) : base(httpClientFactory, destinationDirectory)
4040
{

0 commit comments

Comments
 (0)