support pipes for input file at get and set command

This commit is contained in:
Syping 2025-10-27 21:08:03 +01:00
parent a406e079c5
commit 43902dcbfb

View file

@ -81,10 +81,19 @@ internal static class Program {
} }
} }
private static void Get(FileInfo photoFile, String dataType, String outputFile) { private static void Get(String inputFile, String dataType, String outputFile) {
try { try {
using Photo photo = new(); using Photo photo = new();
photo.LoadFile(photoFile.FullName);
if (inputFile == "-" || inputFile == String.Empty) {
using MemoryStream photoStream = new();
using Stream input = Console.OpenStandardInput();
input.CopyTo(photoStream);
photo.Load(photoStream.ToArray());
}
else {
photo.LoadFile(inputFile);
}
Byte[] content = []; Byte[] content = [];
switch (dataType.ToLowerInvariant()) { switch (dataType.ToLowerInvariant()) {
@ -147,16 +156,29 @@ internal static class Program {
} }
} }
private static void Set(FileInfo photoFile, String? format, String? jpegFile, String? description, String? json, String? title, bool updateSign, String? outputFile) { private static void Set(String inputFile, String? format, String? jpegFile, String? description, String? json, String? title, bool updateSign, String? outputFile) {
if (format == null && jpegFile == null && description == null if (format == null && jpegFile == null && description == null
&& json == null && title == null && !updateSign) { && json == null && title == null && !updateSign) {
Console.Error.WriteLine("No value has being set"); Console.Error.WriteLine("No value has being set");
Environment.Exit(1); Environment.Exit(1);
} }
else if (inputFile == "-" && jpegFile == "-") {
Console.Error.WriteLine("Multiple pipes are not supported");
Environment.Exit(1);
}
try { try {
using Photo photo = new(); using Photo photo = new();
photo.LoadFile(photoFile.FullName);
if (inputFile == "-" || inputFile == String.Empty) {
using MemoryStream photoStream = new();
using Stream input = Console.OpenStandardInput();
input.CopyTo(photoStream);
photo.Load(photoStream.ToArray());
}
else {
photo.LoadFile(inputFile);
}
if (format != null) { if (format != null) {
photo.Format = format.ToLowerInvariant() switch { photo.Format = format.ToLowerInvariant() switch {
@ -198,7 +220,7 @@ internal static class Program {
else { else {
String tempFile = Path.GetTempFileName(); String tempFile = Path.GetTempFileName();
photo.SaveFile(tempFile); photo.SaveFile(tempFile);
File.Move(tempFile, !String.IsNullOrEmpty(outputFile) ? outputFile : photoFile.FullName, true); File.Move(tempFile, !String.IsNullOrEmpty(outputFile) ? outputFile : inputFile, true);
} }
} }
catch (RagePhotoException exception) { catch (RagePhotoException exception) {
@ -341,8 +363,8 @@ internal static class Program {
private static Command GetCommand { private static Command GetCommand {
get { get {
Argument<FileInfo> photoArgument = new("photo") { Argument<String> inputArgument = new("input") {
Description = "Photo File" Description = "Input File"
}; };
Argument<String> dataTypeArgument = new("dataType") { Argument<String> dataTypeArgument = new("dataType") {
Description = "Data Type", Description = "Data Type",
@ -360,10 +382,10 @@ internal static class Program {
DefaultValueFactory = _ => "-" DefaultValueFactory = _ => "-"
}; };
Command getCommand = new("get", "Get Photo Data") { Command getCommand = new("get", "Get Photo Data") {
photoArgument, dataTypeArgument, outputOption inputArgument, dataTypeArgument, outputOption
}; };
getCommand.SetAction(result => Get( getCommand.SetAction(result => Get(
result.GetRequiredValue(photoArgument), result.GetRequiredValue(inputArgument),
result.GetRequiredValue(dataTypeArgument), result.GetRequiredValue(dataTypeArgument),
result.GetRequiredValue(outputOption))); result.GetRequiredValue(outputOption)));
return getCommand; return getCommand;
@ -372,8 +394,8 @@ internal static class Program {
private static Command SetCommand { private static Command SetCommand {
get { get {
Argument<FileInfo> photoArgument = new("photo") { Argument<String> inputArgument = new("input") {
Description = "Photo File" Description = "Input File"
}; };
Option<String?> formatOption = new("--format", "-f") { Option<String?> formatOption = new("--format", "-f") {
Description = "Photo Format" Description = "Photo Format"
@ -397,10 +419,10 @@ internal static class Program {
Description = "Output File" Description = "Output File"
}; };
Command setCommand = new("set", "Set Photo Data") { Command setCommand = new("set", "Set Photo Data") {
photoArgument, formatOption, jpegOption, descriptionOption, jsonOption, titleOption, updateSignOption, outputOption inputArgument, formatOption, jpegOption, descriptionOption, jsonOption, titleOption, updateSignOption, outputOption
}; };
setCommand.SetAction(result => Set( setCommand.SetAction(result => Set(
result.GetRequiredValue(photoArgument), result.GetRequiredValue(inputArgument),
result.GetValue(formatOption), result.GetValue(formatOption),
result.GetValue(jpegOption), result.GetValue(jpegOption),
result.GetValue(descriptionOption), result.GetValue(descriptionOption),