From c64719ed3f35c31255de275ec119aebe34b5e13e Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 8 Jan 2024 14:14:35 -0500 Subject: [PATCH] Remove InstallName() (#1506) * remove InstallName dead code * code cleanup --- src/code/ACRServerAPICalls.cs | 75 ++++----------------------------- src/code/LocalServerApiCalls.cs | 14 ++++-- src/code/NuGetServerAPICalls.cs | 14 ++++-- src/code/V2ServerAPICalls.cs | 37 +++++----------- src/code/V3ServerAPICalls.cs | 15 ++++--- 5 files changed, 49 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/LocalServerApiCalls.cs b/src/code/LocalServerApiCalls.cs index 2bd862be3..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" @@ -227,12 +229,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..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" @@ -403,12 +405,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..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" @@ -295,13 +297,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; }