| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.IO.Abstractions; |
| | 4 | | using CommandLine; |
| | 5 | | using Mklinker.Abstractions; |
| | 6 | | using System.Collections.Generic; |
| | 7 | |
|
| | 8 | | namespace Mklinker.Commands { |
| | 9 | |
|
| | 10 | | [Verb ("validate", HelpText = "This will validate the config file to see if it is valid")] |
| | 11 | | class ValidateCommand : GlobalOptions { |
| | 12 | |
|
| | 13 | | [Option ("all", HelpText = "This will list all entries in config and not just the ones that didn't pass validati |
| 17 | 14 | | public bool displayAll { get; private set; } |
| | 15 | |
|
| 0 | 16 | | public ValidateCommand() : base() {} |
| | 17 | |
|
| 8 | 18 | | public ValidateCommand(bool displayAll, string path) : base(path) { |
| 4 | 19 | | this.displayAll = displayAll; |
| 4 | 20 | | } |
| | 21 | |
|
| 4 | 22 | | internal void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver path |
| 0 | 23 | | if (!configHandler.DoesConfigExist(path)) { |
| 0 | 24 | | console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create |
| 0 | 25 | | return; |
| | 26 | | } |
| | 27 | |
|
| 4 | 28 | | IConfig config = configHandler.LoadConfig(path); |
| 4 | 29 | | bool isValid = true; |
| | 30 | |
|
| 24 | 31 | | foreach (ConfigLink configLink in config.LinkList) { |
| 4 | 32 | | string resolvedSourcePath = pathResolver.GetAbsoluteResolvedPath(configLink.sourcePath, config.Variables |
| 4 | 33 | | string resolvedTargetPath = pathResolver.GetAbsoluteResolvedPath(configLink.targetPath, config.Variables |
| | 34 | |
|
| 4 | 35 | | bool validation1 = ValidateExistence(fileSystem, resolvedSourcePath); |
| 4 | 36 | | bool validation2 = ValidateLinkType(fileSystem, resolvedSourcePath, configLink.linkType); |
| 4 | 37 | | bool validation3 = ValidateDuplicate(pathResolver, config, resolvedTargetPath); |
| | 38 | |
|
| 6 | 39 | | if (displayAll || !validation1 || !validation2 || !validation3) { |
| 2 | 40 | | console.WriteLine($"\n{ configLink.ToString() }"); |
| | 41 | |
|
| 2 | 42 | | isValid = false; |
| 2 | 43 | | } |
| | 44 | |
|
| 5 | 45 | | if (!validation1 || displayAll) { |
| 1 | 46 | | console.WriteLine ($"\t# Source path exists: { (validation1 ? "Yes" : "No") }", |
| 1 | 47 | | validation1 |
| 1 | 48 | | ? IConsole.ContentType.Positive |
| 1 | 49 | | : IConsole.ContentType.Negative); |
| 1 | 50 | | } |
| | 51 | |
|
| 6 | 52 | | if (!validation2 || displayAll) { |
| 2 | 53 | | console.WriteLine ($"\t# Link type acceptable: { (validation2 ? "Yes" : "No") }", |
| 2 | 54 | | validation2 |
| 2 | 55 | | ? IConsole.ContentType.Positive |
| 2 | 56 | | : IConsole.ContentType.Negative); |
| 2 | 57 | | } |
| | 58 | |
|
| 0 | 59 | | if (!validation3 || displayAll) { |
| 0 | 60 | | console.WriteLine ($"\t# Duplicate target path exists: { (validation3 ? "False" : "True") }", |
| 0 | 61 | | validation3 |
| 0 | 62 | | ? IConsole.ContentType.Positive |
| 0 | 63 | | : IConsole.ContentType.Negative); |
| 0 | 64 | | } |
| 4 | 65 | | } |
| | 66 | |
|
| 4 | 67 | | if (config.LinkList.Count == 0) |
| 1 | 68 | | console.WriteLine("Config is empty"); |
| 3 | 69 | | else if (isValid) |
| 1 | 70 | | console.WriteLine("Config is 100% valid"); |
| 4 | 71 | | } |
| | 72 | |
|
| 4 | 73 | | internal bool ValidateExistence (IFileSystem fileSystem, string resolvedSourcePath) { |
| 4 | 74 | | return fileSystem.File.Exists(resolvedSourcePath) || fileSystem.Directory.Exists(resolvedSourcePath); |
| 4 | 75 | | } |
| | 76 | |
|
| 4 | 77 | | internal bool ValidateLinkType (IFileSystem fileSystem, string resolvedSourcePath, ConfigLink.LinkType linkType) |
| 4 | 78 | | if (linkType == ConfigLink.LinkType.Default) |
| 1 | 79 | | return true; |
| | 80 | |
|
| 4 | 81 | | if (fileSystem.File.Exists(resolvedSourcePath)) { |
| 1 | 82 | | return linkType == ConfigLink.LinkType.Symbolic || linkType == ConfigLink.LinkType.Hard; |
| | 83 | | } |
| | 84 | |
|
| 3 | 85 | | if (fileSystem.Directory.Exists(resolvedSourcePath)) { |
| 1 | 86 | | return linkType == ConfigLink.LinkType.Symbolic || linkType == ConfigLink.LinkType.Junction; |
| | 87 | | } |
| | 88 | |
|
| 1 | 89 | | return false; |
| 4 | 90 | | } |
| | 91 | |
|
| 4 | 92 | | internal bool ValidateDuplicate (IPathResolver pathResolver, IConfig config, string resolvedTargetPath) { |
| 4 | 93 | | return config.LinkList |
| 10 | 94 | | .Where(link => pathResolver.GetAbsoluteResolvedPath(link.targetPath, config.Variables) |
| 10 | 95 | | .Equals(resolvedTargetPath, StringComparison.OrdinalIgnoreCase)) |
| 4 | 96 | | .Count() <= 1; |
| 4 | 97 | | } |
| | 98 | |
|
| | 99 | | } |
| | 100 | |
|
| | 101 | | } |