//
// ExtractNosFileCommand.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Net;
using System.Text;
using NosSmooth.Data.NOSFiles.Files;
using NosSmooth.Data.NOSFiles.Readers;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Results;
namespace NosSmooth.Data.CLI.Commands;
///
/// A command to extract files from a NosTale archive.
///
public class ExtractNosFileCommand : CommandGroup
{
private readonly FileReader _reader;
///
/// Initializes a new instance of the class.
///
/// The file reader.
public ExtractNosFileCommand(FileReader reader)
{
_reader = reader;
}
///
/// Handles extract command.
///
/// The input nos archive.
/// The output directory to put the extracted files to.
/// A result that may or may not have succeeded..
[Command("extract")]
public async Task HandleExtract
(
string inputFile,
[Option('o', "option")]
string outputDirectory = "out"
)
{
Directory.CreateDirectory(outputDirectory);
var readResult = _reader.ReadFileSystemFile(inputFile);
if (!readResult.IsSuccess)
{
return Result.FromError(readResult);
}
foreach (var file in readResult.Entity.Content.Files)
{
var outputPath = Path.Combine(outputDirectory, file.Path);
await File.WriteAllBytesAsync(outputPath, file.Content, CancellationToken);
}
return Result.FromSuccess();
}
}