Exemplo n.º 1
0
        internal void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pathResolver)
        {
            if (!configHandler.DoesConfigExist(path))
            {
                console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a config file.", IConsole.ContentType.Negative);
                return;
            }

            IConfig config = configHandler.LoadConfig(path);

            // Force forward slash
            sourcePath = sourcePath.Replace('\\', '/');
            targetPath = targetPath.Replace('\\', '/');

            string formattedSourcePath = pathResolver.GetAbsoluteResolvedPath(sourcePath, config.Variables);
            string formattedTargetPath = pathResolver.GetAbsoluteResolvedPath(targetPath, config.Variables);

            if (!force && !fileSystem.File.Exists(formattedSourcePath) && !fileSystem.Directory.Exists(formattedSourcePath))
            {
                console.WriteLine($"\nThe sourcePath '{sourcePath}' is invalid because it does not exist", IConsole.ContentType.Negative);
                return;
            }

            if (!force && config.LinkList.Any(link => pathResolver.GetAbsoluteResolvedPath(link.targetPath, config.Variables).Equals(formattedTargetPath)))
            {
                console.WriteLine($"\nThe targetPath '{targetPath}' is invalid because it already exists in config file", IConsole.ContentType.Negative);
                return;
            }

            config.LinkList.Add(new ConfigLink(sourcePath, targetPath, linkType));
            configHandler.SaveConfig(config, path);

            console.WriteLine($"\nAdded new { linkType.ToString() } link to config file: \n" +
                              $"Source: '{ sourcePath }'\n" +
                              $"Target: '{ targetPath }'");
        }