diff --git a/Wabbajack.CLI/CLIUtils.cs b/Wabbajack.CLI/CLIUtils.cs new file mode 100644 index 00000000..c9a081e8 --- /dev/null +++ b/Wabbajack.CLI/CLIUtils.cs @@ -0,0 +1,27 @@ +using System; + +namespace Wabbajack.CLI +{ + internal static class CLIUtils + { + internal static void Log(string msg, bool newLine = true) + { + //TODO: maybe also write to a log file? + if(newLine) + Console.WriteLine(msg); + else + Console.Write(msg); + } + + internal static int Exit(string msg, int code) + { + Log(msg); + return code; + } + + internal static void LogException(Exception e, string msg) + { + Console.WriteLine($"{msg}\n{e}"); + } + } +} diff --git a/Wabbajack.CLI/Verbs/ChangeDownload.cs b/Wabbajack.CLI/Verbs/ChangeDownload.cs index 774c709b..966dfad9 100644 --- a/Wabbajack.CLI/Verbs/ChangeDownload.cs +++ b/Wabbajack.CLI/Verbs/ChangeDownload.cs @@ -41,11 +41,6 @@ namespace Wabbajack.CLI.Verbs [Option("meta", Default = true, HelpText = "Whether to also transfer the meta file for the archive")] public bool IncludeMeta { get; set; } - private static void Log(string msg) - { - Console.WriteLine(msg); - } - private struct TransferFile { public readonly string Input; @@ -63,34 +58,22 @@ namespace Wabbajack.CLI.Verbs protected override async Task Run() { if (!File.Exists(Modlist)) - { - Log($"The file {Modlist} does not exist!"); - return -1; - } + return CLIUtils.Exit($"The file {Modlist} does not exist!", -1); if (!Directory.Exists(Input)) - { - Log($"The input directory {Input} does not exist!"); - return -1; - } + return CLIUtils.Exit($"The input directory {Input} does not exist!", -1); if (!Directory.Exists(Output)) { - Log($"The output directory {Output} does not exist, it will be created."); + CLIUtils.Log($"The output directory {Output} does not exist, it will be created."); Directory.CreateDirectory(Output); } if (!Modlist.EndsWith(Consts.ModListExtension) && !Modlist.EndsWith("modlist.txt")) - { - Log($"The file {Modlist} is not a valid modlist file!"); - return -1; - } + return CLIUtils.Exit($"The file {Modlist} is not a valid modlist file!", -1); if (Copy && Move) - { - Log("You can't set both copy and move flags!"); - return -1; - } + return CLIUtils.Exit("You can't set both copy and move flags!", -1); var isModlist = Modlist.EndsWith(Consts.ModListExtension); @@ -106,17 +89,15 @@ namespace Wabbajack.CLI.Verbs } catch (Exception e) { - Log($"Error while loading the Modlist!\n{e}"); - return 1; + return CLIUtils.Exit($"Error while loading the Modlist!\n{e}", 1); } if (modlist == null) { - Log("The Modlist could not be loaded!"); - return 1; + return CLIUtils.Exit("The Modlist could not be loaded!", 1); } - Log($"Modlist contains {modlist.Archives.Count} archives."); + CLIUtils.Log($"Modlist contains {modlist.Archives.Count} archives."); modlist.Archives.Do(a => { @@ -125,11 +106,11 @@ namespace Wabbajack.CLI.Verbs if (!File.Exists(inputPath)) { - Log($"File {inputPath} does not exist, skipping."); + CLIUtils.Log($"File {inputPath} does not exist, skipping."); return; } - Log($"Adding {inputPath} to the transfer list."); + CLIUtils.Log($"Adding {inputPath} to the transfer list."); list.Add(new TransferFile(inputPath, outputPath)); var metaInputPath = Path.Combine(inputPath, ".meta"); @@ -137,34 +118,34 @@ namespace Wabbajack.CLI.Verbs if (File.Exists(metaInputPath)) { - Log($"Found meta file {metaInputPath}"); + CLIUtils.Log($"Found meta file {metaInputPath}"); if (IncludeMeta) { - Log($"Adding {metaInputPath} to the transfer list."); + CLIUtils.Log($"Adding {metaInputPath} to the transfer list."); list.Add(new TransferFile(metaInputPath, metaOutputPath)); } else { - Log($"Meta file {metaInputPath} will be ignored."); + CLIUtils.Log($"Meta file {metaInputPath} will be ignored."); } } else { - Log($"Found no meta file for {inputPath}"); + CLIUtils.Log($"Found no meta file for {inputPath}"); if (IncludeMeta) { if (string.IsNullOrWhiteSpace(a.Meta)) { - Log($"Meta for {a.Name} is empty, this should not be possible but whatever."); + CLIUtils.Log($"Meta for {a.Name} is empty, this should not be possible but whatever."); return; } - Log("Adding meta from archive info the transfer list"); + CLIUtils.Log("Adding meta from archive info the transfer list"); list.Add(new TransferFile(a.Meta, metaOutputPath, true)); } else { - Log($"Meta will be ignored for {a.Name}"); + CLIUtils.Log($"Meta will be ignored for {a.Name}"); } } }); @@ -172,28 +153,20 @@ namespace Wabbajack.CLI.Verbs else { if (!Directory.Exists(Mods)) - { - Log($"Mods directory {Mods} does not exist!"); - return -1; - } + return CLIUtils.Exit($"Mods directory {Mods} does not exist!", -1); - Log($"Reading modlist.txt from {Modlist}"); + CLIUtils.Log($"Reading modlist.txt from {Modlist}"); string[] modlist = File.ReadAllLines(Modlist); if (modlist == null || modlist.Length == 0) - { - Log($"Provided modlist.txt file at {Modlist} is empty or could not be read!"); - return -1; - } + return CLIUtils.Exit($"Provided modlist.txt file at {Modlist} is empty or could not be read!", -1); var mods = modlist.Where(s => s.StartsWith("+")).Select(s => s.Substring(1)).ToHashSet(); - if (mods.Count == 0) - { - Log("Counted mods from modlist.txt are 0!"); - return -1; - } - Log($"Found {mods.Count} mods in modlist.txt"); + if (mods.Count == 0) + return CLIUtils.Exit("Counted mods from modlist.txt are 0!", -1); + + CLIUtils.Log($"Found {mods.Count} mods in modlist.txt"); var downloads = new HashSet(); @@ -204,14 +177,14 @@ namespace Wabbajack.CLI.Verbs var meta = Path.Combine(d, "meta.ini"); if (!File.Exists(meta)) { - Log($"Mod meta file {meta} does not exist, skipping"); + CLIUtils.Log($"Mod meta file {meta} does not exist, skipping"); return; } string[] ini = File.ReadAllLines(meta); if (ini == null || ini.Length == 0) { - Log($"Mod meta file {meta} could not be read or is empty!"); + CLIUtils.Log($"Mod meta file {meta} could not be read or is empty!"); return; } @@ -219,47 +192,47 @@ namespace Wabbajack.CLI.Verbs .Select(i => i.Replace("installationFile=", "")) .Do(i => { - Log($"Found installationFile {i}"); + CLIUtils.Log($"Found installationFile {i}"); downloads.Add(i); }); }); - Log($"Found {downloads.Count} installationFiles from mod metas."); + CLIUtils.Log($"Found {downloads.Count} installationFiles from mod metas."); Directory.EnumerateFiles(Input, "*", SearchOption.TopDirectoryOnly) .Where(f => downloads.Contains(Path.GetFileNameWithoutExtension(f))) .Do(f => { - Log($"Found archive {f}"); + CLIUtils.Log($"Found archive {f}"); var outputPath = Path.Combine(Output, Path.GetFileName(f)); - Log($"Adding {f} to the transfer list"); + CLIUtils.Log($"Adding {f} to the transfer list"); list.Add(new TransferFile(f, outputPath)); var metaInputPath = Path.Combine(f, ".meta"); if (File.Exists(metaInputPath)) { - Log($"Found meta file for {f} at {metaInputPath}"); + CLIUtils.Log($"Found meta file for {f} at {metaInputPath}"); if (IncludeMeta) { var metaOutputPath = Path.Combine(outputPath, ".meta"); - Log($"Adding {metaInputPath} to the transfer list."); + CLIUtils.Log($"Adding {metaInputPath} to the transfer list."); list.Add(new TransferFile(metaInputPath, metaOutputPath)); } else { - Log("Meta file will be ignored"); + CLIUtils.Log("Meta file will be ignored"); } } else { - Log($"Found no meta file for {f}"); + CLIUtils.Log($"Found no meta file for {f}"); } }); } - Log($"Transfer list contains {list.Count} items"); + CLIUtils.Log($"Transfer list contains {list.Count} items"); var success = 0; var failed = 0; var skipped = 0; @@ -269,24 +242,24 @@ namespace Wabbajack.CLI.Verbs { if (Overwrite) { - Log($"Output file {f.Output} already exists, it will be overwritten"); + CLIUtils.Log($"Output file {f.Output} already exists, it will be overwritten"); if (f.IsMeta || Move) { - Log($"Deleting file at {f.Output}"); + CLIUtils.Log($"Deleting file at {f.Output}"); try { File.Delete(f.Output); } catch (Exception e) { - Log($"Could not delete file {f.Output}!\n{e}"); + CLIUtils.Log($"Could not delete file {f.Output}!\n{e}"); failed++; } } } else { - Log($"Output file {f.Output} already exists, skipping"); + CLIUtils.Log($"Output file {f.Output} already exists, skipping"); skipped++; return; } @@ -294,7 +267,7 @@ namespace Wabbajack.CLI.Verbs if (f.IsMeta) { - Log($"Writing meta data to {f.Output}"); + CLIUtils.Log($"Writing meta data to {f.Output}"); try { File.WriteAllText(f.Output, f.Input, Encoding.UTF8); @@ -302,7 +275,7 @@ namespace Wabbajack.CLI.Verbs } catch (Exception e) { - Log($"Error while writing meta data to {f.Output}!\n{e}"); + CLIUtils.Log($"Error while writing meta data to {f.Output}!\n{e}"); failed++; } } @@ -310,7 +283,7 @@ namespace Wabbajack.CLI.Verbs { if (Copy) { - Log($"Copying file {f.Input} to {f.Output}"); + CLIUtils.Log($"Copying file {f.Input} to {f.Output}"); try { File.Copy(f.Input, f.Output, Overwrite ? CopyOptions.None : CopyOptions.FailIfExists, CopyMoveProgressHandler, null); @@ -318,13 +291,13 @@ namespace Wabbajack.CLI.Verbs } catch (Exception e) { - Log($"Error while copying file {f.Input} to {f.Output}!\n{e}"); + CLIUtils.Log($"Error while copying file {f.Input} to {f.Output}!\n{e}"); failed++; } } else if(Move) { - Log($"Moving file {f.Input} to {f.Output}"); + CLIUtils.Log($"Moving file {f.Input} to {f.Output}"); try { File.Move(f.Input, f.Output, Overwrite ? MoveOptions.ReplaceExisting : MoveOptions.None, CopyMoveProgressHandler, null); @@ -332,16 +305,16 @@ namespace Wabbajack.CLI.Verbs } catch (Exception e) { - Log($"Error while moving file {f.Input} to {f.Output}!\n{e}"); + CLIUtils.Log($"Error while moving file {f.Input} to {f.Output}!\n{e}"); failed++; } } } }); - Log($"Skipped transfers: {skipped}"); - Log($"Failed transfers: {failed}"); - Log($"Successful transfers: {success}"); + CLIUtils.Log($"Skipped transfers: {skipped}"); + CLIUtils.Log($"Failed transfers: {failed}"); + CLIUtils.Log($"Successful transfers: {success}"); return 0; } diff --git a/Wabbajack.CLI/Verbs/DownloadUrl.cs b/Wabbajack.CLI/Verbs/DownloadUrl.cs index 8d2a4dda..ead09310 100644 --- a/Wabbajack.CLI/Verbs/DownloadUrl.cs +++ b/Wabbajack.CLI/Verbs/DownloadUrl.cs @@ -23,11 +23,8 @@ namespace Wabbajack.CLI.Verbs { var state = DownloadDispatcher.Infer(Url); if (state == null) - { - Console.WriteLine($"Could not find download source for URL {Url}"); - return 1; - } - + return CLIUtils.Exit($"Could not find download source for URL {Url}", 1); + DownloadDispatcher.PrepareAll(new []{state}); using var queue = new WorkQueue(); diff --git a/Wabbajack.CLI/Verbs/UpdateModlists.cs b/Wabbajack.CLI/Verbs/UpdateModlists.cs index ed6fac60..bc1922e5 100644 --- a/Wabbajack.CLI/Verbs/UpdateModlists.cs +++ b/Wabbajack.CLI/Verbs/UpdateModlists.cs @@ -10,7 +10,7 @@ namespace Wabbajack.CLI.Verbs { protected override async Task Run() { - Console.WriteLine($"Job ID: {await AuthorAPI.UpdateServerModLists()}"); + CLIUtils.Log($"Job ID: {await AuthorAPI.UpdateServerModLists()}"); return 0; } } diff --git a/Wabbajack.CLI/Verbs/UpdateNexusCache.cs b/Wabbajack.CLI/Verbs/UpdateNexusCache.cs index 6cb2c16d..a293ecbb 100644 --- a/Wabbajack.CLI/Verbs/UpdateNexusCache.cs +++ b/Wabbajack.CLI/Verbs/UpdateNexusCache.cs @@ -11,7 +11,7 @@ namespace Wabbajack.CLI.Verbs { protected override async Task Run() { - Console.WriteLine($"Job ID: {await AuthorAPI.UpdateNexusCache()}"); + CLIUtils.Log($"Job ID: {await AuthorAPI.UpdateNexusCache()}"); return 0; } } diff --git a/Wabbajack.CLI/Verbs/Validate.cs b/Wabbajack.CLI/Verbs/Validate.cs index 8ca8acc4..5a21e0af 100644 --- a/Wabbajack.CLI/Verbs/Validate.cs +++ b/Wabbajack.CLI/Verbs/Validate.cs @@ -30,18 +30,12 @@ namespace Wabbajack.CLI.Verbs protected override async Task Run() { if (!File.Exists(Input)) - { - Console.WriteLine($"The file {Input} does not exist!"); - return -1; - } + return CLIUtils.Exit($"The file {Input} does not exist!", -1); if (!Input.EndsWith(Consts.ModListExtension)) - { - Console.WriteLine($"The file {Input} does not end with {Consts.ModListExtension}!"); - return -1; - } - + return CLIUtils.Exit($"The file {Input} does not end with {Consts.ModListExtension}!", -1); + ModList modlist; try @@ -50,14 +44,12 @@ namespace Wabbajack.CLI.Verbs } catch (Exception e) { - Console.WriteLine($"Error while loading the Modlist!\n{e}"); - return 1; + return CLIUtils.Exit($"Error while loading the Modlist!\n{e}", 1); } if (modlist == null) { - Console.WriteLine($"The Modlist could not be loaded!"); - return 1; + return CLIUtils.Exit($"The Modlist could not be loaded!", 1); } @@ -69,12 +61,10 @@ namespace Wabbajack.CLI.Verbs } catch (Exception e) { - Console.WriteLine($"Error during Validation!\n{e}"); - return 1; + return CLIUtils.Exit($"Error during Validation!\n{e}", 1); } - Console.WriteLine("The Modlist passed the Validation"); - return 0; + return CLIUtils.Exit("The Modlist passed the Validation", 0); } } }