-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
423 lines (375 loc) · 19.7 KB
/
Program.cs
File metadata and controls
423 lines (375 loc) · 19.7 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SQLScripter.Models;
using SQLScripter.Services;
using SQLScripter.Security;
using System.Runtime.Versioning;
namespace SQLScripter
{
[SupportedOSPlatform("windows")]
class Program
{
public static int SqlServerMaxNameLength;
public static int SqlDatabaseMaxNameLength = 30;
private static IServiceProvider _serviceProvider = null!;
static async Task Main(string[] args)
{
// Register encoding provider for .NET Core/.NET 8 compatibility
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
// Initialize log4net
var logRepository = log4net.LogManager.GetRepository(Assembly.GetEntryAssembly()!);
log4net.Config.XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
// Check for credential management commands
if (args.Length > 0)
{
string cmd = args[0].TrimStart('-').ToLower();
if (cmd == "add" || cmd == "remove" || cmd == "list")
{
HandleCredentialCommand(args);
return;
}
if (cmd == "h" || cmd == "help" || cmd == "?")
{
ShowHelp();
return;
}
}
try
{
// Parse command line arguments
var options = ParseArguments(args);
if (options.Errors.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
foreach (var err in options.Errors) Console.WriteLine($"Error: {err}");
Console.ResetColor();
Console.WriteLine("Use --help for usage information.");
return;
}
// Setup services
ConfigureServices();
var logger = _serviceProvider.GetRequiredService<ILoggerService>();
var configuration = _serviceProvider.GetRequiredService<IConfiguration>();
var sqlScripterSection = configuration.GetSection("SQLScripter");
var appSettings = sqlScripterSection.Get<AppSettings>() ?? new AppSettings();
// Apply global overrides from CLI
if (!string.IsNullOrEmpty(options.OutputFolder)) appSettings.OutputFolder = options.OutputFolder;
if (options.Threads.HasValue) appSettings.MaxConcurrentThreads = options.Threads.Value;
if (options.Zip.HasValue) appSettings.ZipFolder = options.Zip.Value;
string applicationName = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyProductAttribute>()?.Product ?? "SQLScripter";
string version = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? Assembly.GetExecutingAssembly().GetName().Version?.ToString()
?? "4.3.0.0";
Console.Title = $"{applicationName} v{version}";
// Print header with effective configuration
PrintHeader(applicationName, version, appSettings, logger);
logger.LogEvent($"{applicationName} v{version} started", EventLogEntryType.Information);
// Load servers from appsettings.json
var serversList = configuration.GetSection("Servers").Get<List<ServerSettings>>() ?? new List<ServerSettings>();
// Logic for server filtering or addition
if (!string.IsNullOrEmpty(options.Server))
{
var existing = serversList.FirstOrDefault(s => s.SQLServer.Equals(options.Server, StringComparison.OrdinalIgnoreCase));
if (existing != null)
{
serversList = new List<ServerSettings> { existing };
logger.Info("", "", $"Targeting specific server from config: {options.Server}");
}
else
{
var newServer = new ServerSettings { SQLServer = options.Server };
serversList = new List<ServerSettings> { newServer };
logger.Info("", "", $"Targeting ad-hoc server: {options.Server}");
}
}
// Apply database and object type overrides
foreach (var server in serversList)
{
if (!string.IsNullOrEmpty(options.Database))
{
server.Databases = options.Database;
}
if (!string.IsNullOrEmpty(options.ObjectTypes))
{
server.ObjectTypes = options.ObjectTypes;
logger.Info(server.SQLServer, "", $"Overriding object types: {server.ObjectTypes}");
}
}
if (serversList.Count == 0)
{
logger.Error("", "", "No servers to process. Check appsettings.json or command line arguments.", new Exception("Server list is empty"));
return;
}
logger.Info("", "", $"Processing {serversList.Count} server(s)...");
// Start processing
var orchestrator = _serviceProvider.GetRequiredService<IOrchestrationService>();
await orchestrator.ProcessServersAsync(serversList, appSettings, applicationName);
logger.Info("", "", "Application finished successfully.");
logger.LogEvent($"{applicationName} finished successfully", EventLogEntryType.Information);
}
catch (Exception ex)
{
var logger = _serviceProvider?.GetService<ILoggerService>();
if (logger != null)
{
logger.WriteToLog("", "", "Error", ex);
logger.LogEvent($"SQLScripter fatal error: {ex.Message}", EventLogEntryType.Error);
}
else
{
Console.WriteLine($"Fatal Error: {ex.Message}");
}
Environment.Exit(1);
}
}
private static void ConfigureServices()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var services = new ServiceCollection();
// Add configuration
services.AddSingleton<IConfiguration>(configuration);
// Add services
services.AddSingleton<ILoggerService>(sp => {
var config = sp.GetRequiredService<IConfiguration>();
var appSettings = config.GetSection("SQLScripter").Get<AppSettings>() ?? new AppSettings();
return new LoggerService(appSettings.WriteToConsole, appSettings.ConsoleForeGroundColour);
});
services.AddSingleton<IConnectionService, ConnectionService>();
services.AddSingleton<IFileManagementService, FileManagementService>();
// ScriptingService needs the boolean from config
services.AddSingleton<IScriptingService>(sp => {
var config = sp.GetRequiredService<IConfiguration>();
var appSettings = config.GetSection("SQLScripter").Get<AppSettings>() ?? new AppSettings();
return new ScriptingService(sp.GetRequiredService<ILoggerService>(), appSettings.ScriptOneFilePerObjectType);
});
services.AddSingleton<IOrchestrationService, OrchestrationService>();
_serviceProvider = services.BuildServiceProvider();
}
private static void PrintHeader(string appName, string version, AppSettings settings, ILoggerService logger)
{
// Print visual header to console only
if (settings.WriteToConsole)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("===================================================================================");
Console.WriteLine($" {appName} v{version}");
Console.WriteLine("===================================================================================");
Console.ResetColor();
Console.WriteLine();
}
// Print and log configuration
logger.Info("", "", "Configuration:");
logger.Info("", "", $"{"Output Folder:",-30} {settings.OutputFolder}");
logger.Info("", "", $"{"Script One File Per Type:",-30} {settings.ScriptOneFilePerObjectType}");
logger.Info("", "", $"{"Max Concurrent Threads:",-30} {settings.MaxConcurrentThreads}");
logger.Info("", "", $"{"ZIP Output:",-30} {settings.ZipFolder}");
if (settings.ZipFolder)
{
logger.Info("", "", $"{"ZIP Password Protected:",-30} {!string.IsNullOrEmpty(settings.ZipPassword)}");
logger.Info("", "", $"{"Delete Folder After ZIP:",-30} {settings.DeleteOutputFolderAfterZip}");
}
logger.Info("", "", $"{"Days to Keep Files:",-30} {settings.DaysToKeepFilesInOutputFolder}");
logger.Info("", "", "");
}
private static void HandleCredentialCommand(string[] args)
{
var storage = new CredentialsStorage();
string command = args[0].TrimStart('-').ToLower();
try
{
switch (command)
{
case "add":
if (args.Length < 4 || args.Length > 5)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: Invalid arguments for 'add' command.");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("Usage: SQLScripter.exe add <server> <username> <password> [sql|win]");
Console.WriteLine("Example (SQL): SQLScripter.exe add SQLSERVER01 sa MyPassword");
Console.WriteLine("Example (Windows): SQLScripter.exe add PROD-SQL01 DOMAIN\\user MyPassword win");
return;
}
AuthenticationType authType = AuthenticationType.Sql;
if (args.Length == 5)
{
string type = args[4].ToLower();
if (type == "win" || type == "windows")
authType = AuthenticationType.Windows;
else if (type != "sql")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: Invalid authentication type '{type}'. Use 'sql' or 'win'.");
Console.ResetColor();
return;
}
}
storage.AddOrUpdateCredential(args[1], args[2], args[3], authType);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"✓ Credentials ({authType}) for server '{args[1]}' have been encrypted and saved successfully.");
Console.ResetColor();
break;
case "remove":
if (args.Length != 2)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: Invalid arguments for 'remove' command.");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("Usage: SQLScripter.exe remove <server>");
Console.WriteLine("Example: SQLScripter.exe remove SQLSERVER01");
return;
}
if (storage.RemoveCredential(args[1]))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"✓ Credentials for server '{args[1]}' have been removed successfully.");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"⚠ No credentials found for server '{args[1]}'.");
Console.ResetColor();
}
break;
case "list":
var credentials = storage.LoadCredentials();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Stored Credentials:");
Console.WriteLine("===================");
Console.ResetColor();
Console.WriteLine();
if (credentials.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("No credentials stored.");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"{"Server",-35} {"Username",-20} {"Type",-10}");
Console.WriteLine(new string('-', 70));
Console.ResetColor();
foreach (var cred in credentials)
{
Console.WriteLine($"{cred.Server,-35} {cred.Username,-20} {cred.AuthType,-10}");
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Total: {credentials.Count} credential(s)");
Console.ResetColor();
}
Console.WriteLine();
break;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: {ex.Message}");
Console.ResetColor();
}
}
private static void ShowHelp()
{
string appName = Assembly.GetExecutingAssembly().GetName().Name ?? "SQLScripter";
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"{appName} - SQL Server Scripting Utility");
Console.ResetColor();
Console.WriteLine("Usage:");
Console.WriteLine($" {appName}.exe [options]");
Console.WriteLine($" {appName}.exe <command> [args]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" -s, --server <name> Target a specific SQL Server");
Console.WriteLine(" -d, --database <name> Target a specific database (can be a list: db1;db2)");
Console.WriteLine(" -t, --types <list> Object types to script (e.g. Tables;Views;Procedures;Functions;Triggers;Indexes)");
Console.WriteLine(" -o, --output <path> Override output folder path");
Console.WriteLine(" -n, --threads <num> Max concurrent threads (1-100)");
Console.WriteLine(" -z, --zip <true|false> Enable or disable ZIP output");
Console.WriteLine(" -h, --help Show this help information");
Console.WriteLine();
Console.WriteLine("Legacy Usage (Object Types only):");
Console.WriteLine($" {appName}.exe Tables Views");
Console.WriteLine();
Console.WriteLine("Credential Commands:");
Console.WriteLine($" {appName}.exe add <server> <user> <pass> [sql|win]");
Console.WriteLine($" {appName}.exe remove <server>");
Console.WriteLine($" {appName}.exe list");
Console.WriteLine();
}
private static CommandLineOptions ParseArguments(string[] args)
{
var options = new CommandLineOptions();
if (args.Length == 0) return options;
// If the first argument doesn't start with a dash, assume legacy mode (all args are object types)
if (!args[0].StartsWith("-") && !args[0].StartsWith("/"))
{
options.ObjectTypes = string.Join(";", args);
return options;
}
for (int i = 0; i < args.Length; i++)
{
string arg = args[i].ToLower();
string? value = (i + 1 < args.Length) ? args[i + 1] : null;
switch (arg)
{
case "-s": case "--server":
if (value == null) options.Errors.Add("Server name missing");
else { options.Server = value; i++; }
break;
case "-d": case "--database":
if (value == null) options.Errors.Add("Database name missing");
else { options.Database = value; i++; }
break;
case "-t": case "--types":
if (value == null) options.Errors.Add("Object types missing");
else { options.ObjectTypes = value; i++; }
break;
case "-o": case "--output":
if (value == null) options.Errors.Add("Output path missing");
else { options.OutputFolder = value; i++; }
break;
case "-n": case "--threads":
if (int.TryParse(value, out int t)) { options.Threads = t; i++; }
else options.Errors.Add("Invalid thread count");
break;
case "-z": case "--zip":
if (bool.TryParse(value, out bool z)) { options.Zip = z; i++; }
else options.Errors.Add("Invalid zip value (use true/false)");
break;
case "-h": case "--help": case "/?":
options.ShowHelp = true;
break;
}
}
return options;
}
}
public class CommandLineOptions
{
public string? Server { get; set; }
public string? Database { get; set; }
public string? ObjectTypes { get; set; }
public string? OutputFolder { get; set; }
public int? Threads { get; set; }
public bool? Zip { get; set; }
public bool ShowHelp { get; set; }
public List<string> Errors { get; } = new();
}
}