Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 16d84b9

Browse files
authored
Merge pull request #35 from microsoftgraph/feat/add_output_filter
Add output filter resolution
2 parents 47940be + 5d873c3 commit 16d84b9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Program.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ApiSdk;
22
using Azure.Identity;
3+
using DevLab.JmesPath;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Hosting;
@@ -25,6 +26,7 @@
2526
using System.Threading.Tasks;
2627
using System.Collections.Generic;
2728
using System;
29+
using System.CommandLine.IO;
2830

2931
namespace Microsoft.Graph.Cli
3032
{
@@ -80,14 +82,22 @@ static async Task<int> Main(string[] args)
8082
var builder = BuildCommandLine(client, commands).UseDefaults().UseHost(CreateHostBuilder);
8183
builder.AddMiddleware((invocation) => {
8284
var host = invocation.GetHost();
85+
var outputFilter = host.Services.GetRequiredService<IOutputFilter>();
8386
var outputFormatterFactory = host.Services.GetRequiredService<IOutputFormatterFactory>();
87+
invocation.BindingContext.AddService<IOutputFilter>(_ => outputFilter);
8488
invocation.BindingContext.AddService<IOutputFormatterFactory>(_ => outputFormatterFactory);
8589
});
8690
builder.UseExceptionHandler((ex, context) => {
8791
if (ex is AuthenticationRequiredException) {
8892
Console.ResetColor();
8993
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);
91101
Console.ResetColor();
92102
}
93103
});
@@ -118,6 +128,8 @@ static IHostBuilder CreateHostBuilder(string[] args) =>
118128
var authSection = ctx.Configuration.GetSection(Constants.AuthenticationSection);
119129
services.Configure<AuthenticationOptions>(authSection);
120130
services.AddSingleton<IPathUtility, PathUtility>();
131+
services.AddSingleton<IOutputFilter, JmesPathOutputFilter>();
132+
services.AddSingleton<JmesPath>();
121133
services.AddSingleton<IOutputFormatterFactory, OutputFormatterFactory>();
122134
});
123135

0 commit comments

Comments
 (0)