From fd66313858ae40c8d2ce436f147cb2733b1465bd Mon Sep 17 00:00:00 2001 From: erri120 Date: Mon, 6 Apr 2020 15:20:14 +0200 Subject: [PATCH] Added Extension check for files --- Wabbajack.CLI/CLIUtils.cs | 23 +++++++++++++++++++++-- Wabbajack.CLI/Verbs/Validate.cs | 14 ++------------ Wabbajack.Common/Consts.cs | 2 +- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Wabbajack.CLI/CLIUtils.cs b/Wabbajack.CLI/CLIUtils.cs index f53844f6..34b462a6 100644 --- a/Wabbajack.CLI/CLIUtils.cs +++ b/Wabbajack.CLI/CLIUtils.cs @@ -30,7 +30,13 @@ namespace Wabbajack.CLI /// /// Validating if the file exists /// - internal class IsFileAttribute : AValidateAttribute { } + internal class IsFileAttribute : AValidateAttribute + { + /// + /// Extension the file should have + /// + public string? Extension { get; set; } + } /// /// 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))) diff --git a/Wabbajack.CLI/Verbs/Validate.cs b/Wabbajack.CLI/Verbs/Validate.cs index a08e7100..893892c9 100644 --- a/Wabbajack.CLI/Verbs/Validate.cs +++ b/Wabbajack.CLI/Verbs/Validate.cs @@ -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; } /// /// Runs the Validation of a Modlist /// - /// - /// - /// -1 bad Input - /// 0 valid modlist - /// 1 broken modlist - /// - /// + /// protected override async Task 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 diff --git a/Wabbajack.Common/Consts.cs b/Wabbajack.Common/Consts.cs index 085f16a4..39dec636 100644 --- a/Wabbajack.Common/Consts.cs +++ b/Wabbajack.Common/Consts.cs @@ -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";