Skip to content

Add debug statements #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .ci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,23 @@ jobs:
inputs:
azureSubscription: PSResourceGetACR
azurePowerShellVersion: LatestVersion
pwsh: true
ScriptType: InlineScript
inline: |
$modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules'
$env:PSModulePath = $modulePath + [System.IO.Path]::PathSeparator + $env:PSModulePath
$testModuleName = "test-module"
$ACRRepoName = "ACRRepo"
$ACRRepoUri = "https://psresourcegettest.azurecr.io"
Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose
Write-Verbose -Verbose "Registering ACR repository with Az authentication completed"
Get-PSResourceRepository -Name $ACRRepoName -Verbose
Write-Verbose -Verbose "Get-PSResourceRepository completed"
Write-Verbose -Verbose "Finding resource with Name: $testModuleName"
Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -ErrorAction Stop
Write-Verbose -Verbose "Importing build utilities (buildtools.psd1)"
Import-Module -Name (Join-Path -Path '${{ parameters.buildDirectory }}' -ChildPath 'buildtools.psd1') -Force
Invoke-ModuleTestsACR -Type Functional
Expand Down
180 changes: 103 additions & 77 deletions src/code/FindPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
public sealed class FindPSResource : PSCmdlet
{
#region Members

private const string NameParameterSet = "NameParameterSet";
private const string CommandNameParameterSet = "CommandNameParameterSet";
private const string DscResourceNameParameterSet = "DscResourceNameParameterSet";
Expand All @@ -39,7 +39,7 @@ public sealed class FindPSResource : PSCmdlet
/// Specifies name of a resource or resources to find. Accepts wild card characters.
/// </summary>
[SupportsWildcards]
[Parameter(Position = 0,
[Parameter(Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = NameParameterSet)]
Expand Down Expand Up @@ -115,15 +115,20 @@ public sealed class FindPSResource : PSCmdlet

protected override void BeginProcessing()
{
WriteVerbose("Beginning Find-PSResource processing");
_cancellationTokenSource = new CancellationTokenSource();

var networkCred = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null;

WriteVerbose("Creating FindHelper instance");

_findHelper = new FindHelper(
cancellationToken: _cancellationTokenSource.Token,
cmdletPassedIn: this,
networkCredential: networkCred);

WriteVerbose("FindHelper instance created successfully");

// Create a repository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();
Expand Down Expand Up @@ -167,99 +172,120 @@ protected override void ProcessRecord()

private void ProcessResourceNameParameterSet()
{
WriteDebug("In FindPSResource::ProcessResourceNameParameterSet()");
// only cases where Name is allowed to not be specified is if Type or Tag parameters are
if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name)))
try
{
if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag)))
{
ProcessTags();
return;
}
else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type)))
WriteDebug("Testing");
WriteDebug("In FindPSResource::ProcessResourceNameParameterSet() XXXXXXXXXXX");
WriteVerbose("checking if Name parameter is specified");

var check = MyInvocation?.BoundParameters?.ContainsKey(nameof(Name));

WriteVerbose("Value of check for Name parameter: " + check);

// only cases where Name is allowed to not be specified is if Type or Tag parameters are
if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name)))
{
Name = new string[] {"*"};
WriteDebug("Name parameter not provided, checking for Type or Tag parameters");
if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag)))
{
ProcessTags();
return;
}
else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type)))
{
Name = new string[] { "*" };
}
else
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."),
"NameParameterNotProvided",
ErrorCategory.InvalidOperation,
this));
}
}
else

WriteVerbose("Processing Name parameter for Find-PSResource cmdlet");
WriteDebug("Filtering package name(s) on wildcards");
Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard);
WriteVerbose($"Name parameter processed, contains {Name.Length} entries");

foreach (string error in errorMsgs)
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."),
"NameParameterNotProvided",
ErrorCategory.InvalidOperation,
WriteError(new ErrorRecord(
new PSInvalidOperationException(error),
"ErrorFilteringNamesForUnsupportedWildcards",
ErrorCategory.InvalidArgument,
this));
}
}

WriteDebug("Filtering package name(s) on wildcards");
Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard);

foreach (string error in errorMsgs)
{
WriteError(new ErrorRecord(
new PSInvalidOperationException(error),
"ErrorFilteringNamesForUnsupportedWildcards",
ErrorCategory.InvalidArgument,
this));
}

// this catches the case where Name wasn't passed in as null or empty,
// but after filtering out unsupported wildcard names there are no elements left in namesToSearch
if (Name.Length == 0)
{
WriteDebug("Package name(s) could not be resolved");
return;
}
// this catches the case where Name wasn't passed in as null or empty,
// but after filtering out unsupported wildcard names there are no elements left in namesToSearch
if (Name.Length == 0)
{
WriteDebug("Package name(s) could not be resolved");
return;
}

// determine/parse out Version param
VersionType versionType = VersionType.VersionRange;
NuGetVersion nugetVersion = null;
VersionRange versionRange = null;
// determine/parse out Version param
VersionType versionType = VersionType.VersionRange;
NuGetVersion nugetVersion = null;
VersionRange versionRange = null;

if (Version != null)
{
WriteDebug("Parsing package version");
if (!NuGetVersion.TryParse(Version, out nugetVersion))
if (Version != null)
{
if (Version.Trim().Equals("*"))
WriteDebug("Parsing package version");
if (!NuGetVersion.TryParse(Version, out nugetVersion))
{
versionRange = VersionRange.All;
versionType = VersionType.VersionRange;
if (Version.Trim().Equals("*"))
{
versionRange = VersionRange.All;
versionType = VersionType.VersionRange;
}
else if (!VersionRange.TryParse(Version, out versionRange))
{
WriteError(new ErrorRecord(
new ArgumentException("Argument for -Version parameter is not in the proper format"),
"IncorrectVersionFormat",
ErrorCategory.InvalidArgument,
this));

return;
}
}
else if (!VersionRange.TryParse(Version, out versionRange))
else
{
WriteError(new ErrorRecord(
new ArgumentException("Argument for -Version parameter is not in the proper format"),
"IncorrectVersionFormat",
ErrorCategory.InvalidArgument,
this));

return;
versionType = VersionType.SpecificVersion;
}
}
else
{
versionType = VersionType.SpecificVersion;
versionType = VersionType.NoVersion;
}
}
else
{
versionType = VersionType.NoVersion;
}

foreach (PSResourceInfo pkg in _findHelper.FindByResourceName(
name: Name,
type: Type,
versionRange: versionRange,
nugetVersion: nugetVersion,
versionType: versionType,
version: Version,
prerelease: Prerelease,
tag: Tag,
repository: Repository,
includeDependencies: IncludeDependencies,
suppressErrors: false))
foreach (PSResourceInfo pkg in _findHelper.FindByResourceName(
name: Name,
type: Type,
versionRange: versionRange,
nugetVersion: nugetVersion,
versionType: versionType,
version: Version,
prerelease: Prerelease,
tag: Tag,
repository: Repository,
includeDependencies: IncludeDependencies,
suppressErrors: false))
{
WriteObject(pkg);
}
}
catch (Exception ex)
{
WriteObject(pkg);
WriteError(new ErrorRecord(
ex,
"FindPSResourceError",
ErrorCategory.NotSpecified,
this));
}
}

Expand Down Expand Up @@ -289,7 +315,7 @@ private void ProcessCommandOrDscParameterSet(bool isSearchingForCommands)
WriteDebug("Command or DSCResource name(s) could not be resolved");
return;
}

foreach (PSCommandResourceInfo cmdPkg in _findHelper.FindByCommandOrDscResource(
isSearchingForCommands: isSearchingForCommands,
prerelease: Prerelease,
Expand Down Expand Up @@ -325,7 +351,7 @@ private void ProcessTags()
WriteDebug("Tags(s) could not be resolved");
return;
}

foreach (PSResourceInfo tagPkg in _findHelper.FindByTag(
type: Type,
prerelease: Prerelease,
Expand Down
8 changes: 8 additions & 0 deletions src/code/GetPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ public sealed class GetPSResourceRepository : PSCmdlet

protected override void BeginProcessing()
{
WriteVerbose("Beginning Get-PSResourceRepository processing");
RepositorySettings.CheckRepositoryStore();
WriteVerbose("Repository store checked successfully");
}

protected override void ProcessRecord()
{
WriteVerbose("Processing Get-PSResourceRepository cmdlet");
string nameArrayAsString = (Name == null || !Name.Any() || string.Equals(Name[0], "*") || Name[0] == null)
? "all" : string.Join(", ", Name);
WriteDebug($"Reading repository info for '{nameArrayAsString}'");
List<PSRepositoryInfo> items = RepositorySettings.Read(Name, out string[] errorList);
WriteVerbose($"Read {items.Count} repositories");

// handle non-terminating errors
foreach (string error in errorList)
Expand All @@ -57,10 +61,14 @@ protected override void ProcessRecord()
this));
}

WriteVerbose($"Returning {items.Count} repositories");

foreach (PSRepositoryInfo repo in items)
{
WriteObject(repo);
}

WriteVerbose("Get-PSResourceRepository cmdlet processing complete");
}

#endregion
Expand Down
Loading
Loading