| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO.Abstractions; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Linq; |
| | | 6 | | using CommandLine; |
| | | 7 | | using Mklinker.Abstractions; |
| | | 8 | | |
| | | 9 | | namespace Mklinker.Commands { |
| | | 10 | | |
| | | 11 | | [Verb("removevar", HelpText = "Removes a variable from config file")] |
| | | 12 | | class RemoveVariableCommand : GlobalOptions, IDefaultCommandHandler { |
| | | 13 | | |
| | | 14 | | [Value(0, HelpText = "The name of the variable", Required = true)] |
| | 5 | 15 | | public string name { get; private set; } |
| | | 16 | | |
| | 0 | 17 | | public RemoveVariableCommand() : base() {} |
| | | 18 | | |
| | 4 | 19 | | public RemoveVariableCommand(string name, string path) : base(path) { |
| | 2 | 20 | | this.name = name; |
| | 2 | 21 | | } |
| | | 22 | | |
| | 2 | 23 | | public void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem) { |
| | 0 | 24 | | if (!configHandler.DoesConfigExist(path)) { |
| | 0 | 25 | | console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a config |
| | 0 | 26 | | return; |
| | | 27 | | } |
| | | 28 | | |
| | 2 | 29 | | IConfig config = configHandler.LoadConfig(path); |
| | 3 | 30 | | Variable existingVariable = config.Variables.FirstOrDefault(variable => variable.name.Equals(name, StringCompariso |
| | | 31 | | |
| | 3 | 32 | | if (existingVariable == null) { |
| | 1 | 33 | | console.WriteLine($"A variable with name '{ name }' does not exist", IConsole.ContentType.Negative); |
| | 2 | 34 | | } else { |
| | 1 | 35 | | config.Variables.Remove(existingVariable); |
| | 1 | 36 | | configHandler.SaveConfig(config, path); |
| | | 37 | | |
| | 1 | 38 | | console.WriteLine($"Variable with name '{ name }' has been removed"); |
| | 1 | 39 | | } |
| | 2 | 40 | | } |
| | | 41 | | |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | } |