Skip to content

Commit

Permalink
Added appget list command. Towards appget#22
Browse files Browse the repository at this point in the history
  • Loading branch information
kayone committed Feb 18, 2019
1 parent 3a01715 commit 208eb65
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/AppGet/AppGet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
<Compile Include="Commands\IVariableInteractivityCommand.cs" />
<Compile Include="Commands\Clean\CleanCommandHandler.cs" />
<Compile Include="Commands\Clean\CleanOptions.cs" />
<Compile Include="Commands\List\ListCommandHandler.cs" />
<Compile Include="Commands\List\ListOptions.cs" />
<Compile Include="Commands\UpdateAll\UpdateAllCommandHandler.cs" />
<Compile Include="Commands\UpdateAll\UpdateAllOptions.cs" />
<Compile Include="Commands\Update\UpdateCommandHandler.cs" />
Expand Down
23 changes: 18 additions & 5 deletions src/AppGet/CommandLine/TableHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AppGet.Manifest;
using AppGet.Update;

Expand All @@ -21,13 +20,27 @@ public static void ShowTable(this IEnumerable<PackageManifest> packages)
}


public static void ShowTable(this IEnumerable<PackageUpdate> updates)
public static void ShowTable(this IEnumerable<PackageUpdate> updates, bool printStatus = true)
{
var table = new ConsoleTable("Name", "Package ID", "Installed Version", "Available Version");
var columns = new List<string> { "Name", "Package ID", "Installed Version", "Available Version" };

foreach (var update in updates.OrderBy(c => c.PackageId))
if (printStatus)
{
table.AddRow(update.Name, update.PackageId, update.InstalledVersion, update.AvailableVersion);
columns.Add("Status");
}

var table = new ConsoleTable(columns.ToArray());

foreach (var update in updates)
{
var cols = new List<object> { update.Name, update.PackageId, update.InstalledVersion, update.AvailableVersion };

if (printStatus)
{
cols.Add(update.Status);
}

table.AddRow(cols.ToArray());
}

Print(table);
Expand Down
42 changes: 42 additions & 0 deletions src/AppGet/Commands/List/ListCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using System.Threading.Tasks;
using AppGet.CommandLine;
using AppGet.Infrastructure.Composition;
using AppGet.Update;
using Colorful;
using NLog;

namespace AppGet.Commands.List
{
[Handles(typeof(ListOptions))]
public class ListCommandHandler : ICommandHandler
{
private readonly UpdateService _updateService;
private readonly Logger _logger;

public ListCommandHandler(UpdateService updateService, Logger logger)
{
_updateService = updateService;
_logger = logger;
}


public async Task Execute(AppGetOption commandOptions)
{
var matches = await _updateService.GetUpdates();

var sorted = matches.OrderByDescending(c => c.Status);

sorted.ShowTable();

Console.WriteLine();
_logger.Info($"Total Applications: {matches.Count:n0} Updates Available: {matches.Count(c => c.Status == UpdateStatus.Available):n0}");
Console.WriteLine();

if (matches.Any(c => c.Status == UpdateStatus.Available))
{
Console.WriteLine("Run 'appget update-all' to apply all updates.");
}
}
}
}
9 changes: 9 additions & 0 deletions src/AppGet/Commands/List/ListOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CommandLine;

namespace AppGet.Commands.List
{
[Verb("list", HelpText = "List all currently installed packages known to AppGet")]
public class ListOptions : AppGetOption
{
}
}
25 changes: 19 additions & 6 deletions src/AppGet/Commands/Outdated/OutdatedCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AppGet.Infrastructure.Composition;
using AppGet.Update;
using Colorful;
using NLog;

namespace AppGet.Commands.Outdated
{
Expand All @@ -13,22 +14,28 @@ namespace AppGet.Commands.Outdated
public class OutdatedCommandHandler : ICommandHandler
{
private readonly UpdateService _updateService;
private readonly Logger _logger;

public OutdatedCommandHandler(UpdateService updateService)
public OutdatedCommandHandler(UpdateService updateService, Logger logger)
{
_updateService = updateService;
_logger = logger;
}


public async Task Execute(AppGetOption commandOptions)
{
var matches = await _updateService.GetUpdates();

var updates = matches.Where(c => c.Status == UpdateStatus.Available).ToList();
var updates = matches
.Where(c => c.Status == UpdateStatus.Available)
.OrderBy(c => c.PackageId)
.ToList();

if (updates.Any())
{
Console.WriteLine("{0} Available Updates:", updates.Count);
updates.ShowTable();
Console.WriteLine($"{updates.Count} Available Updates:");
updates.ShowTable(false);

Console.WriteLine("");
}
Expand All @@ -43,11 +50,17 @@ public async Task Execute(AppGetOption commandOptions)
Console.WriteLine("");
Console.WriteLine("Latest Version Already Installed:", Color.Green);

var upToDate = matches.Where(c => c.Status == UpdateStatus.UpToDate);
var upToDate = matches
.Where(c => c.Status == UpdateStatus.UpToDate)
.OrderBy(c => c.PackageId);

upToDate.ShowTable();
upToDate.ShowTable(false);
}

Console.WriteLine();
_logger.Info($"Total Applications: {matches.Count:n0} Updates Available: {matches.Count(c => c.Status == UpdateStatus.Available):n0}");
Console.WriteLine();

if (updates.Any())
{
Console.WriteLine("Run 'appget update-all' to apply all updates.");
Expand Down
6 changes: 1 addition & 5 deletions src/AppGet/Update/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ public async Task<List<PackageUpdate>> GetUpdates()
_logger.Debug("Getting list of installed application");
var records = _windowsInstallerClient.GetRecords();

_logger.Info("Getting list of available updates...");
_logger.Info("Getting list of matching packages...");
var updates = await _novoClient.GetUpdates(records);

Console.WriteLine();
_logger.Info("Total Applications: {0:n0} Updates Available: {1:n0}", updates.Count, updates.Count(c => c.Status == UpdateStatus.Available));
Console.WriteLine();

return updates;
}

Expand Down

0 comments on commit 208eb65

Please sign in to comment.