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

Commit 6506a09

Browse files
authored
Add debug as a CommandLine global option. (#300)
* Add debug as a CommandLine global option. * Update graph cli core
1 parent 20e6606 commit 6506a09

File tree

4 files changed

+142
-105
lines changed

4 files changed

+142
-105
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
### Changed
13+
14+
## [1.0.0-preview.1] - 2023-06-16
15+
16+
### Added
17+
18+
- Added overloaded OData functions as commands. Examples of newly added command
19+
paths:
20+
- `mgc drives items workbook worksheets charts image-with-width`
21+
- `mgc drives items workbook worksheets charts image-with-width-with-height`
22+
23+
### Changed
24+
25+
- `--file` has now been split into `--input-file` and `--output-file` to cater
26+
for endpoints that accept a stream and return a stream.
27+
- Fix error when accessing `mgc users direct-reports graph-org-contact`. There
28+
are now 2 similar commands under `mgc users direct-reports graph-org-contact`.
29+
- `mgc users direct-reports graph-org-contact`
30+
- `mgc users direct-reports graph-org-contact-by-id`
31+
1032
## [0.4.0-preview.1] - 2023-05-05
1133

1234
### Added

src/Program.cs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.CommandLine;
43
using System.CommandLine.Builder;
54
using System.CommandLine.Hosting;
@@ -24,12 +23,10 @@
2423
using Microsoft.Graph.Cli.Core.Authentication;
2524
using Microsoft.Graph.Cli.Core.Commands.Authentication;
2625
using Microsoft.Graph.Cli.Core.Configuration;
27-
using Microsoft.Graph.Cli.Core.Configuration.Extensions;
2826
using Microsoft.Graph.Cli.Core.Http;
2927
using Microsoft.Graph.Cli.Core.IO;
3028
using Microsoft.Kiota.Abstractions;
3129
using Microsoft.Kiota.Abstractions.Authentication;
32-
using Microsoft.Kiota.Abstractions.Serialization;
3330
using Microsoft.Kiota.Cli.Commons.Extensions;
3431
using Microsoft.Kiota.Http.HttpClientLibrary;
3532
using 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
}

src/msgraph-cli.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ImplicitUsings>disable</ImplicitUsings>
88
<Nullable>enable</Nullable>
99
<AssemblyName>mgc</AssemblyName>
10-
<Version>0.4.0-preview.1</Version>
10+
<Version>1.0.0-preview.1</Version>
1111
</PropertyGroup>
1212

1313
<PropertyGroup>
@@ -26,7 +26,7 @@
2626
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
2727
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
2828
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
29-
<PackageReference Include="Microsoft.Graph.Cli.Core" Version="0.2.1-preview.1" />
29+
<PackageReference Include="Microsoft.Graph.Cli.Core" Version="1.0.0-preview.1" />
3030
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
3131
</ItemGroup>
3232
</Project>

0 commit comments

Comments
 (0)