Skip to content

Commit 9257d44

Browse files
committed
Add option to silence informational tool messages
Note that dotnet tool messages themselves will not be muted (i.e. tool installed/updated).
1 parent 9d1cb90 commit 9257d44

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ Arguments:
2121
Options:
2222
-s, --source <source> NuGet feed to check for updates. [default: https://api.nuget.org/v3/index.json]
2323
-i, --interval <interval> Time interval in seconds for the update checks. [default: 5]
24+
-q, --quiet Do not display any informational messages.
2425
-?, -h, --help Show help and usage information
2526
--version Show version information
27+
2628
```
2729

2830
Features:

src/dotnet-evergreen/Application.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4-
using System.Runtime.InteropServices;
54
using System.Threading;
65
using Spectre.Console;
76

@@ -10,16 +9,22 @@ namespace Devlooped
109
class Application
1110
{
1211
readonly CancellationTokenSource shutdownSource = new CancellationTokenSource();
12+
readonly bool quiet;
1313
bool stoppingChildProcess = false;
1414

1515
public CancellationToken ShutdownToken => shutdownSource.Token;
1616

17-
public Application() => Console.CancelKeyPress += (s, e) => e.Cancel = OnCancelKeyPress();
17+
public Application(bool quiet = false)
18+
{
19+
this.quiet = quiet;
20+
Console.CancelKeyPress += (s, e) => e.Cancel = OnCancelKeyPress();
21+
}
1822

1923
public Process Start(ProcessStartInfo start, CancellationTokenSource cancellation)
2024
{
2125
var process = Process.Start(start);
22-
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)}:{process!.Id} Started[/]");
26+
if (!quiet)
27+
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)}:{process!.Id} Started[/]");
2328

2429
process!.EnableRaisingEvents = true;
2530
var cancelled = false;
@@ -56,19 +61,22 @@ public Process Start(ProcessStartInfo start, CancellationTokenSource cancellatio
5661

5762
process.Exited += (s, e) =>
5863
{
59-
if (!cancelled)
60-
{
61-
if (process.ExitCode == 0)
62-
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)} Exited[/]");
63-
else
64-
AnsiConsole.MarkupLine($"[red]{Path.GetFileNameWithoutExtension(start.FileName)} Exited[/]");
65-
}
66-
else
64+
if (!quiet)
6765
{
68-
if (ShutdownToken.IsCancellationRequested)
69-
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)} Shutdown[/]");
66+
if (!cancelled)
67+
{
68+
if (process.ExitCode == 0)
69+
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)} Exited[/]");
70+
else
71+
AnsiConsole.MarkupLine($"[red]{Path.GetFileNameWithoutExtension(start.FileName)} Exited[/]");
72+
}
7073
else
71-
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)} Restarting[/]");
74+
{
75+
if (ShutdownToken.IsCancellationRequested)
76+
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)} Shutdown[/]");
77+
else
78+
AnsiConsole.MarkupLine($"[grey]{Path.GetFileNameWithoutExtension(start.FileName)} Restarting[/]");
79+
}
7280
}
7381

7482
// If the tool exits with an error, exit the application too,
@@ -91,7 +99,9 @@ bool OnCancelKeyPress()
9199
// process ourselves (i.e. a Ctrl+C is actually entered manually by the user).
92100
if (!stoppingChildProcess)
93101
{
94-
AnsiConsole.MarkupLine("[yellow]Shutting down...[/]");
102+
if (!quiet)
103+
AnsiConsole.MarkupLine("[yellow]Shutting down...[/]");
104+
95105
shutdownSource.Cancel();
96106
}
97107

src/dotnet-evergreen/Program.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
const string NuGetFeed = "https://api.nuget.org/v3/index.json";
1717

1818
var config = Config.Build().GetSection("evergreen");
19-
var app = new Application();
2019

2120
// Only append symbols we can't gather from configuration, which makes it
2221
// possible to avoid consuming arguments from the tool being executed.
@@ -28,10 +27,11 @@
2827
var toolArgs = new Argument<string[]>("args", "Additional arguments and options supported by the tool");
2928
var sourceOpt = new Option<string>(new[] { "-s", "--source", "/s", "/source" }, () => source, "NuGet feed to check for updates.");
3029
var intervalOpt = new Option<int>(new[] { "-i", "--interval", "/i", "/interval" }, () => interval.GetValueOrDefault(5), "Time interval in seconds for the update checks.");
30+
var quietOpt = new Option(new[] { "-q", "--quiet", "/q", "/quiet" }, "Do not display any informational messages.");
3131
var helpOpt = new Option(new[] { "-h", "/h", "--help", "-?", "/?" });
3232

3333
// First do full parse to detect tool/source
34-
var parser = new Parser(toolArg, toolArgs, intervalOpt, sourceOpt, helpOpt);
34+
var parser = new Parser(toolArg, toolArgs, intervalOpt, sourceOpt, quietOpt, helpOpt);
3535
var result = parser.Parse(args);
3636
var tool = result.ValueForArgument(toolArg);
3737

@@ -41,6 +41,7 @@
4141
toolArgs,
4242
sourceOpt,
4343
intervalOpt,
44+
quietOpt,
4445
})
4546
.UseHelpBuilder(context => new ToolHelpBuilder(context.Console))
4647
.UseHelp()
@@ -53,11 +54,14 @@
5354

5455
// Now that we know we have a tool command, strip the arguments *after* the tool
5556
// and re-parse the options, just so we don't accidentally grab a tool option as ours.
56-
result = new Parser(sourceOpt, intervalOpt, helpOpt).Parse(arguments.Take(arguments.IndexOf(tool!)).ToArray());
57+
result = new Parser(sourceOpt, intervalOpt, quietOpt, helpOpt).Parse(arguments.Take(arguments.IndexOf(tool!)).ToArray());
5758

5859
if (result.FindResultFor(helpOpt) != null)
5960
return ShowHelp();
6061

62+
var quiet = result.FindResultFor(quietOpt) != null;
63+
var app = new Application(quiet);
64+
6165
interval = result.ValueForOption(intervalOpt);
6266
source = result.ValueForOption(sourceOpt);
6367

@@ -91,7 +95,7 @@
9195
Timer? timer = null;
9296
timer = new Timer(_ => CheckUpdates(), default, TimeSpan.FromSeconds((double)interval), TimeSpan.FromSeconds((double)interval));
9397

94-
if (!app.ShutdownToken.IsCancellationRequested)
98+
if (!app.ShutdownToken.IsCancellationRequested && !quiet)
9599
AnsiConsole.MarkupLine("[grey]Press Ctrl+C to exit.[/]");
96100

97101
while (!app.ShutdownToken.IsCancellationRequested)
@@ -110,7 +114,9 @@ void CheckUpdates()
110114
var update = await tools.FindUpdateAsync(info.PackageId, info.Version, source!);
111115
if (update != null)
112116
{
113-
AnsiConsole.MarkupLine($"[yellow]Update v{update.ToNormalizedString()} found.[/]");
117+
if (!quiet)
118+
AnsiConsole.MarkupLine($"[yellow]Update v{update.ToNormalizedString()} found.[/]");
119+
114120
// Causes the running tool to be stopped while we update. See Application.Start.
115121
toolCancellation.Cancel();
116122

0 commit comments

Comments
 (0)