-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (89 loc) · 4.03 KB
/
Program.cs
File metadata and controls
107 lines (89 loc) · 4.03 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
using System.CommandLine;
using QueryTerminal.CommandHandling;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
using QueryTerminal.Data;
using QueryTerminal.Prompting;
using Microsoft.Extensions.Configuration;
using QueryTerminal.OutputFormatting;
namespace QueryTerminal;
class Program
{
static async Task<int> Main(string[] args)
{
// configure service collection
var services = new ServiceCollection();
services.AddKeyedTransient<IQueryTerminalDbConnection, SqlQueryTerminalDbConnection>("mssql");
services.AddKeyedTransient<IQueryTerminalDbConnection, SqliteQueryTerminalDbConnection>("sqlite");
services.AddTransient<IQueryTerminalDbConnection>(serviceProvider =>
serviceProvider.GetRequiredKeyedService<IQueryTerminalDbConnection>(serviceProvider.GetRequiredService<IConfiguration>()["type"])
);
services.AddTransient<IDotCommands, DotCommands>();
services.AddTransient<QueryTerminalPrompt>();
services.AddTransient<QueryTerminalPromptCallbacks>();
services.AddSingleton<IOutputFormats,OutputFormats>();
services.AddTransient<IRenderer,Renderer>();
services.AddSingleton<RootCommandHandler>();
// Configure command and options
var rootCommand = new RootCommand("Connect and run commands against databases");
var databaseTypeOption = new Option<string>(
name: "--type",
description: "The type of database to connect to. Supported values are 'mssql' for Microsoft SQL Server and 'sqlite' for SQLite."
);
databaseTypeOption.AddAlias("-t");
rootCommand.AddOption(databaseTypeOption);
var connectionStringOption = new Option<string?>(
name: "--connectionString",
description: "The connection string used to connect to the database."
);
connectionStringOption.AddAlias("-c");
rootCommand.AddOption(connectionStringOption);
var sqlQueryOption = new Option<string?>(
name: "--query",
description: "The query to run. When using this option, the command is immediately run, output is sent to stdout, and the application terminates. Omitting this option launches REPL mode."
);
sqlQueryOption.AddAlias("-q");
rootCommand.AddOption(sqlQueryOption);
var outputFormatOption = new Option<string>(
name: "--outputFormat",
description: "The format to use to output data",
getDefaultValue: () => "csv"
);
outputFormatOption.AddAlias("-o");
rootCommand.AddOption(outputFormatOption);
rootCommand.SetHandler(async context =>
{
var dbType = context.ParseResult.GetValueForOption(databaseTypeOption);
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(
rootCommand.Options.Select(option => new KeyValuePair<string, string?>(option.Name.Replace("--", ""), context.ParseResult.GetValueForOption(option) as string))
);
services.AddSingleton<IConfiguration>(configurationBuilder.Build());
var serviceProvider = services.BuildServiceProvider();
var cancellationToken = context.GetCancellationToken();
var handler = serviceProvider.GetRequiredService<RootCommandHandler>();
await handler.RunAsync(cancellationToken);
});
// Execution and error handling
try
{
await rootCommand.InvokeAsync(args);
}
catch (OperationCanceledException)
{
Console.Error.WriteLine("Aborted");
return 1;
}
catch (ArgumentException e)
{
AnsiConsole.WriteException(e);
return 1;
}
catch (Exception e)
{
AnsiConsole.WriteException(e);
return 1;
}
return 0;
}
}