| | 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("addvar", HelpText = "Adds a new variable to config file")] |
| | 12 | | class AddVariableCommand : GlobalOptions, IDefaultCommandHandler { |
| | 13 | |
|
| | 14 | | [Value(0, HelpText = "The name of the variable", Required = true)] |
| 9 | 15 | | public string name { get; private set; } |
| | 16 | |
|
| | 17 | | [Value(1, HelpText = "The value of the variable", Required = true)] |
| 7 | 18 | | public string value { get; private set; } |
| | 19 | |
|
| | 20 | | [Option('f', "force", Default = false, HelpText = "If this flag is set it will override existing variable if name al |
| 5 | 21 | | public bool force { get; private set; } |
| | 22 | |
|
| 0 | 23 | | public AddVariableCommand() : base() {} |
| | 24 | |
|
| 6 | 25 | | public AddVariableCommand (string name, string value, bool force, string path) : base(path) { |
| 3 | 26 | | this.name = name; |
| 3 | 27 | | this.value = value; |
| 3 | 28 | | this.force = force; |
| 3 | 29 | | } |
| | 30 | |
|
| 3 | 31 | | public void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem) { |
| 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 | |
|
| 3 | 37 | | IConfig config = configHandler.LoadConfig(path); |
| 5 | 38 | | Variable existingVariable = config.Variables.FirstOrDefault(variable => variable.name.Equals(name, StringCompariso |
| | 39 | |
|
| 4 | 40 | | if (existingVariable == null) { |
| 1 | 41 | | config.Variables.Add (new Variable (name, value)); |
| 1 | 42 | | console.WriteLine($"Added new variable '{ name }' with value '{ value }'"); |
| | 43 | |
|
| 1 | 44 | | configHandler.SaveConfig(config, path); |
| 3 | 45 | | } else { |
| 3 | 46 | | if (!force) { |
| 1 | 47 | | console.WriteLine($"A variable with the name '{ name }' already exists", IConsole.ContentType.Negative); |
| 2 | 48 | | } else { |
| 1 | 49 | | existingVariable.value = value; |
| 1 | 50 | | console.WriteLine($"Replaced existing variable value for '{ name }' with '{ value }'"); |
| | 51 | |
|
| 1 | 52 | | configHandler.SaveConfig(config, path); |
| 1 | 53 | | } |
| 2 | 54 | | } |
| 3 | 55 | | } |
| | 56 | |
|
| | 57 | | } |
| | 58 | |
|
| | 59 | | } |