77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using System.Text.Json;
|
|
|
|
namespace MatrixImageEditor;
|
|
|
|
internal static class Program
|
|
{
|
|
[STAThread]
|
|
private static int Main(string[] args)
|
|
{
|
|
try
|
|
{
|
|
if (args.Length > 0)
|
|
{
|
|
return RunCommand(args);
|
|
}
|
|
ApplicationConfiguration.Initialize();
|
|
Application.Run(new MainForm());
|
|
return 0;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
if (Environment.UserInteractive && args.Length == 0)
|
|
{
|
|
AppDialog.Error(null, exception.Message, "镜像配置编辑器");
|
|
}
|
|
else
|
|
{
|
|
Console.Error.WriteLine(exception.Message);
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
private static int RunCommand(string[] args)
|
|
{
|
|
if (args is ["--validate", var image])
|
|
{
|
|
var config = ImageOperations.ReadConfig(image);
|
|
var publicInfo = new
|
|
{
|
|
valid = true,
|
|
config.SoftwareVersion,
|
|
config.Account.Username,
|
|
config.Wifi.Ssid,
|
|
config.Ipv4.Mode,
|
|
config.Ipv4.Address,
|
|
};
|
|
Console.WriteLine(JsonSerializer.Serialize(publicInfo));
|
|
return 0;
|
|
}
|
|
if (args is ["--apply", var source, var configPath, var destination])
|
|
{
|
|
var sourcePath = ImagePathRules.ValidateExistingImage(source);
|
|
var destinationPath = ImagePathRules.ValidateNewDestination(sourcePath, destination);
|
|
var config = ImageConfigCodec.Deserialize(File.ReadAllText(configPath));
|
|
_ = ImageOperations.ReadConfig(sourcePath);
|
|
try
|
|
{
|
|
File.Copy(sourcePath, destinationPath, overwrite: false);
|
|
ImageOperations.WriteConfig(destinationPath, config);
|
|
ImageOperations.WriteSha256(destinationPath, null);
|
|
_ = ImageOperations.ReadConfig(destinationPath);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
var cleanupError = ImagePathRules.CleanupNewDestination(destinationPath);
|
|
if (cleanupError is not null) throw new IOException($"{exception.Message}\n清理失败:{cleanupError}", exception);
|
|
throw;
|
|
}
|
|
Console.WriteLine(destinationPath);
|
|
return 0;
|
|
}
|
|
Console.Error.WriteLine("用法:镜像配置编辑器.exe [--validate 镜像 | --apply 原镜像 配置.json 新镜像]");
|
|
return 2;
|
|
}
|
|
}
|