From c40d45e429b83b421db2e2fccf9061dbee2068ea Mon Sep 17 00:00:00 2001 From: Santiago Squarzon Date: Thu, 16 Jan 2025 14:47:08 -0300 Subject: [PATCH] another try --- src/PSTree/CommandWithPathBase.cs | 28 +++++++++++------ src/PSTree/Extensions/PathExtensions.cs | 40 ++++++++++++++++++++----- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/PSTree/CommandWithPathBase.cs b/src/PSTree/CommandWithPathBase.cs index 8621261..f6ba882 100644 --- a/src/PSTree/CommandWithPathBase.cs +++ b/src/PSTree/CommandWithPathBase.cs @@ -53,22 +53,30 @@ protected IEnumerable 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) { @@ -76,13 +84,15 @@ protected IEnumerable EnumerateResolvedPaths() 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; } } } diff --git a/src/PSTree/Extensions/PathExtensions.cs b/src/PSTree/Extensions/PathExtensions.cs index 4b2f651..05c2146 100644 --- a/src/PSTree/Extensions/PathExtensions.cs +++ b/src/PSTree/Extensions/PathExtensions.cs @@ -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);