-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (108 loc) · 4.47 KB
/
Copy pathProgram.cs
File metadata and controls
131 lines (108 loc) · 4.47 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
using CommandLine;
using LinkDotNet.Blog.UpgradeAssistant;
using Spectre.Console;
return await Parser.Default.ParseArguments<CommandLineOptions>(args)
.MapResult(
async opts => await RunWithOptions(opts),
_ => Task.FromResult(1));
static async Task<int> RunWithOptions(CommandLineOptions options)
{
if (options.Help)
{
ShowHelp();
return 0;
}
if (options.Version)
{
ShowVersion();
return 0;
}
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}");
if (options.DryRun)
{
ConsoleOutput.WriteWarning("Running in DRY RUN mode - no changes will be saved.");
}
var manager = new MigrationManager(MigrationDiscovery.DiscoverAll());
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();
var allSuccessful = true;
foreach (var file in files)
{
var success = await manager.MigrateFileAsync(file, options.DryRun, backupDirectory);
allSuccessful = allSuccessful && success;
AnsiConsole.WriteLine();
}
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)
{
if (File.Exists(path) && path.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
return new List<string> { path };
}
if (Directory.Exists(path))
{
return Directory.GetFiles(path, "appsettings*.json", SearchOption.TopDirectoryOnly)
.Where(f => !Path.GetFileName(f).Equals("appsettings.json", StringComparison.OrdinalIgnoreCase))
.OrderBy(f => f)
.ToList();
}
return new List<string>();
}
static void ShowHelp()
{
AnsiConsole.Write(new FigletText("Blog Upgrade Assistant").Color(Color.Magenta1));
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[bold]Automatically migrates appsettings configuration files to the latest version.[/]");
AnsiConsole.WriteLine();
var table = new Table()
.Border(TableBorder.Rounded)
.AddColumn(new TableColumn("[bold cyan]Option[/]"))
.AddColumn(new TableColumn("[bold cyan]Description[/]"));
table.AddRow("[yellow]-p, --path <path>[/]", "Path to appsettings file or directory\nDefaults to current directory");
table.AddRow("[yellow]-d, --dry-run[/]", "Preview changes without applying them");
table.AddRow("[yellow]-b, --backup-dir <path>[/]", "Custom backup directory path\nDefaults to './backups'");
table.AddRow("[yellow]-h, --help[/]", "Display this help message");
table.AddRow("[yellow]-v, --version[/]", "Display tool version");
AnsiConsole.Write(table);
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[bold]Examples:[/]");
AnsiConsole.MarkupLine(" [dim]# Migrate files in current directory[/]");
AnsiConsole.MarkupLine(" [green]dotnet run[/]");
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine(" [dim]# Preview changes[/]");
AnsiConsole.MarkupLine(" [green]dotnet run -- --dry-run[/]");
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine(" [dim]# Migrate specific file[/]");
AnsiConsole.MarkupLine(" [green]dotnet run -- --path appsettings.Production.json[/]");
}
static void ShowVersion()
{
AnsiConsole.Write(new FigletText("v1.0.0").Color(Color.Cyan1));
AnsiConsole.MarkupLine("[bold]Blog Upgrade Assistant[/]");
AnsiConsole.MarkupLine($"[dim]Target Blog Version: 12.0[/]");
}