Skip to content

Commit

Permalink
Remove InstallName() (#1506)
Browse files Browse the repository at this point in the history
* remove InstallName dead code

* code cleanup
  • Loading branch information
anamnavi authored Jan 8, 2024
1 parent e511b71 commit c64719e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 106 deletions.
75 changes: 9 additions & 66 deletions src/code/ACRServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio

/// <summary>
/// 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"
Expand All @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions src/code/LocalServerApiCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio

/// <summary>
/// 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"
Expand All @@ -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;
}

Expand Down
14 changes: 10 additions & 4 deletions src/code/NuGetServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio

/// <summary>
/// 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"
Expand All @@ -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;
}

Expand Down
37 changes: 10 additions & 27 deletions src/code/V2ServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio

/// <summary>
/// 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"
Expand All @@ -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;
}

Expand Down Expand Up @@ -1112,28 +1117,6 @@ private string FindVersionGlobbing(string packageName, VersionRange versionRange
return HttpRequestCall(requestUrlV2, out errRecord);
}

/// <summary>
/// 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.
/// </summary>
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;
}

/// <summary>
/// Installs package with specific name and version.
/// Name: no wildcard support.
Expand Down
15 changes: 10 additions & 5 deletions src/code/V3ServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ public override FindResults FindVersionWithTag(string packageName, string versio

/// <summary>
/// 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"
Expand All @@ -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;
}

Expand Down

0 comments on commit c64719e

Please sign in to comment.