11using System ;
2- using System . Collections . Generic ;
32using System . CommandLine ;
43using System . CommandLine . Builder ;
54using System . CommandLine . Hosting ;
2423using Microsoft . Graph . Cli . Core . Authentication ;
2524using Microsoft . Graph . Cli . Core . Commands . Authentication ;
2625using Microsoft . Graph . Cli . Core . Configuration ;
27- using Microsoft . Graph . Cli . Core . Configuration . Extensions ;
2826using Microsoft . Graph . Cli . Core . Http ;
2927using Microsoft . Graph . Cli . Core . IO ;
3028using Microsoft . Kiota . Abstractions ;
3129using Microsoft . Kiota . Abstractions . Authentication ;
32- using Microsoft . Kiota . Abstractions . Serialization ;
3330using Microsoft . Kiota . Cli . Commons . Extensions ;
3431using Microsoft . Kiota . Http . HttpClientLibrary ;
3532using Microsoft . Kiota . Serialization . Form ;
@@ -40,17 +37,17 @@ namespace Microsoft.Graph.Cli
4037{
4138 class Program
4239 {
40+ private static bool debugEnabled = false ;
41+ private static AzureEventSourceListener ? listener = null ;
42+
4343 static async Task < int > Main ( string [ ] args )
4444 {
4545 Console . InputEncoding = Encoding . Unicode ;
4646 Console . OutputEncoding = Encoding . Unicode ;
4747 var builder = BuildCommandLine ( )
4848 . UseDefaults ( )
49- . UseHost ( a =>
50- {
51- // Pass all the args to avoid system.commandline swallowing them up
52- return CreateHostBuilder ( args ) ;
53- } ) . UseRequestAdapter ( ic =>
49+ . UseHost ( CreateHostBuilder )
50+ . UseRequestAdapter ( ic =>
5451 {
5552 var host = ic . GetHost ( ) ;
5653 var adapter = host . Services . GetRequiredService < IRequestAdapter > ( ) ;
@@ -59,16 +56,9 @@ static async Task<int> Main(string[] args)
5956 {
6057 adapter . BaseUrl = client . BaseAddress ? . ToString ( ) ;
6158 }
59+ adapter . BaseUrl = adapter . BaseUrl ? . TrimEnd ( '/' ) ;
6260 return adapter ;
6361 } ) . RegisterCommonServices ( ) ;
64-
65- builder . AddMiddleware ( async ( ic , next ) =>
66- {
67- var op = ic . GetHost ( ) . Services . GetService < IOptions < ExtraOptions > > ( ) ? . Value ;
68- // Show Azure Identity logs if the --debug option is set
69- using AzureEventSourceListener ? listener = op ? . DebugEnabled == true ? AzureEventSourceListener . CreateConsoleLogger ( EventLevel . LogAlways ) : null ;
70- await next ( ic ) ;
71- } ) ;
7262 builder . AddMiddleware ( async ( ic , next ) =>
7363 {
7464 var host = ic . GetHost ( ) ;
@@ -108,24 +98,35 @@ static async Task<int> Main(string[] args)
10898 context . ExitCode = exitCode ;
10999 } ) ;
110100
111- var parser = builder . Build ( ) ;
112-
113- return await parser . InvokeAsync ( args ) ;
101+ try
102+ {
103+ var parser = builder . Build ( ) ;
104+ return await parser . InvokeAsync ( args ) ;
105+ }
106+ finally
107+ {
108+ listener ? . Dispose ( ) ;
109+ }
114110 }
115111
116112 static CommandLineBuilder BuildCommandLine ( )
117113 {
118114 var rootCommand = new GraphClient ( ) . BuildRootCommand ( ) ;
119115 rootCommand . Description = "Microsoft Graph CLI" ;
120- // Support specifying additional arguments as configuration arguments
121- // System.CommandLine might swallow valid config tokens sometimes.
122- // e.g. if a command has an option --debug and we also want to use
123- // --debug for configs.
124- rootCommand . TreatUnmatchedTokensAsErrors = false ;
125116
126117 var builder = new CommandLineBuilder ( rootCommand ) ;
118+ var debugOption = new Option < bool > ( "--debug" , "Enable debug output" ) ;
119+
120+ builder . AddMiddleware ( async ( ic , next ) =>
121+ {
122+ debugEnabled = ic . ParseResult . GetValueForOption < bool > ( debugOption ) ;
123+ listener = AzureEventSourceListener . CreateConsoleLogger ( debugEnabled ? EventLevel . LogAlways : EventLevel . Warning ) ;
124+ await next ( ic ) ;
125+ } ) ;
126+
127127 rootCommand . AddCommand ( new LoginCommand ( builder ) ) ;
128128 rootCommand . AddCommand ( new LogoutCommand ( ) ) ;
129+ rootCommand . AddGlobalOption ( debugOption ) ;
129130
130131 return builder ;
131132 }
@@ -141,10 +142,6 @@ static IHostBuilder CreateHostBuilder(string[] args) =>
141142 {
142143 var authSection = ctx . Configuration . GetSection ( nameof ( AuthenticationOptions ) ) ;
143144 services . Configure < AuthenticationOptions > ( authSection ) ;
144- services . Configure < ExtraOptions > ( op =>
145- {
146- op . DebugEnabled = ctx . Configuration . GetValue < bool > ( "Debug" ) ;
147- } ) ;
148145 services . AddTransient < LoggingHandler > ( ) ;
149146 services . AddSingleton < HttpClient > ( p =>
150147 {
@@ -155,8 +152,7 @@ static IHostBuilder CreateHostBuilder(string[] args) =>
155152 GraphServiceLibraryClientVersion = $ "{ assemblyVersion ? . Major ?? 0 } .{ assemblyVersion ? . Minor ?? 0 } .{ assemblyVersion ? . Build ?? 0 } ",
156153 GraphServiceTargetVersion = "1.0"
157154 } ;
158- var loggingHandler = p . GetRequiredService < LoggingHandler > ( ) ;
159- return GraphCliClientFactory . GetDefaultClient ( options , lowestPriorityMiddlewares : new [ ] { loggingHandler } ) ;
155+ return GraphCliClientFactory . GetDefaultClient ( options , loggingHandler : p . GetRequiredService < LoggingHandler > ( ) ) ;
160156 } ) ;
161157 services . AddSingleton < IAuthenticationProvider > ( p =>
162158 {
@@ -197,13 +193,8 @@ static IHostBuilder CreateHostBuilder(string[] args) =>
197193 } ) . ConfigureLogging ( ( ctx , logBuilder ) =>
198194 {
199195 logBuilder . SetMinimumLevel ( LogLevel . Warning ) ;
200- // If a config is unavailable, troubleshoot using (ctx.Configuration as IConfigurationRoot)?.GetDebugView();
201- // At this point, the host isn't ready. Get the config option directly.
202- var debugEnabled = ctx . Configuration . GetValue < bool > ( "Debug" ) ;
203- if ( debugEnabled == true )
204- {
205- logBuilder . AddFilter ( "Microsoft.Graph.Cli" , LogLevel . Debug ) ;
206- }
196+ // Allow runtime selection of log level
197+ logBuilder . AddFilter ( "Microsoft.Graph.Cli" , level => level >= ( debugEnabled ? LogLevel . Debug : LogLevel . Warning ) ) ;
207198 } ) ;
208199
209200 static void ConfigureAppConfiguration ( IConfigurationBuilder builder , string [ ] args )
@@ -217,7 +208,6 @@ static void ConfigureAppConfiguration(IConfigurationBuilder builder, string[] ar
217208 builder . AddJsonFile ( userConfigPath , optional : true ) ;
218209 builder . AddJsonFile ( authCache . GetAuthenticationCacheFilePath ( ) , optional : true , reloadOnChange : true ) ;
219210 builder . AddEnvironmentVariables ( prefix : "MGC_" ) ;
220- builder . AddCommandLine ( args . ExpandFlagsForConfiguration ( ) ) ;
221211 }
222212 }
223213}
0 commit comments