-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (97 loc) · 5.07 KB
/
Copy pathProgram.cs
File metadata and controls
109 lines (97 loc) · 5.07 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO.Abstractions;
using Azure.DataApiBuilder.Config;
using Cli.Commands;
using CommandLine;
using Microsoft.Extensions.Logging;
namespace Cli
{
/// <summary>
/// Main class for CLI
/// </summary>
public class Program
{
public const string PRODUCT_NAME = "Microsoft.DataApiBuilder";
/// <summary>
/// Main CLI entry point
/// </summary>
/// <param name="args">CLI arguments</param>
/// <returns>0 on success, -1 on failure.</returns>
public static int Main(string[] args)
{
// Load environment variables from .env file if present.
DotNetEnv.Env.Load();
// Parse MCP and LogLevel flags in a single pass for efficiency.
// These flags need to be known before logger creation.
ParseEarlyFlags(args);
// Logger setup and configuration
ILoggerFactory loggerFactory = Utils.LoggerFactoryForCli;
ILogger<Program> cliLogger = loggerFactory.CreateLogger<Program>();
ILogger<ConfigGenerator> configGeneratorLogger = loggerFactory.CreateLogger<ConfigGenerator>();
ILogger<Utils> cliUtilsLogger = loggerFactory.CreateLogger<Utils>();
ConfigGenerator.SetLoggerForCliConfigGenerator(configGeneratorLogger);
Utils.SetCliUtilsLogger(cliUtilsLogger);
// Sets up the filesystem used for reading and writing runtime configuration files.
IFileSystem fileSystem = new FileSystem();
FileSystemRuntimeConfigLoader loader = new(fileSystem, handler: null, isCliLoader: true);
return Execute(args, cliLogger, fileSystem, loader);
}
/// <summary>
/// Parses flags that need to be known before logger creation.
/// Scans args in a single pass for efficiency.
/// </summary>
/// <param name="args">Command line arguments</param>
private static void ParseEarlyFlags(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (string.Equals(arg, "--mcp-stdio", StringComparison.OrdinalIgnoreCase))
{
Utils.IsMcpStdioMode = true;
}
else if (string.Equals(arg, "--log-level", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
Utils.IsCliOverriding = true;
if (Enum.TryParse<LogLevel>(args[i + 1], ignoreCase: true, out LogLevel cliLogLevel))
{
Utils.CliLogLevel = cliLogLevel;
}
}
}
}
/// <summary>
/// Execute the CLI command
/// </summary>
/// <param name="args">Command line arguments</param>
/// <param name="cliLogger">Logger used as sink for informational and error messages.</param>
/// <param name="fileSystem">Filesystem used for reading and writing configuration files, and exporting GraphQL schemas.</param>
/// <param name="loader">Loads the runtime config.</param>
/// <returns>Exit Code: 0 success, -1 failure</returns>
public static int Execute(string[] args, ILogger cliLogger, IFileSystem fileSystem, FileSystemRuntimeConfigLoader loader)
{
Parser parser = new(settings =>
{
settings.CaseInsensitiveEnumValues = true;
settings.HelpWriter = Console.Out;
});
// Parsing user arguments and executing required methods.
int result = parser.ParseArguments<InitOptions, AddOptions, UpdateOptions, StartOptions, ValidateOptions, ExportOptions, AddTelemetryOptions, ConfigureOptions, AutoConfigOptions, AutoConfigSimulateOptions, AppNameOptions>(args)
.MapResult(
(InitOptions options) => options.Handler(cliLogger, loader, fileSystem),
(AddOptions options) => options.Handler(cliLogger, loader, fileSystem),
(UpdateOptions options) => options.Handler(cliLogger, loader, fileSystem),
(StartOptions options) => options.Handler(cliLogger, loader, fileSystem),
(ValidateOptions options) => options.Handler(cliLogger, loader, fileSystem),
(AddTelemetryOptions options) => options.Handler(cliLogger, loader, fileSystem),
(ConfigureOptions options) => options.Handler(cliLogger, loader, fileSystem),
(AutoConfigOptions options) => options.Handler(cliLogger, loader, fileSystem),
(AutoConfigSimulateOptions options) => options.Handler(cliLogger, loader, fileSystem),
(ExportOptions options) => options.Handler(cliLogger, loader, fileSystem),
(AppNameOptions options) => options.Handler(cliLogger, loader, fileSystem),
errors => DabCliParserErrorHandler.ProcessErrorsAndReturnExitCode(errors));
return result;
}
}
}