protected override int OnExecuteCommand(CliExecutionContext context) { var paths = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User)?.Split(';').ToList() ?? new List <string>(); if (!Global && !Local && !User) { Global = Local = User = true; } _console.WriteLine("The following aliases are available:"); _console.WriteLine(); if (Global) { PrintList("Global", GlobalCommandsPath); } if (Local) { PrintList("List", LocalCommandsPath); } if (User) { PrintList("User", UserCommandsPath); } return((int)ExitCode.Okay); void PrintList(string scopeName, string path) { bool isInstalled = paths.Contains(path, StringComparer.OrdinalIgnoreCase); _console.Write($"{scopeName} ("); _console.WriteWithColor(isInstalled ? "Installed" : "Not Installed", isInstalled ? ConsoleColor.Green : ConsoleColor.Red); _console.WriteLine(")"); var commands = _commandsService.LoadCommands(path); if (commands.Count > 0) { new TableControl(_console) { Margin = new(3, 0, 0, 0), ShowColumnHeaders = false, Columns = { new() { WidthMode = ColumnWidthMode.Auto }, new() { WidthMode = ColumnWidthMode.Star }, }, Rows = commands.Select(x => new Row { Values = { x.Key, x.Value.Description ?? "<No Description>" } }).ToArray(), }.Render();
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)); }
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); } }