Skip to content

Commit

Permalink
another try
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Jan 16, 2025
1 parent 1620d1e commit c40d45e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
28 changes: 19 additions & 9 deletions src/PSTree/CommandWithPathBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,46 @@ protected IEnumerable<string> EnumerateResolvedPaths()
{
if (IsLiteral)
{
string resolvedPath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(
path: path,
string resolved = SessionState.Path.GetUnresolvedProviderPathFromPSPath(
path: path.Normalize(),
provider: out provider,
drive: out _);

if (provider.Validate(resolvedPath, this))
if (!provider.ValidateProvider())
{
yield return resolvedPath;
WriteError(provider.ToInvalidProviderError(resolved));
continue;
}

if (!resolved.ValidateExists())
{
WriteError(resolved.ToInvalidPathError());
continue;
}

yield return resolved;
continue;
}

try
{
resolvedPaths = GetResolvedProviderPathFromPSPath(path, out provider);
resolvedPaths = GetResolvedProviderPathFromPSPath(path.Normalize(), out provider);
}
catch (Exception exception)
{
WriteError(exception.ToResolvePathError(path));
continue;
}


foreach (string resolvedPath in resolvedPaths)
foreach (string resolved in resolvedPaths)
{
if (provider.Validate(resolvedPath, this))
if (!provider.ValidateProvider())
{
yield return resolvedPath;
WriteError(provider.ToInvalidProviderError(resolved));
continue;
}

yield return resolved;
}
}
}
Expand Down
40 changes: 32 additions & 8 deletions src/PSTree/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,47 @@ namespace PSTree.Extensions;

internal static class PathExtensions
{
internal static bool Validate(
this ProviderInfo provider,
string path,
PSCmdlet cmdlet)
internal static bool ValidateProvider(this ProviderInfo provider)
{
if (provider.ImplementingType == typeof(FileSystemProvider))
{
return true;
}

ErrorRecord error = provider.ToInvalidProviderError(path);
cmdlet.WriteError(error);
return false;
}

internal static bool Exists(this string path) =>
File.Exists(path) || Directory.Exists(path);
internal static string Normalize(this string path)
{
if (!path.ValidateExists())
{
return path;
}

path = path.TrimEnd(['\\', '/']);

if (string.IsNullOrWhiteSpace(path))
{
return Path.DirectorySeparatorChar.ToString();
}

if (Path.GetPathRoot(path) == path)
{
return string.Concat(path, Path.DirectorySeparatorChar);
}

return path;
}

internal static bool ValidateExists(this string path)
{
if (File.Exists(path) || Directory.Exists(path))
{
return true;
}

return false;
}

internal static bool IsHidden(this FileSystemInfo item) =>
item.Attributes.HasFlag(FileAttributes.Hidden);
Expand Down

0 comments on commit c40d45e

Please sign in to comment.