All checks were successful
Run the JSON parser tests / test (push) Has been skipped
74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using TextCopy;
|
|
|
|
namespace Jtr.Tools.CLI;
|
|
|
|
public static class InputOutputHandler
|
|
{
|
|
public static async Task<string> HandleInput(InputSource input, string? inputArgument, FileInfo? inputFile)
|
|
{
|
|
string json;
|
|
|
|
switch (input)
|
|
{
|
|
case InputSource.s:
|
|
case InputSource.std:
|
|
case InputSource.stdin:
|
|
json = inputArgument ?? string.Empty;
|
|
break;
|
|
|
|
case InputSource.c:
|
|
case InputSource.clip:
|
|
case InputSource.clipboard:
|
|
json = await ClipboardService.GetTextAsync() ?? string.Empty;
|
|
break;
|
|
|
|
case InputSource.f:
|
|
case InputSource.file:
|
|
if (inputFile is null)
|
|
throw new ArgumentException("Input file was not specified.");
|
|
|
|
if (!inputFile.Exists)
|
|
throw new FileNotFoundException($"File {inputFile.FullName} does not exist.");
|
|
|
|
json = await File.ReadAllTextAsync(inputFile.FullName);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception($"Input type '{input}' is not supported.");
|
|
}
|
|
|
|
return json.Trim();
|
|
}
|
|
|
|
public static async Task HandleOutput(OutputOptions output, FileInfo? outputFile, string result)
|
|
{
|
|
switch (output)
|
|
{
|
|
case OutputOptions.s:
|
|
case OutputOptions.std:
|
|
case OutputOptions.stdout:
|
|
Console.WriteLine(result);
|
|
break;
|
|
|
|
case OutputOptions.c:
|
|
case OutputOptions.clip:
|
|
case OutputOptions.clipboard:
|
|
await ClipboardService.SetTextAsync(result);
|
|
break;
|
|
|
|
case OutputOptions.f:
|
|
case OutputOptions.file:
|
|
if (outputFile is null)
|
|
throw new ArgumentException("Output file was not specified.");
|
|
|
|
if (outputFile.Exists)
|
|
throw new Exception($"File {outputFile.FullName} already exists.");
|
|
|
|
await File.WriteAllTextAsync(outputFile.FullName, result);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception($"Output type '{output}' is not supported.");
|
|
}
|
|
}
|
|
} |