| | | 1 | | using System.Linq; |
| | | 2 | | using CommandLine; |
| | | 3 | | using System.IO.Abstractions; |
| | | 4 | | using Mklinker.Abstractions; |
| | | 5 | | |
| | | 6 | | namespace Mklinker.Commands { |
| | | 7 | | |
| | | 8 | | [Verb("addlink", HelpText = "Adds a new link to config file")] |
| | | 9 | | class AddLinkCommand : GlobalOptions { |
| | | 10 | | |
| | | 11 | | [Value(0, HelpText = "The path to the source file", Required = true)] |
| | 872 | 12 | | public string sourcePath { get; private set; } |
| | | 13 | | |
| | | 14 | | [Value(1, HelpText = "The path to new link file", Required = true)] |
| | 872 | 15 | | public string targetPath { get; private set; } |
| | | 16 | | |
| | | 17 | | [Value(2, Default = ConfigLink.LinkType.Default, HelpText = "The type of link you want to create. Default is Symboli |
| | 433 | 18 | | public ConfigLink.LinkType linkType { get; private set; } |
| | | 19 | | |
| | | 20 | | [Option('f', "force", Default = false, HelpText = "If this flag is set it will ignore validation checks and add it n |
| | 0 | 21 | | public bool force { get; private set; } |
| | | 22 | | |
| | 0 | 23 | | public AddLinkCommand() : base() {} |
| | | 24 | | |
| | 290 | 25 | | public AddLinkCommand(string sourcePath, string targetPath, ConfigLink.LinkType linkType, string path) : base(path) |
| | 145 | 26 | | this.targetPath = targetPath; |
| | 145 | 27 | | this.sourcePath = sourcePath; |
| | 145 | 28 | | this.linkType = linkType; |
| | 145 | 29 | | } |
| | | 30 | | |
| | 146 | 31 | | internal void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pathReso |
| | 0 | 32 | | if (!configHandler.DoesConfigExist(path)) { |
| | 0 | 33 | | console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a config |
| | 0 | 34 | | return; |
| | | 35 | | } |
| | | 36 | | |
| | 146 | 37 | | IConfig config = configHandler.LoadConfig(path); |
| | | 38 | | |
| | | 39 | | // Force forward slash |
| | 146 | 40 | | sourcePath = sourcePath.Replace ('\\', '/'); |
| | 146 | 41 | | targetPath = targetPath.Replace ('\\', '/'); |
| | | 42 | | |
| | 146 | 43 | | string formattedSourcePath = pathResolver.GetAbsoluteResolvedPath(sourcePath, config.Variables); |
| | 146 | 44 | | string formattedTargetPath = pathResolver.GetAbsoluteResolvedPath(targetPath, config.Variables); |
| | | 45 | | |
| | 147 | 46 | | if (!force && !fileSystem.File.Exists(formattedSourcePath) && !fileSystem.Directory.Exists(formattedSourcePath)) { |
| | 1 | 47 | | console.WriteLine($"\nThe sourcePath '{sourcePath}' is invalid because it does not exist", IConsole.ContentType. |
| | 1 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 578 | 51 | | if (!force && config.LinkList.Any(link => pathResolver.GetAbsoluteResolvedPath(link.targetPath, config.Variables). |
| | 1 | 52 | | console.WriteLine($"\nThe targetPath '{targetPath}' is invalid because it already exists in config file", IConso |
| | 1 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | 144 | 56 | | config.LinkList.Add(new ConfigLink(sourcePath, targetPath, linkType)); |
| | 144 | 57 | | configHandler.SaveConfig(config, path); |
| | | 58 | | |
| | 144 | 59 | | console.WriteLine($"\nAdded new { linkType.ToString() } link to config file: \n" + |
| | 144 | 60 | | $"Source: '{ sourcePath }'\n" + |
| | 144 | 61 | | $"Target: '{ targetPath }'"); |
| | 146 | 62 | | } |
| | | 63 | | |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | } |