Added Extension check for files

This commit is contained in:
erri120 2020-04-06 15:20:14 +02:00
parent 0f4c843730
commit fd66313858
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
3 changed files with 24 additions and 15 deletions

View File

@ -30,7 +30,13 @@ namespace Wabbajack.CLI
/// <summary>
/// Validating if the file exists
/// </summary>
internal class IsFileAttribute : AValidateAttribute { }
internal class IsFileAttribute : AValidateAttribute
{
/// <summary>
/// Extension the file should have
/// </summary>
public string? Extension { get; set; }
}
/// <summary>
/// Validating if the directory exists
@ -89,8 +95,21 @@ namespace Wabbajack.CLI
if (p.HasAttribute(typeof(IsFileAttribute)))
{
var fileAttribute = (IsFileAttribute)attribute;
isFile = true;
valid = File.Exists(value);
if (!File.Exists(value))
valid = false;
else
{
if (!string.IsNullOrWhiteSpace(fileAttribute.Extension))
{
valid = value.EndsWith(fileAttribute.Extension);
if(!valid)
Exit($"The file {value} does not have the extension {fileAttribute.Extension}!",
ExitCode.BadArguments);
}
}
}
if (p.HasAttribute(typeof(IsDirectoryAttribute)))

View File

@ -4,32 +4,22 @@ using CommandLine;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Validation;
using File = Alphaleonis.Win32.Filesystem.File;
namespace Wabbajack.CLI.Verbs
{
[Verb("validate", HelpText = @"Validates a Modlist")]
public class Validate : AVerb
{
[IsFile(CustomMessage = "The modlist file %1 does not exist!")]
[IsFile(CustomMessage = "The modlist file %1 does not exist!", Extension = Consts.ModListExtension)]
[Option('i', "input", Required = true, HelpText = @"Modlist file")]
public string? Input { get; set; }
/// <summary>
/// Runs the Validation of a Modlist
/// </summary>
/// <returns>
/// <para>
/// <c>-1</c> bad Input
/// <c>0</c> valid modlist
/// <c>1</c> broken modlist
/// </para>
/// </returns>
/// <returns></returns>
protected override async Task<int> Run()
{
if (Input != null && !Input.EndsWith(Consts.ModListExtension))
return CLIUtils.Exit($"The file {Input} does not end with {Consts.ModListExtension}!", ExitCode.BadArguments);
ModList modlist;
try

View File

@ -90,7 +90,7 @@ namespace Wabbajack.Common
public static string HashFileExtension => ".xxHash";
public static string MetaFileExtension => ".meta";
public static string ModListExtension = ".wabbajack";
public const string ModListExtension = ".wabbajack";
public static string LocalAppDataPath => Path.Combine(KnownFolders.LocalAppData.Path, "Wabbajack");
public static string MetricsKeyHeader => "x-metrics-key";