protected override int OnExecuteCommand(CliExecutionContext context)
        {
            if (Description == null)
            {
                _console.Write("Description: ");
                Description = _console.ReadLine();
            }

            if (!Local && !Global && !User)
            {
                var result = SelectControl.Show(_console, SelectControl.OneSelectionMode.LeftRight, "Command Scope: ", 1, "User", "Local", "Global");
                User   = result.Index == 0;
                Local  = result.Index == 1;
                Global = result.Index == 2;
            }

            var path      = User ? UserCommandsPath : Local ? LocalCommandsPath : GlobalCommandsPath;
            var scopeName = User ? UserScopeName : Local ? LocalScopeName : GlobalScopeName;
            var commands  = _commandsService.LoadCommands(path);

            if (_commandsService.TryGetCommand(commands, Alias, out var command))
            {
                if (!Force)
                {
                    throw new ApplicationExitException(ExitCode.AliasAddExists, $"The alias \"{Alias}\" already exists for scope \"{scopeName}\". Use the --force parameter to override the existing alias.");
                }
                commands.Remove(command.Alias !);
            }

            while (string.IsNullOrWhiteSpace(Command))
            {
                _console.Write("Command: ");
                Command = _console.ReadLine();
            }

            var tool = Tool?.ToTool();

            commands.Add(Alias !, new Command
            {
                Description = Description,
                Tool        = tool,
                CommandText = Command,
            });

            if (!_commandsService.SaveCommands(path, commands))
            {
                return((int)ExitCode.AliasAddFailedModifyJson);
            }

            var success = true;

            success &= _commandsService.WriteScriptFile(path, Alias !, TerminalTool.PowerShell, Command, Description, tool);
            success &= _commandsService.WriteScriptFile(path, Alias !, TerminalTool.Cmd, Command, Description, tool);

            if (success)
            {
                _console.WriteLineWithColor($"Successfully added alias \"{Alias}\" to scope \"{scopeName}\".", ConsoleColor.Green);
            }
            else
            {
                _console.WriteLineWithColor($"Partially added alias \"{Alias}\" to scope \"{scopeName}\".", ConsoleColor.Yellow);
            }

            return((int)(success ? ExitCode.Okay : ExitCode.AliasAddFailedCreateScript));
        }
Beispiel #2
0
        protected override int OnExecuteCommand(CliExecutionContext context)
        {
            if (!Global && !Local && !User)
            {
                Global = Local = User = true;
            }

            int  removeCount     = 0;
            bool completeSuccess = true;

            if (Global)
            {
                completeSuccess &= RemoveCommand("Global", GlobalCommandsPath);
            }
            if (Local)
            {
                completeSuccess &= RemoveCommand("Local", LocalCommandsPath);
            }
            if (User)
            {
                completeSuccess &= RemoveCommand("User", UserCommandsPath);
            }

            if (removeCount == 0)
            {
                _console.WriteLineWithColor($"The alias \"{Alias}\" does not exist in any scope.", ConsoleColor.Red);
                return((int)ExitCode.AliasRemoveNotExists);
            }

            return((int)(completeSuccess ? ExitCode.Okay : ExitCode.AliasRemoveFailedDeleteScript));

            bool RemoveCommand(string scopeName, string path)
            {
                var commands = _commandsService.LoadCommands(path);

                if (_commandsService.TryGetCommand(commands, Alias, out var command))
                {
                    commands.Remove(command.Alias !);

                    if (!_commandsService.SaveCommands(path, commands))
                    {
                        throw new ApplicationExitException(ExitCode.AliasRemoveFailedModifyJson);
                    }

                    bool success = true;
                    success &= _commandsService.DeleteScriptFile(path, command.Alias !, TerminalTool.PowerShell);
                    success &= _commandsService.DeleteScriptFile(path, command.Alias !, TerminalTool.Cmd);

                    removeCount++;
                    if (success)
                    {
                        _console.WriteLineWithColor($"Successfully removed command \"{command.Alias}\" from scope \"{scopeName}\".", ConsoleColor.Green);
                    }
                    else
                    {
                        _console.WriteLineWithColor($"Partially removed command \"{command.Alias}\" from scope \"{scopeName}\".", ConsoleColor.Yellow);
                    }
                    return(success);
                }

                _console.WriteLineWithColor($"The alias \"{Alias}\" does not exist in the scope \"{scopeName}\".", ConsoleColor.Yellow);
                return(true);
            }
        }