|
| 1 | +using Snap2HTMLNG.Shared.Builder; |
| 2 | +using Snap2HTMLNG.Shared.Models; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using System.Reflection; |
| 8 | +using Snap2HTMLNG.Shared.CLI; |
| 9 | + |
| 10 | +namespace Snap2HTMLNG.CLI |
| 11 | +{ |
| 12 | + internal class Program |
| 13 | + { |
| 14 | + |
| 15 | + private static bool _validationCheck = false; |
| 16 | + static void Main(string[] args) |
| 17 | + { |
| 18 | + |
| 19 | +#if DEBUG |
| 20 | + // Information |
| 21 | + CommandLine.WriteDebug($" THIS IS A PREVIEW BUILD."); |
| 22 | + // New Lines |
| 23 | + Console.WriteLine(""); |
| 24 | +#endif |
| 25 | + |
| 26 | + List<CommandLineModel> normalizedArgs = CommandLine.CommandLineSplit(args); |
| 27 | + |
| 28 | + if(normalizedArgs.Exists(x => x.Name == "help")) |
| 29 | + { |
| 30 | + HelpInformation(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + InitialValidation(normalizedArgs); |
| 35 | + |
| 36 | + // Check if the initial validation checks have passed |
| 37 | + if(_validationCheck) |
| 38 | + { |
| 39 | + string rootDirectory = normalizedArgs.Find(x => x.Name == "path").Value;// + @"\"; |
| 40 | + string saveDirectory = normalizedArgs.Find(x => x.Name == "output").Value; |
| 41 | + |
| 42 | + // Check if the user has requested a randomized file name and set if they have |
| 43 | + if(normalizedArgs.Exists(x =>x.Name == "randomize")) |
| 44 | + { |
| 45 | + CommandLine.WriteInformation($"Randomized file name requested..."); |
| 46 | + |
| 47 | + string randomFileName = $"{Guid.NewGuid()}.html"; |
| 48 | + |
| 49 | + saveDirectory = $@"{saveDirectory}\{randomFileName}"; |
| 50 | + |
| 51 | + CommandLine.WriteInformation($"Randomized file set to {randomFileName}"); |
| 52 | + |
| 53 | + CommandLine.WriteInformation($"Output path is now: {saveDirectory}"); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + // Validate that the Scan Path actually exists |
| 58 | + if (!Directory.Exists(rootDirectory)) |
| 59 | + { |
| 60 | + CommandLine.WriteError($"Your scan path {rootDirectory} does not exist or Snap2HTML-NG does not have access."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Check if the Save location actually exists |
| 65 | + if (!Directory.Exists(Path.GetDirectoryName(saveDirectory))) |
| 66 | + { |
| 67 | + CommandLine.WriteError($"Your save path {saveDirectory} does not exist or Snap2HTML-NG does not have access."); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Check if the rest of the settigns have been passed and assign |
| 72 | + bool skipHidden = !normalizedArgs.Exists(x => x.Name == "hidden"); // if found, do not skip |
| 73 | + bool skipSystem = !normalizedArgs.Exists(x => x.Name == "system"); // if found, do not skip |
| 74 | + |
| 75 | + // Check if the user wants to link files to the root which allows them to be easily selected when viewing in a browser |
| 76 | + bool linkFilesToRoot = false; |
| 77 | + string linkDirectory = ""; |
| 78 | + if (normalizedArgs.Exists(x => x.Name == "link")) |
| 79 | + { |
| 80 | + linkFilesToRoot = true; |
| 81 | + linkDirectory = normalizedArgs.Find(x => x.Name == "link").Value; |
| 82 | + } |
| 83 | + |
| 84 | + string title = $"Snapshot of {saveDirectory}"; |
| 85 | + if (normalizedArgs.Exists(x => x.Name == "title")) |
| 86 | + { |
| 87 | + title = normalizedArgs.Find(x => x.Name == "title").Value; |
| 88 | + } |
| 89 | + |
| 90 | + string searchPattern = "*"; // default is all |
| 91 | + if (normalizedArgs.Exists(x => x.Name == "pattern")) |
| 92 | + { |
| 93 | + searchPattern = normalizedArgs.Find(x => x.Name == "pattern").Value; |
| 94 | + } |
| 95 | + |
| 96 | +#if DEBUG |
| 97 | + foreach (var normalizedArg in normalizedArgs) |
| 98 | + { |
| 99 | + CommandLine.WriteDebug($"Name: {normalizedArg.Name}, Value: {normalizedArg.Value}"); |
| 100 | + } |
| 101 | +#endif |
| 102 | + // Create the settings model and assign the relevant information to each property |
| 103 | + UserSettingsModel usm = new UserSettingsModel |
| 104 | + { |
| 105 | + RootDirectory = rootDirectory, |
| 106 | + Title = title, |
| 107 | + OutputFile = saveDirectory, |
| 108 | + SkipHiddenItems = skipHidden, |
| 109 | + SkipSystemItems = skipSystem, |
| 110 | + OpenInBrowserAfterCapture = false, // this will always be false in console mode |
| 111 | + LinkFiles = linkFilesToRoot, |
| 112 | + LinkRoot = linkDirectory, |
| 113 | + SearchPattern = searchPattern |
| 114 | + }; |
| 115 | + |
| 116 | + DataBuilder.Build(usm, ApplicationInformation().ProductName, ApplicationInformation().ProductVersion); |
| 117 | + |
| 118 | + |
| 119 | + Console.ReadKey(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Checks if the user has passed through any arguments, as well as if they have included the two REQUIRED arguments |
| 126 | + /// </summary> |
| 127 | + /// <param name="normalizedArgs"> |
| 128 | + /// <see cref="List{T}"/> of arguments using <see cref="CommandLineModel"/> |
| 129 | + /// </param> |
| 130 | + static void InitialValidation(List<CommandLineModel> normalizedArgs) |
| 131 | + { |
| 132 | + // Check if we have included any arguments at all, otherwise exit out. |
| 133 | + if (normalizedArgs.Count == 0) |
| 134 | + { |
| 135 | + CommandLine.WriteError("No arguments have been supplied that are recognized. Use -h for help."); |
| 136 | + _validationCheck = false; |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Check if we have included the REQUIRED argument -path:, otherwise exit out. |
| 141 | + if (!normalizedArgs.Exists(x => x.Name == "path")) |
| 142 | + { |
| 143 | + CommandLine.WriteError("You are missing the required argument '-path:'. Use -h for help."); |
| 144 | + _validationCheck = false; |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + // Check if we have included the REQUIRED argument -output:, otherwise exit out. |
| 149 | + if (!normalizedArgs.Exists(x => x.Name == "output")) |
| 150 | + { |
| 151 | + CommandLine.WriteError("You are missing the required argument '-output:'. Use -h for help."); |
| 152 | + _validationCheck = false; |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + _validationCheck = true; |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Returns Help information for using the Command Line |
| 161 | + /// </summary> |
| 162 | + static void HelpInformation() |
| 163 | + { |
| 164 | + Console.ForegroundColor = ConsoleColor.White; |
| 165 | + |
| 166 | + // Information |
| 167 | + Console.WriteLine(" Application Information"); |
| 168 | + Console.WriteLine($" {ApplicationInformation().ProductName} v{ApplicationInformation().ProductVersion}"); |
| 169 | + |
| 170 | + // New Lines |
| 171 | + Console.WriteLine(""); |
| 172 | + |
| 173 | + // Description |
| 174 | + Console.WriteLine(" Description:"); |
| 175 | + Console.WriteLine(" Help information for Snap2HTML-NG.CLI"); |
| 176 | + |
| 177 | + // New Lines |
| 178 | + Console.WriteLine(""); |
| 179 | + |
| 180 | + // Usage |
| 181 | + Console.WriteLine(" Usage:"); |
| 182 | + Console.WriteLine(" Snap2HTML-NG.CLI [options]"); |
| 183 | + |
| 184 | + // New Lines |
| 185 | + Console.WriteLine(""); |
| 186 | + |
| 187 | + // Options |
| 188 | + Console.WriteLine(" Options:"); |
| 189 | + Console.WriteLine(" -path: [Required] The directory you want to scan"); |
| 190 | + Console.WriteLine(" -output: [Required] The directory where you want to save the file, including the filename unless using -randomize"); |
| 191 | + Console.WriteLine(" -link: [Optional] The directory where you want to link files in the html file"); |
| 192 | + Console.WriteLine(" -title: [Optional] The title of the file which appears at the top of the html file"); |
| 193 | + Console.WriteLine(" -hidden [Optional] Hides Hidden files from the scan, default is TRUE"); |
| 194 | + Console.WriteLine(" -system [Optional] Hides System files from the scan, default is TRUE"); |
| 195 | + Console.WriteLine(" -help, -h [Optional] Shows this information"); |
| 196 | + Console.WriteLine(" -pattern [Optional] Search pattern to only return certain files, default is *"); |
| 197 | + Console.WriteLine(" -randomize [Optional] Generates a random file name instead of needing to specify one in -output"); |
| 198 | + |
| 199 | + // New Lines |
| 200 | + Console.WriteLine(""); |
| 201 | + |
| 202 | + // Examples |
| 203 | + Console.WriteLine(" Examples:"); |
| 204 | + Console.WriteLine($" Snap2HTML-NG.CLI -path:\"C:\\Users\\{Environment.UserName}\\Downloads\" -output:\"C:\\Users\\{Environment.UserName}\\Desktop\\Downloads.html\" "); |
| 205 | + Console.WriteLine($" Snap2HTML-NG.CLI -path:\"C:\\Users\\{Environment.UserName}\\Pictures\" -output:\"C:\\Users\\{Environment.UserName}\\Desktop\" -randomize "); |
| 206 | + Console.WriteLine($" Snap2HTML-NG.CLI -path:\"C:\\Users\\{Environment.UserName}\\Downloads\" -output:\"C:\\Users\\{Environment.UserName}\\Desktop\" -link:\"C:\\Users\\{Environment.UserName}\\Downloads\" -randomize "); |
| 207 | + Console.WriteLine($" Snap2HTML-NG.CLI -path:\"C:\\Users\\{Environment.UserName}\\Downloads\" -output:\"C:\\Users\\{Environment.UserName}\\Desktop\" -link:\"C:\\Users\\{Environment.UserName}\\Downloads\" -randomize -pattern:\"*.mp4\""); |
| 208 | + Console.WriteLine($" Snap2HTML-NG.CLI -path:\"C:\\Users\\{Environment.UserName}\\Videos\" -output:\"C:\\Users\\{Environment.UserName}\\Desktop\\videos.html\" -link:\"C:\\Users\\{Environment.UserName}\\Videos\" -pattern:\"*.mp4\" -title:\"Home Videos\""); |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// Gets the application information using <see cref="FileVersionInfo"/> and <see cref="Assembly"/> |
| 214 | + /// </summary> |
| 215 | + /// <returns> |
| 216 | + /// <see cref="FileVersionInfo"/> |
| 217 | + /// </returns> |
| 218 | + static FileVersionInfo ApplicationInformation() |
| 219 | + { |
| 220 | + // Get product name etc. |
| 221 | + Assembly assembly = Assembly.GetExecutingAssembly(); |
| 222 | + FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); |
| 223 | + |
| 224 | + return fvi; |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments