diff --git a/src/NuGetMirror/ListCommand.cs b/src/NuGetMirror/ListCommand.cs index e949a3f..2adcbd1 100644 --- a/src/NuGetMirror/ListCommand.cs +++ b/src/NuGetMirror/ListCommand.cs @@ -3,7 +3,7 @@ using System.Globalization; using System.Linq; using System.Threading; -using NuGetMirror; +using System.Threading.Tasks; using McMaster.Extensions.CommandLineUtils; using NuGet.CatalogReader; using NuGet.Common; @@ -28,14 +28,10 @@ private static void Run(CommandLineApplication cmd, HttpSource? httpSource, ILog { cmd.Description = "List packages from a v3 source."; - var start = cmd.Option("-s|--start", "Beginning of the commit time range. Packages commited AFTER this time will be included.", CommandOptionType.SingleValue); - var end = cmd.Option("-e|--end", "End of the commit time range. Packages commited at this time will be included.", CommandOptionType.SingleValue); + var start = cmd.Option("-s|--start", "Beginning of the commit time range.", CommandOptionType.SingleValue); + var end = cmd.Option("-e|--end", "End of the commit time range.", CommandOptionType.SingleValue); var verbose = cmd.Option("-v|--verbose", "Write out additional network call information.", CommandOptionType.NoValue); - - var argRoot = cmd.Argument( - "[root]", - "V3 feed index.json URI", - multipleValues: false); + var argRoot = cmd.Argument("[root]", "V3 feed index.json URI", multipleValues: false); cmd.HelpOption(Constants.HelpOption); @@ -48,28 +44,34 @@ private static void Run(CommandLineApplication cmd, HttpSource? httpSource, ILog throw new ArgumentException("Provide the full http url to a v3 nuget feed or a locally configured source name."); } - // Attempts to load a configured source (including inactive ones) and fallback to Uri parsing if it fails - Uri? index; - var sources = PackageSourceProvider.LoadPackageSources(Settings.LoadDefaultSettings(Environment.CurrentDirectory)); + var sources = PackageSourceProvider.LoadPackageSources( + Settings.LoadDefaultSettings(Environment.CurrentDirectory)); + var source = sources.FirstOrDefault(o => o.Name == argRoot.Value); + Uri index; + var localHttpSource = httpSource; + if (source != null) { index = source.SourceUri; - httpSource = HttpSource.Create(Repository.Factory.GetCoreV3(source)); + localHttpSource = HttpSource.Create(Repository.Factory.GetCoreV3(source)); } else { - if (!Uri.TryCreate(argRoot.Value!, UriKind.Absolute, out index)) + if (!Uri.TryCreate(argRoot.Value, UriKind.Absolute, out index!)) { - // The enumerable is safe to re-iterate because it's a List implementation - Debug.Assert(sources is ICollection, "Sources implementation changed"); + Debug.Assert(sources is ICollection); var sourceNames = string.Join(", ", sources.Select(o => o.Name)); - throw new ArgumentException($"Invalid feed identifier: '{argRoot.Value}'. Provide the full http url to a v3 nuget feed or a locally configured identifier. Configured sources are: {sourceNames}"); + throw new ArgumentException( + $"Invalid feed identifier: '{argRoot.Value}'. " + + $"Configured sources are: {sourceNames}"); } if (!index.AbsolutePath.EndsWith("/index.json", StringComparison.OrdinalIgnoreCase)) { - throw new ArgumentException($"Invalid feed url: '{argRoot.Value}'. Provide the full http url to a v3 nuget feed."); + throw new ArgumentException( + $"Invalid feed url: '{argRoot.Value}'. " + + "Provide the full http url to a v3 nuget feed."); } } @@ -88,39 +90,36 @@ private static void Run(CommandLineApplication cmd, HttpSource? httpSource, ILog if (log is ConsoleLogger consoleLogger) { - if (verbose.HasValue()) - { - consoleLogger.VerbosityLevel = LogLevel.Information; - } - else - { - consoleLogger.VerbosityLevel = LogLevel.Minimal; - } + consoleLogger.VerbosityLevel = verbose.HasValue() + ? LogLevel.Information + : LogLevel.Minimal; } - // CatalogReader - using (var cacheContext = new SourceCacheContext()) - using (var catalogReader = new CatalogReader(index!, httpSource, cacheContext, TimeSpan.Zero, log)) - { - var entries = await catalogReader.GetFlattenedEntriesAsync(startTime, endTime, CancellationToken.None); + using var cacheContext = new SourceCacheContext(); + using var catalogReader = new CatalogReader( + index, + localHttpSource ?? throw new InvalidOperationException("HttpSource is required"), + cacheContext, + TimeSpan.Zero, + log); - foreach (var entry in entries - .OrderBy(e => e.Id, StringComparer.OrdinalIgnoreCase) - .ThenBy(e => e.Version)) - { - log.LogMinimal($"{entry.Id} {entry.Version.ToFullString()}"); - } - }; + var entries = await catalogReader.GetFlattenedEntriesAsync(startTime, endTime, CancellationToken.None); + + foreach (var entry in entries + .OrderBy(e => e.Id, StringComparer.OrdinalIgnoreCase) + .ThenBy(e => e.Version)) + { + log.LogMinimal($"{entry.Id} {entry.Version.ToFullString()}"); + } return 0; } catch (Exception ex) { ExceptionUtils.LogException(ex, log); + return 1; } - - return 1; }); } } -} \ No newline at end of file +}