|
1 | | -using LinkDotNet.Blog.UpgradeAssistant; |
| 1 | +using CommandLine; |
| 2 | +using LinkDotNet.Blog.UpgradeAssistant; |
2 | 3 | using Spectre.Console; |
3 | 4 |
|
4 | | -var targetPath = "."; |
5 | | -var backupDirectory = "backups"; |
6 | | -var dryRun = false; |
7 | | -var showHelp = false; |
8 | | -var showVersion = false; |
| 5 | +return await Parser.Default.ParseArguments<CommandLineOptions>(args) |
| 6 | + .MapResult( |
| 7 | + async opts => await RunWithOptions(opts), |
| 8 | + _ => Task.FromResult(1)); |
9 | 9 |
|
10 | | -ParseCommandLineArguments(args, ref targetPath, ref backupDirectory, ref dryRun, ref showHelp, ref showVersion); |
11 | | - |
12 | | -if (showHelp) |
| 10 | +static async Task<int> RunWithOptions(CommandLineOptions options) |
13 | 11 | { |
14 | | - ShowHelp(); |
15 | | - return 0; |
16 | | -} |
| 12 | + if (options.Help) |
| 13 | + { |
| 14 | + ShowHelp(); |
| 15 | + return 0; |
| 16 | + } |
17 | 17 |
|
18 | | -if (showVersion) |
19 | | -{ |
20 | | - ShowVersion(); |
21 | | - return 0; |
22 | | -} |
| 18 | + if (options.Version) |
| 19 | + { |
| 20 | + ShowVersion(); |
| 21 | + return 0; |
| 22 | + } |
23 | 23 |
|
24 | | -targetPath = Path.GetFullPath(targetPath); |
25 | | -backupDirectory = Path.GetFullPath(backupDirectory); |
| 24 | + var targetPath = Path.GetFullPath(options.TargetPath); |
| 25 | + var backupDirectory = Path.GetFullPath(options.BackupDirectory); |
26 | 26 |
|
27 | | -ConsoleOutput.WriteHeader("Blog Upgrade Assistant"); |
28 | | -ConsoleOutput.WriteInfo($"Target: {targetPath}"); |
29 | | -ConsoleOutput.WriteInfo($"Backup directory: {backupDirectory}"); |
| 27 | + ConsoleOutput.WriteHeader("Blog Upgrade Assistant"); |
| 28 | + ConsoleOutput.WriteInfo($"Target: {targetPath}"); |
| 29 | + ConsoleOutput.WriteInfo($"Backup directory: {backupDirectory}"); |
30 | 30 |
|
31 | | -if (dryRun) |
32 | | -{ |
33 | | - ConsoleOutput.WriteWarning("Running in DRY RUN mode - no changes will be saved."); |
34 | | -} |
| 31 | + if (options.DryRun) |
| 32 | + { |
| 33 | + ConsoleOutput.WriteWarning("Running in DRY RUN mode - no changes will be saved."); |
| 34 | + } |
35 | 35 |
|
36 | | -var manager = new MigrationManager(); |
37 | | -var files = GetAppsettingsFiles(targetPath); |
| 36 | + var manager = new MigrationManager(); |
| 37 | + var files = GetAppsettingsFiles(targetPath); |
38 | 38 |
|
39 | | -if (files.Count == 0) |
40 | | -{ |
41 | | - ConsoleOutput.WriteError("No appsettings files found to migrate."); |
42 | | - ConsoleOutput.WriteInfo("Please specify a valid path using --path option."); |
43 | | - return 1; |
44 | | -} |
45 | | - |
46 | | -ConsoleOutput.WriteInfo($"Found {files.Count} file(s) to process."); |
47 | | -AnsiConsole.WriteLine(); |
| 39 | + if (files.Count == 0) |
| 40 | + { |
| 41 | + ConsoleOutput.WriteError("No appsettings files found to migrate."); |
| 42 | + ConsoleOutput.WriteInfo("Please specify a valid path using --path option."); |
| 43 | + return 1; |
| 44 | + } |
48 | 45 |
|
49 | | -var allSuccessful = true; |
50 | | -foreach (var file in files) |
51 | | -{ |
52 | | - var success = await manager.MigrateFileAsync(file, dryRun, backupDirectory); |
53 | | - allSuccessful = allSuccessful && success; |
| 46 | + ConsoleOutput.WriteInfo($"Found {files.Count} file(s) to process."); |
54 | 47 | AnsiConsole.WriteLine(); |
55 | | -} |
56 | 48 |
|
57 | | -if (allSuccessful) |
58 | | -{ |
59 | | - ConsoleOutput.WriteHeader("Migration Complete"); |
60 | | - ConsoleOutput.WriteSuccess("All files processed successfully!"); |
61 | | - |
62 | | - if (!dryRun) |
| 49 | + var allSuccessful = true; |
| 50 | + foreach (var file in files) |
63 | 51 | { |
64 | | - ConsoleOutput.WriteInfo($"Backups saved to: {backupDirectory}"); |
| 52 | + var success = await manager.MigrateFileAsync(file, options.DryRun, backupDirectory); |
| 53 | + allSuccessful = allSuccessful && success; |
| 54 | + AnsiConsole.WriteLine(); |
65 | 55 | } |
66 | | - |
67 | | - ConsoleOutput.WriteInfo("Please review the changes and update any configuration values as needed."); |
68 | | - ConsoleOutput.WriteInfo("See MIGRATION.md for additional manual steps (database migrations, etc.)."); |
69 | | - return 0; |
70 | | -} |
71 | 56 |
|
72 | | -ConsoleOutput.WriteError("Some files could not be processed. Please review the errors above."); |
73 | | -return 1; |
| 57 | + if (allSuccessful) |
| 58 | + { |
| 59 | + ConsoleOutput.WriteHeader("Migration Complete"); |
| 60 | + ConsoleOutput.WriteSuccess("All files processed successfully!"); |
| 61 | + |
| 62 | + if (!options.DryRun) |
| 63 | + { |
| 64 | + ConsoleOutput.WriteInfo($"Backups saved to: {backupDirectory}"); |
| 65 | + } |
| 66 | + |
| 67 | + ConsoleOutput.WriteInfo("Please review the changes and update any configuration values as needed."); |
| 68 | + ConsoleOutput.WriteInfo("See MIGRATION.md for additional manual steps (database migrations, etc.)."); |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + ConsoleOutput.WriteError("Some files could not be processed. Please review the errors above."); |
| 73 | + return 1; |
| 74 | +} |
74 | 75 |
|
75 | 76 | static List<string> GetAppsettingsFiles(string path) |
76 | 77 | { |
@@ -128,32 +129,3 @@ static void ShowVersion() |
128 | 129 | AnsiConsole.MarkupLine("[bold]Blog Upgrade Assistant[/]"); |
129 | 130 | AnsiConsole.MarkupLine($"[dim]Target Blog Version: 12.0[/]"); |
130 | 131 | } |
131 | | - |
132 | | -static void ParseCommandLineArguments(string[] args, ref string targetPath, ref string backupDirectory, ref bool dryRun, ref bool showHelp, ref bool showVersion) |
133 | | -{ |
134 | | - var i = 0; |
135 | | - while (i < args.Length) |
136 | | - { |
137 | | - switch (args[i]) |
138 | | - { |
139 | | - case "-p" or "--path" when i + 1 < args.Length: |
140 | | - i++; |
141 | | - targetPath = args[i]; |
142 | | - break; |
143 | | - case "-b" or "--backup-dir" when i + 1 < args.Length: |
144 | | - i++; |
145 | | - backupDirectory = args[i]; |
146 | | - break; |
147 | | - case "-d" or "--dry-run": |
148 | | - dryRun = true; |
149 | | - break; |
150 | | - case "-h" or "--help": |
151 | | - showHelp = true; |
152 | | - break; |
153 | | - case "-v" or "--version": |
154 | | - showVersion = true; |
155 | | - break; |
156 | | - } |
157 | | - i++; |
158 | | - } |
159 | | -} |
0 commit comments