From 389c1a52a9d2e06ce1302fe5eac5dc79bcbe70ee Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Wed, 4 Oct 2023 10:12:44 -0700 Subject: [PATCH] Add internal for setting caller as compat cmdlet (#1432) --- src/code/InternalHooks.cs | 18 ++++++++++++++++++ src/code/ServerFactory.cs | 8 ++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/code/InternalHooks.cs diff --git a/src/code/InternalHooks.cs b/src/code/InternalHooks.cs new file mode 100644 index 000000000..9b5ea0294 --- /dev/null +++ b/src/code/InternalHooks.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Reflection; + +namespace Microsoft.PowerShell.PSResourceGet.UtilClasses +{ + public class InternalHooks + { + internal static bool InvokedFromCompat; + + public static void SetTestHook(string property, object value) + { + var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic); + fieldInfo?.SetValue(null, value); + } + } +} \ No newline at end of file diff --git a/src/code/ServerFactory.cs b/src/code/ServerFactory.cs index 441e5eef4..8be08662e 100644 --- a/src/code/ServerFactory.cs +++ b/src/code/ServerFactory.cs @@ -25,7 +25,11 @@ static UserAgentInfo() private static string _psResourceGetVersion; private static string _distributionChannel; - internal static string UserAgentString => $"PSResourceGet/{_psResourceGetVersion} PowerShell/{_psVersion} DistributionChannel/{_distributionChannel}"; + internal static string UserAgentString() + { + string psGetCompat = InternalHooks.InvokedFromCompat ? "true" : "false"; + return $"PSResourceGet/{_psResourceGetVersion} PowerShell/{_psVersion} DistributionChannel/{_distributionChannel} PowerShellGetCompat/{psGetCompat}"; + } } internal class ServerFactory @@ -34,7 +38,7 @@ public static ServerApiCall GetServer(PSRepositoryInfo repository, PSCmdlet cmdl { PSRepositoryInfo.APIVersion repoApiVersion = repository.ApiVersion; ServerApiCall currentServer = null; - string userAgentString = UserAgentInfo.UserAgentString; + string userAgentString = UserAgentInfo.UserAgentString(); switch (repoApiVersion) {