From beeb54b0066511128ea5ef2ed43618a22e41f2c3 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 5 Jan 2024 13:04:58 -0500 Subject: [PATCH 1/2] remove InstallName dead code --- src/code/ACRServerAPICalls.cs | 75 ++++----------------------------- src/code/InstallHelper.cs | 1 + src/code/LocalServerApiCalls.cs | 12 ++++-- src/code/NuGetServerAPICalls.cs | 12 ++++-- src/code/V2ServerAPICalls.cs | 37 +++++----------- src/code/V3ServerAPICalls.cs | 13 +++--- 6 files changed, 44 insertions(+), 106 deletions(-) diff --git a/src/code/ACRServerAPICalls.cs b/src/code/ACRServerAPICalls.cs index 0d2ad65ae..2f2d3114a 100644 --- a/src/code/ACRServerAPICalls.cs +++ b/src/code/ACRServerAPICalls.cs @@ -530,6 +530,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio /// /// Installs a specific package. + /// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined. + /// Therefore, package version should not be null in this method. /// Name: no wildcard support. /// Examples: Install "PowerShellGet" /// Install "PowerShellGet" -Version "3.0.0" @@ -540,76 +542,17 @@ public override Stream InstallPackage(string packageName, string packageVersion, Stream results = new MemoryStream(); if (string.IsNullOrEmpty(packageVersion)) { - results = InstallName(packageName, out errRecord); - } - else - { - results = InstallVersion(packageName, packageVersion, out errRecord); - } - - return results; - } - - private Stream InstallName( - string moduleName, - out ErrorRecord errRecord) - { - errRecord = null; - string accessToken = string.Empty; - string tenantID = string.Empty; - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - string moduleVersion = String.Empty; - - var repositoryCredentialInfo = Repository.CredentialInfo; - if (repositoryCredentialInfo != null) - { - accessToken = Utils.GetACRAccessTokenFromSecretManagement( - Repository.Name, - repositoryCredentialInfo, + errRecord = new ErrorRecord( + exception: new ArgumentNullException($"Package version could not be found for {packageName}"), + "PackageVersionNullOrEmptyError", + ErrorCategory.InvalidArgument, _cmdletPassedIn); - _cmdletPassedIn.WriteVerbose("Access token retrieved."); - - tenantID = repositoryCredentialInfo.SecretName; - _cmdletPassedIn.WriteVerbose($"Tenant ID: {tenantID}"); - } - - // Call asynchronous network methods in a try/catch block to handle exceptions. - string registry = Repository.Uri.Host; - - _cmdletPassedIn.WriteVerbose("Getting acr refresh token"); - var acrRefreshToken = GetAcrRefreshToken(registry, tenantID, accessToken, out errRecord); - if (errRecord != null) - { - return null; - } - - _cmdletPassedIn.WriteVerbose("Getting acr access token"); - var acrAccessToken = GetAcrAccessToken(registry, acrRefreshToken, out errRecord); - if (errRecord != null) - { - return null; - } - - _cmdletPassedIn.WriteVerbose($"Getting manifest for {moduleName} - {moduleVersion}"); - var manifest = GetAcrRepositoryManifestAsync(registry, moduleName, moduleVersion, acrAccessToken, out errRecord); - if (errRecord != null) - { - return null; - } - - string digest = GetDigestFromManifest(manifest, out errRecord); - if (errRecord != null) - { - return null; + return results; } - _cmdletPassedIn.WriteVerbose($"Downloading blob for {moduleName} - {moduleVersion}"); - // TODO: error handling here? - var responseContent = GetAcrBlobAsync(registry, moduleName, digest, acrAccessToken).Result; - - return responseContent.ReadAsStreamAsync().Result; + results = InstallVersion(packageName, packageVersion, out errRecord); + return results; } private Stream InstallVersion( diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 1459971a4..0e374e2a7 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -842,6 +842,7 @@ private Hashtable BeginPackageInstall( { // Download the package. string pkgName = pkgToInstall.Name; + Console.WriteLine($"ANAM package version: {pkgVersion}"); Stream responseStream = currentServer.InstallPackage(pkgName, pkgVersion, _prerelease, out ErrorRecord installNameErrRecord); if (installNameErrRecord != null) { diff --git a/src/code/LocalServerApiCalls.cs b/src/code/LocalServerApiCalls.cs index 2bd862be3..f9e69f35e 100644 --- a/src/code/LocalServerApiCalls.cs +++ b/src/code/LocalServerApiCalls.cs @@ -227,12 +227,16 @@ public override Stream InstallPackage(string packageName, string packageVersion, Stream results = new MemoryStream(); if (string.IsNullOrEmpty(packageVersion)) { - results = InstallName(packageName, includePrerelease, out errRecord); - } - else { - results = InstallVersion(packageName, packageVersion, out errRecord); + errRecord = new ErrorRecord( + exception: new ArgumentNullException($"Package version could not be found for {packageName}"), + "PackageVersionNullOrEmptyError", + ErrorCategory.InvalidArgument, + _cmdletPassedIn); + + return results; } + results = InstallVersion(packageName, packageVersion, out errRecord); return results; } diff --git a/src/code/NuGetServerAPICalls.cs b/src/code/NuGetServerAPICalls.cs index 7748695c6..f27fde3e9 100644 --- a/src/code/NuGetServerAPICalls.cs +++ b/src/code/NuGetServerAPICalls.cs @@ -403,12 +403,16 @@ public override Stream InstallPackage(string packageName, string packageVersion, Stream results = new MemoryStream(); if (string.IsNullOrEmpty(packageVersion)) { - results = InstallName(packageName, out errRecord); - } - else { - results = InstallVersion(packageName, packageVersion, out errRecord); + errRecord = new ErrorRecord( + exception: new ArgumentNullException($"Package version could not be found for {packageName}"), + "PackageVersionNullOrEmptyError", + ErrorCategory.InvalidArgument, + _cmdletPassedIn); + + return results; } + results = InstallVersion(packageName, packageVersion, out errRecord); return results; } diff --git a/src/code/V2ServerAPICalls.cs b/src/code/V2ServerAPICalls.cs index d0a766377..c492e58aa 100644 --- a/src/code/V2ServerAPICalls.cs +++ b/src/code/V2ServerAPICalls.cs @@ -666,6 +666,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio /// /// Installs a specific package. + /// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined. + /// Therefore, package version should not be null in this method. /// Name: no wildcard support. /// Examples: Install "PowerShellGet" /// Install "PowerShellGet" -Version "3.0.0" @@ -675,13 +677,16 @@ public override Stream InstallPackage(string packageName, string packageVersion, Stream results = new MemoryStream(); if (string.IsNullOrEmpty(packageVersion)) { - results = InstallName(packageName, out errRecord); - } - else - { - results = InstallVersion(packageName, packageVersion, out errRecord); + errRecord = new ErrorRecord( + exception: new ArgumentNullException($"Package version could not be found for {packageName}"), + "PackageVersionNullOrEmptyError", + ErrorCategory.InvalidArgument, + _cmdletPassedIn); + + return results; } + results = InstallVersion(packageName, packageVersion, out errRecord); return results; } @@ -1112,28 +1117,6 @@ private string FindVersionGlobbing(string packageName, VersionRange versionRange return HttpRequestCall(requestUrlV2, out errRecord); } - /// - /// Installs specific package. - /// Name: no wildcard support. - /// Examples: Install "PowerShellGet" - /// Implementation Note: if not prerelease: https://www.powershellgallery.com/api/v2/package/powershellget (Returns latest stable) - /// if prerelease, call into InstallVersion instead. - /// - private Stream InstallName(string packageName, out ErrorRecord errRecord) - { - _cmdletPassedIn.WriteDebug("In V2ServerAPICalls::InstallName()"); - var requestUrlV2 = $"{Repository.Uri}/package/{packageName}"; - var response = HttpRequestCallForContent(requestUrlV2, out errRecord); - if (errRecord != null) - { - return new MemoryStream(); - } - - var responseStream = response.ReadAsStreamAsync().Result; - - return responseStream; - } - /// /// Installs package with specific name and version. /// Name: no wildcard support. diff --git a/src/code/V3ServerAPICalls.cs b/src/code/V3ServerAPICalls.cs index dec0da5ea..7237c07b3 100644 --- a/src/code/V3ServerAPICalls.cs +++ b/src/code/V3ServerAPICalls.cs @@ -295,13 +295,16 @@ public override Stream InstallPackage(string packageName, string packageVersion, Stream results = new MemoryStream(); if (string.IsNullOrEmpty(packageVersion)) { - results = InstallName(packageName, out errRecord); - } - else - { - results = InstallVersion(packageName, packageVersion, out errRecord); + errRecord = new ErrorRecord( + exception: new ArgumentNullException($"Package version could not be found for {packageName}"), + "PackageVersionNullOrEmptyError", + ErrorCategory.InvalidArgument, + _cmdletPassedIn); + + return results; } + results = InstallVersion(packageName, packageVersion, out errRecord); return results; } From 4f4a3945d0657827894eee0fc2ee431eb6d6e84d Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 5 Jan 2024 13:09:51 -0500 Subject: [PATCH 2/2] code cleanup --- src/code/InstallHelper.cs | 1 - src/code/LocalServerApiCalls.cs | 2 ++ src/code/NuGetServerAPICalls.cs | 2 ++ src/code/V3ServerAPICalls.cs | 2 ++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 0e374e2a7..1459971a4 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -842,7 +842,6 @@ private Hashtable BeginPackageInstall( { // Download the package. string pkgName = pkgToInstall.Name; - Console.WriteLine($"ANAM package version: {pkgVersion}"); Stream responseStream = currentServer.InstallPackage(pkgName, pkgVersion, _prerelease, out ErrorRecord installNameErrRecord); if (installNameErrRecord != null) { diff --git a/src/code/LocalServerApiCalls.cs b/src/code/LocalServerApiCalls.cs index f9e69f35e..c32f3db57 100644 --- a/src/code/LocalServerApiCalls.cs +++ b/src/code/LocalServerApiCalls.cs @@ -218,6 +218,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio /// /// Installs a specific package. + /// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined. + /// Therefore, package version should not be null in this method. /// Name: no wildcard support. /// Examples: Install "PowerShellGet" /// Install "PowerShellGet" -Version "3.0.0" diff --git a/src/code/NuGetServerAPICalls.cs b/src/code/NuGetServerAPICalls.cs index f27fde3e9..73b7dfa93 100644 --- a/src/code/NuGetServerAPICalls.cs +++ b/src/code/NuGetServerAPICalls.cs @@ -394,6 +394,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio /// /// Installs a specific package. + /// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined. + /// Therefore, package version should not be null in this method. /// Name: no wildcard support. /// Examples: Install "PowerShellGet" /// Install "PowerShellGet" -Version "3.0.0" diff --git a/src/code/V3ServerAPICalls.cs b/src/code/V3ServerAPICalls.cs index 7237c07b3..fd7c39e63 100644 --- a/src/code/V3ServerAPICalls.cs +++ b/src/code/V3ServerAPICalls.cs @@ -285,6 +285,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio /// /// Installs a specific package. + /// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined. + /// Therefore, package version should not be null in this method. /// Name: no wildcard support. /// Examples: Install "PowerShellGet" /// Install "PowerShellGet" -Version "3.0.0"