Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tools/LinkDotNet.Blog.UpgradeAssistant/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CommandLine;

namespace LinkDotNet.Blog.UpgradeAssistant;

public class CommandLineOptions
{
[Option('p', "path", Required = false, Default = ".",
HelpText = "Path to appsettings file or directory. Defaults to current directory")]
public string TargetPath { get; init; } = ".";

[Option('b', "backup-dir", Required = false, Default = "backups",
HelpText = "Custom backup directory path. Defaults to './backups'")]
public string BackupDirectory { get; init; } = "backups";

[Option('d', "dry-run", Required = false, Default = false,
HelpText = "Preview changes without applying them")]
public bool DryRun { get; init; }

[Option('h', "help", Required = false, Default = false,
HelpText = "Display help message")]
public bool Help { get; init; }

[Option('v', "version", Required = false, Default = false,
HelpText = "Display tool version")]
public bool Version { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" />
<PackageReference Include="Spectre.Console" />
</ItemGroup>

Expand Down
144 changes: 58 additions & 86 deletions tools/LinkDotNet.Blog.UpgradeAssistant/Program.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
using LinkDotNet.Blog.UpgradeAssistant;
using CommandLine;
using LinkDotNet.Blog.UpgradeAssistant;
using Spectre.Console;

var targetPath = ".";
var backupDirectory = "backups";
var dryRun = false;
var showHelp = false;
var showVersion = false;
return await Parser.Default.ParseArguments<CommandLineOptions>(args)
.MapResult(
async opts => await RunWithOptions(opts),
_ => Task.FromResult(1));

ParseCommandLineArguments(args, ref targetPath, ref backupDirectory, ref dryRun, ref showHelp, ref showVersion);

if (showHelp)
static async Task<int> RunWithOptions(CommandLineOptions options)
{
ShowHelp();
return 0;
}
if (options.Help)
{
ShowHelp();
return 0;
}

if (showVersion)
{
ShowVersion();
return 0;
}
if (options.Version)
{
ShowVersion();
return 0;
}

targetPath = Path.GetFullPath(targetPath);
backupDirectory = Path.GetFullPath(backupDirectory);
var targetPath = Path.GetFullPath(options.TargetPath);
var backupDirectory = Path.GetFullPath(options.BackupDirectory);

ConsoleOutput.WriteHeader("Blog Upgrade Assistant");
ConsoleOutput.WriteInfo($"Target: {targetPath}");
ConsoleOutput.WriteInfo($"Backup directory: {backupDirectory}");
ConsoleOutput.WriteHeader("Blog Upgrade Assistant");
ConsoleOutput.WriteInfo($"Target: {targetPath}");
ConsoleOutput.WriteInfo($"Backup directory: {backupDirectory}");

if (dryRun)
{
ConsoleOutput.WriteWarning("Running in DRY RUN mode - no changes will be saved.");
}
if (options.DryRun)
{
ConsoleOutput.WriteWarning("Running in DRY RUN mode - no changes will be saved.");
}

var manager = new MigrationManager();
var files = GetAppsettingsFiles(targetPath);
var manager = new MigrationManager();
var files = GetAppsettingsFiles(targetPath);

if (files.Count == 0)
{
ConsoleOutput.WriteError("No appsettings files found to migrate.");
ConsoleOutput.WriteInfo("Please specify a valid path using --path option.");
return 1;
}

ConsoleOutput.WriteInfo($"Found {files.Count} file(s) to process.");
AnsiConsole.WriteLine();
if (files.Count == 0)
{
ConsoleOutput.WriteError("No appsettings files found to migrate.");
ConsoleOutput.WriteInfo("Please specify a valid path using --path option.");
return 1;
}

var allSuccessful = true;
foreach (var file in files)
{
var success = await manager.MigrateFileAsync(file, dryRun, backupDirectory);
allSuccessful = allSuccessful && success;
ConsoleOutput.WriteInfo($"Found {files.Count} file(s) to process.");
AnsiConsole.WriteLine();
}

if (allSuccessful)
{
ConsoleOutput.WriteHeader("Migration Complete");
ConsoleOutput.WriteSuccess("All files processed successfully!");

if (!dryRun)
var allSuccessful = true;
foreach (var file in files)
{
ConsoleOutput.WriteInfo($"Backups saved to: {backupDirectory}");
var success = await manager.MigrateFileAsync(file, options.DryRun, backupDirectory);
allSuccessful = allSuccessful && success;
AnsiConsole.WriteLine();
}

ConsoleOutput.WriteInfo("Please review the changes and update any configuration values as needed.");
ConsoleOutput.WriteInfo("See MIGRATION.md for additional manual steps (database migrations, etc.).");
return 0;
}

ConsoleOutput.WriteError("Some files could not be processed. Please review the errors above.");
return 1;
if (allSuccessful)
{
ConsoleOutput.WriteHeader("Migration Complete");
ConsoleOutput.WriteSuccess("All files processed successfully!");

if (!options.DryRun)
{
ConsoleOutput.WriteInfo($"Backups saved to: {backupDirectory}");
}

ConsoleOutput.WriteInfo("Please review the changes and update any configuration values as needed.");
ConsoleOutput.WriteInfo("See MIGRATION.md for additional manual steps (database migrations, etc.).");
return 0;
}

ConsoleOutput.WriteError("Some files could not be processed. Please review the errors above.");
return 1;
}

static List<string> GetAppsettingsFiles(string path)
{
Expand Down Expand Up @@ -128,32 +129,3 @@ static void ShowVersion()
AnsiConsole.MarkupLine("[bold]Blog Upgrade Assistant[/]");
AnsiConsole.MarkupLine($"[dim]Target Blog Version: 12.0[/]");
}

static void ParseCommandLineArguments(string[] args, ref string targetPath, ref string backupDirectory, ref bool dryRun, ref bool showHelp, ref bool showVersion)
{
var i = 0;
while (i < args.Length)
{
switch (args[i])
{
case "-p" or "--path" when i + 1 < args.Length:
i++;
targetPath = args[i];
break;
case "-b" or "--backup-dir" when i + 1 < args.Length:
i++;
backupDirectory = args[i];
break;
case "-d" or "--dry-run":
dryRun = true;
break;
case "-h" or "--help":
showHelp = true;
break;
case "-v" or "--version":
showVersion = true;
break;
}
i++;
}
}