-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (105 loc) · 5.22 KB
/
Program.cs
File metadata and controls
117 lines (105 loc) · 5.22 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
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Help;
using System.CommandLine.Hosting;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Cosmos.DataTransfer.Core;
class Program
{
public static async Task<int> Main(string[] args)
{
var rootCommand = new RootCommand("Azure data migration tool") { TreatUnmatchedTokensAsErrors = false };
rootCommand.AddCommand(new RunCommand());
rootCommand.AddCommand(new ListCommand());
rootCommand.AddCommand(new InitCommand());
rootCommand.AddCommand(new SettingsCommand());
// execute Run if no command provided
RunCommand.AddRunOptions(rootCommand);
rootCommand.SetHandler(async ctx =>
{
var host = ctx.GetHost();
var logger = host.Services.GetService<ILoggerFactory>();
var config = host.Services.GetService<IConfiguration>();
var loader = host.Services.GetService<IExtensionLoader>();
if (loader == null || config == null || logger == null)
{
ctx.Console.Error.WriteLine("Missing required command");
}
else
{
var handler = new RunCommand.CommandHandler(loader, config, logger)
{
Source = ctx.BindingContext.ParseResult.GetValueForOption(rootCommand.Options.ElementAt(0)) as string,
Sink = ctx.BindingContext.ParseResult.GetValueForOption(rootCommand.Options.ElementAt(1)) as string,
Settings = ctx.BindingContext.ParseResult.GetValueForOption(rootCommand.Options.ElementAt(2)) as FileInfo
};
ctx.ExitCode = await handler.InvokeAsync(ctx);
}
});
var cmdlineBuilder = new CommandLineBuilder(rootCommand);
var parser = cmdlineBuilder.UseHost(_ => Host.CreateDefaultBuilder(args),
builder =>
{
builder.ConfigureAppConfiguration((hostContext, cfg) =>
{
// Keep explicit .NET precedence order:
// appsettings.json -> appsettings.{Environment}.json (for example appsettings.Development.json)
// -> user secrets (only when Environment == Development)
// -> environment variables -> command line.
// This ensures env vars and CLI args override file-based defaults.
cfg.Sources.Clear();
var exeFolder = AppContext.BaseDirectory;
cfg.SetBasePath(exeFolder);
cfg.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
cfg.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: false);
if (hostContext.HostingEnvironment.IsDevelopment())
{
cfg.AddUserSecrets<Program>(optional: true);
}
cfg.AddEnvironmentVariables();
cfg.AddCommandLine(args);
}).ConfigureServices((hostContext, services) =>
{
services.AddTransient<IExtensionLoader, ExtensionLoader>();
services.AddTransient<IRawOutputWriter, ConsoleOutputWriter>();
services.AddTransient<IExtensionManifestBuilder, ExtensionManifestBuilder>();
})
.UseCommandHandler<RunCommand, RunCommand.CommandHandler>()
.UseCommandHandler<ListCommand, ListCommand.CommandHandler>()
.UseCommandHandler<InitCommand, InitCommand.CommandHandler>()
.UseCommandHandler<SettingsCommand, SettingsCommand.CommandHandler>();
})
.UseHelp(AddAdditionalArgumentsHelp)
.UseDefaults().Build();
return await parser.InvokeAsync(args);
}
private static void AddAdditionalArgumentsHelp(HelpContext helpContext)
{
helpContext.HelpBuilder.CustomizeLayout(_ =>
{
var layout = HelpBuilder.Default.GetLayout().ToList();
layout.Remove(HelpBuilder.Default.AdditionalArgumentsSection());
bool runCommand = helpContext.Command.GetType() == typeof(RunCommand);
bool rootCommand = helpContext.Command.GetType() == typeof(RootCommand);
if (runCommand || rootCommand)
{
layout.Add(ctx =>
{
if (rootCommand)
{
ctx.Output.WriteLine();
}
ctx.Output.WriteLine("Additional Arguments:");
ctx.Output.WriteLine(" Extension specific settings can be provided as additional arguments in the form:");
ctx.HelpBuilder.WriteColumns(new List<TwoColumnHelpRow> { new("--<Source|Sink>Settings:<name> <value>", "ex: --SourceSettings:FilePath MyDataFile.json") }.AsReadOnly(), ctx);
});
}
return layout;
});
}
}