forked from microsoftgraph/msgraph-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (128 loc) · 6.41 KB
/
Copy pathProgram.cs
File metadata and controls
142 lines (128 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using ApiSdk;
using Azure.Identity;
using DevLab.JmesPath;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Graph.Cli.Core.Authentication;
using Microsoft.Graph.Cli.Core.Commands.Authentication;
using Microsoft.Graph.Cli.Core.Configuration;
using Microsoft.Graph.Cli.Core.IO;
using Microsoft.Kiota.Authentication.Azure;
using Microsoft.Kiota.Cli.Commons.IO;
using Microsoft.Kiota.Http.HttpClientLibrary;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Parsing;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using System.CommandLine.IO;
namespace Microsoft.Graph.Cli
{
class Program
{
static async Task<int> Main(string[] args)
{
// We don't have access to a built host yet. Get configuration settings using a configuration builder.
// Required to set initial token credentials.
var configBuilder = new ConfigurationBuilder();
ConfigureAppConfiguration(configBuilder);
var config = configBuilder.Build();
var authSettings = config.GetSection(nameof(AuthenticationOptions)).Get<AuthenticationOptions>();
var authServiceFactory = new AuthenticationServiceFactory(new PathUtility());
var authStrategy = AuthenticationStrategy.DeviceCode;
var credential = await authServiceFactory.GetTokenCredentialAsync(authStrategy, authSettings?.TenantId, authSettings?.ClientId);
var authProvider = new AzureIdentityAuthenticationProvider(credential, new string[] { "graph.microsoft.com" });
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
var options = new GraphClientOptions
{
GraphProductPrefix = "graph-cli",
GraphServiceLibraryClientVersion = $"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}",
GraphServiceTargetVersion = "1.0"
};
using var httpClient = GraphCliClientFactory.GetDefaultClient(options);
var core = new HttpClientRequestAdapter(authProvider, httpClient: httpClient);
var client = new GraphClient(core);
var commands = new List<Command>();
var loginCommand = new LoginCommand(authServiceFactory);
commands.Add(loginCommand.Build());
var logoutCommand = new LogoutCommand(new LogoutService());
commands.Add(logoutCommand.Build());
var builder = BuildCommandLine(client, commands).UseDefaults().UseHost(CreateHostBuilder);
builder.AddMiddleware((invocation) =>
{
var host = invocation.GetHost();
var outputFilter = host.Services.GetRequiredService<IOutputFilter>();
var outputFormatterFactory = host.Services.GetRequiredService<IOutputFormatterFactory>();
var pagingService = host.Services.GetRequiredService<IPagingService>();
invocation.BindingContext.AddService(_ => outputFilter);
invocation.BindingContext.AddService(_ => outputFormatterFactory);
invocation.BindingContext.AddService(_ => pagingService);
});
builder.UseExceptionHandler((ex, context) =>
{
if (ex is AuthenticationRequiredException)
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
context.Console.Error.WriteLine("Token acquisition failed. Run mgc login command first to get an access token.");
Console.ResetColor();
}
else
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
context.Console.Error.WriteLine(ex.Message);
context.Console.Error.WriteLine(ex.StackTrace);
Console.ResetColor();
}
});
var parser = builder.Build();
return await parser.InvokeAsync(args);
}
static CommandLineBuilder BuildCommandLine(GraphClient client, IEnumerable<Command> commands)
{
var rootCommand = client.BuildRootCommand();
rootCommand.Description = "Microsoft Graph CLI";
foreach (var command in commands)
{
rootCommand.AddCommand(command);
}
return new CommandLineBuilder(rootCommand);
}
static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder().ConfigureHostConfiguration((configHost) =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
}).ConfigureAppConfiguration((ctx, config) =>
{
ConfigureAppConfiguration(config);
}).ConfigureServices((ctx, services) =>
{
var authSection = ctx.Configuration.GetSection(nameof(AuthenticationOptions));
services.Configure<AuthenticationOptions>(authSection);
services.AddSingleton<IPathUtility, PathUtility>();
services.AddSingleton<IAuthenticationCacheUtility, AuthenticationCacheUtility>();
services.AddSingleton<IOutputFilter, JmesPathOutputFilter>();
services.AddSingleton<JmesPath>();
services.AddSingleton<IOutputFormatterFactory, OutputFormatterFactory>();
services.AddSingleton<IPagingService, GraphODataPagingService>();
});
static void ConfigureAppConfiguration(IConfigurationBuilder builder)
{
builder.Sources.Clear();
builder.AddJsonFile(Path.Combine(System.AppContext.BaseDirectory, "app-settings.json"), optional: true);
var pathUtil = new PathUtility();
var authCache = new AuthenticationCacheUtility(pathUtil);
var dataDir = pathUtil.GetApplicationDataDirectory();
var userConfigPath = Path.Combine(dataDir, "settings.json");
builder.AddJsonFile(userConfigPath, optional: true);
builder.AddJsonFile(authCache.GetAuthenticationCacheFilePath(), optional: true, reloadOnChange: true);
builder.AddEnvironmentVariables(prefix: "MGC_");
}
}
}