|
1 | 1 | using ApiSdk; |
2 | 2 | using Azure.Identity; |
| 3 | +using DevLab.JmesPath; |
3 | 4 | using Microsoft.Extensions.Configuration; |
4 | 5 | using Microsoft.Extensions.DependencyInjection; |
5 | 6 | using Microsoft.Extensions.Hosting; |
|
25 | 26 | using System.Threading.Tasks; |
26 | 27 | using System.Collections.Generic; |
27 | 28 | using System; |
| 29 | +using System.CommandLine.IO; |
28 | 30 |
|
29 | 31 | namespace Microsoft.Graph.Cli |
30 | 32 | { |
@@ -80,14 +82,22 @@ static async Task<int> Main(string[] args) |
80 | 82 | var builder = BuildCommandLine(client, commands).UseDefaults().UseHost(CreateHostBuilder); |
81 | 83 | builder.AddMiddleware((invocation) => { |
82 | 84 | var host = invocation.GetHost(); |
| 85 | + var outputFilter = host.Services.GetRequiredService<IOutputFilter>(); |
83 | 86 | var outputFormatterFactory = host.Services.GetRequiredService<IOutputFormatterFactory>(); |
| 87 | + invocation.BindingContext.AddService<IOutputFilter>(_ => outputFilter); |
84 | 88 | invocation.BindingContext.AddService<IOutputFormatterFactory>(_ => outputFormatterFactory); |
85 | 89 | }); |
86 | 90 | builder.UseExceptionHandler((ex, context) => { |
87 | 91 | if (ex is AuthenticationRequiredException) { |
88 | 92 | Console.ResetColor(); |
89 | 93 | Console.ForegroundColor = ConsoleColor.Red; |
90 | | - Console.Error.WriteLine("Token acquisition failed. Run mgc login command first to get an access token."); |
| 94 | + context.Console.Error.WriteLine("Token acquisition failed. Run mgc login command first to get an access token."); |
| 95 | + Console.ResetColor(); |
| 96 | + } else { |
| 97 | + Console.ResetColor(); |
| 98 | + Console.ForegroundColor = ConsoleColor.Red; |
| 99 | + context.Console.Error.WriteLine(ex.Message); |
| 100 | + context.Console.Error.WriteLine(ex.StackTrace); |
91 | 101 | Console.ResetColor(); |
92 | 102 | } |
93 | 103 | }); |
@@ -118,6 +128,8 @@ static IHostBuilder CreateHostBuilder(string[] args) => |
118 | 128 | var authSection = ctx.Configuration.GetSection(Constants.AuthenticationSection); |
119 | 129 | services.Configure<AuthenticationOptions>(authSection); |
120 | 130 | services.AddSingleton<IPathUtility, PathUtility>(); |
| 131 | + services.AddSingleton<IOutputFilter, JmesPathOutputFilter>(); |
| 132 | + services.AddSingleton<JmesPath>(); |
121 | 133 | services.AddSingleton<IOutputFormatterFactory, OutputFormatterFactory>(); |
122 | 134 | }); |
123 | 135 |
|
|
0 commit comments