Skip to content

Commit 4a2f390

Browse files
authored
Refactor UpgradeAssistant to use CommandLineParser (#471)
1 parent ee80c7c commit 4a2f390

File tree

3 files changed

+85
-86
lines changed

3 files changed

+85
-86
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CommandLine;
2+
3+
namespace LinkDotNet.Blog.UpgradeAssistant;
4+
5+
public class CommandLineOptions
6+
{
7+
[Option('p', "path", Required = false, Default = ".",
8+
HelpText = "Path to appsettings file or directory. Defaults to current directory")]
9+
public string TargetPath { get; init; } = ".";
10+
11+
[Option('b', "backup-dir", Required = false, Default = "backups",
12+
HelpText = "Custom backup directory path. Defaults to './backups'")]
13+
public string BackupDirectory { get; init; } = "backups";
14+
15+
[Option('d', "dry-run", Required = false, Default = false,
16+
HelpText = "Preview changes without applying them")]
17+
public bool DryRun { get; init; }
18+
19+
[Option('h', "help", Required = false, Default = false,
20+
HelpText = "Display help message")]
21+
public bool Help { get; init; }
22+
23+
[Option('v', "version", Required = false, Default = false,
24+
HelpText = "Display tool version")]
25+
public bool Version { get; init; }
26+
}

tools/LinkDotNet.Blog.UpgradeAssistant/LinkDotNet.Blog.UpgradeAssistant.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="CommandLineParser" />
910
<PackageReference Include="Spectre.Console" />
1011
</ItemGroup>
1112

Lines changed: 58 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
1-
using LinkDotNet.Blog.UpgradeAssistant;
1+
using CommandLine;
2+
using LinkDotNet.Blog.UpgradeAssistant;
23
using Spectre.Console;
34

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));
99

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)
1311
{
14-
ShowHelp();
15-
return 0;
16-
}
12+
if (options.Help)
13+
{
14+
ShowHelp();
15+
return 0;
16+
}
1717

18-
if (showVersion)
19-
{
20-
ShowVersion();
21-
return 0;
22-
}
18+
if (options.Version)
19+
{
20+
ShowVersion();
21+
return 0;
22+
}
2323

24-
targetPath = Path.GetFullPath(targetPath);
25-
backupDirectory = Path.GetFullPath(backupDirectory);
24+
var targetPath = Path.GetFullPath(options.TargetPath);
25+
var backupDirectory = Path.GetFullPath(options.BackupDirectory);
2626

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}");
3030

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+
}
3535

36-
var manager = new MigrationManager();
37-
var files = GetAppsettingsFiles(targetPath);
36+
var manager = new MigrationManager();
37+
var files = GetAppsettingsFiles(targetPath);
3838

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+
}
4845

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.");
5447
AnsiConsole.WriteLine();
55-
}
5648

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)
6351
{
64-
ConsoleOutput.WriteInfo($"Backups saved to: {backupDirectory}");
52+
var success = await manager.MigrateFileAsync(file, options.DryRun, backupDirectory);
53+
allSuccessful = allSuccessful && success;
54+
AnsiConsole.WriteLine();
6555
}
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-
}
7156

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+
}
7475

7576
static List<string> GetAppsettingsFiles(string path)
7677
{
@@ -128,32 +129,3 @@ static void ShowVersion()
128129
AnsiConsole.MarkupLine("[bold]Blog Upgrade Assistant[/]");
129130
AnsiConsole.MarkupLine($"[dim]Target Blog Version: 12.0[/]");
130131
}
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

Comments
 (0)