Skip to content

Commit

Permalink
various CLI improvements (#1190)
Browse files Browse the repository at this point in the history
* various CLI improvements

* various CLI improvements

* e2e test fixes
  • Loading branch information
jdx authored Dec 15, 2023
1 parent 474534a commit 151218a
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 88 deletions.
2 changes: 0 additions & 2 deletions .config/insta.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
test:
auto_review: true
review:
warn_undiscovered: true
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ v20.0.0
- [Commands](#commands)
- [`rtx activate [OPTIONS] [SHELL_TYPE]`](#rtx-activate-options-shell_type)
- [`rtx alias get <PLUGIN> <ALIAS>`](#rtx-alias-get-plugin-alias)
- [`rtx alias ls [PLUGIN]`](#rtx-alias-ls-plugin)
- [`rtx alias ls [OPTIONS] [PLUGIN]`](#rtx-alias-ls-options-plugin)
- [`rtx alias set <PLUGIN> <ALIAS> <VALUE>`](#rtx-alias-set-plugin-alias-value)
- [`rtx alias unset <PLUGIN> <ALIAS>`](#rtx-alias-unset-plugin-alias)
- [`rtx bin-paths`](#rtx-bin-paths)
Expand Down Expand Up @@ -1702,7 +1702,7 @@ Examples:
20.0.0
```

### `rtx alias ls [PLUGIN]`
### `rtx alias ls [OPTIONS] [PLUGIN]`

```text
List aliases
Expand All @@ -1714,12 +1714,16 @@ For user config, aliases are defined like the following in `~/.config/rtx/config
[alias.node]
lts = "20.0.0"
Usage: alias ls [PLUGIN]
Usage: alias ls [OPTIONS] [PLUGIN]
Arguments:
[PLUGIN]
Show aliases for <PLUGIN>
Options:
--no-header
Don't show table header
Examples:
$ rtx aliases
node lts-hydrogen 20.0.0
Expand Down
24 changes: 13 additions & 11 deletions completions/_rtx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _rtx() {
(implode) __rtx_implode_cmd && ret=0 ;;
(i|install) __rtx_install_cmd && ret=0 ;;
(latest) __rtx_latest_cmd && ret=0 ;;
(link) __rtx_link_cmd && ret=0 ;;
(ln|link) __rtx_link_cmd && ret=0 ;;
(l|local) __rtx_local_cmd && ret=0 ;;
(list|ls) __rtx_ls_cmd && ret=0 ;;
(list-all|list-remote|ls-remote) __rtx_ls_remote_cmd && ret=0 ;;
Expand All @@ -44,7 +44,7 @@ _rtx() {
(reshim) __rtx_reshim_cmd && ret=0 ;;
(self-update) __rtx_self_update_cmd && ret=0 ;;
(settings) __rtx_settings_cmd && ret=0 ;;
(s|shell) __rtx_shell_cmd && ret=0 ;;
(sh|shell) __rtx_shell_cmd && ret=0 ;;
(sync) __rtx_sync_cmd && ret=0 ;;
(trust) __rtx_trust_cmd && ret=0 ;;
(remove|rm|uninstall) __rtx_uninstall_cmd && ret=0 ;;
Expand Down Expand Up @@ -72,6 +72,7 @@ __rtx_activate_cmd() {
__rtx_alias_cmd() {
_arguments -s -S \
'(-p --plugin)'{-p,--plugin}'=[filter aliases by plugin]:plugin:__rtx_plugins' \
'--no-header[Don'\''t show table header]' \
'(-q --quiet)'{-q,--quiet}'[Suppress output]' \
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all prompts]' \
Expand Down Expand Up @@ -105,6 +106,7 @@ __rtx_alias_get_cmd() {
__rtx_alias_ls_cmd() {
_arguments -s -S \
'::plugin:__rtx_plugins' \
'--no-header[Don'\''t show table header]' \
'(-q --quiet)'{-q,--quiet}'[Suppress output]' \
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all prompts]'
Expand Down Expand Up @@ -440,7 +442,7 @@ __rtx_plugins_cmd() {
curcontext="${curcontext%:*:*}:rtx-cmd-$words[1]:"
case $words[1] in
(a|add|i|install) __rtx_plugins_install_cmd && ret=0 ;;
(l|link) __rtx_plugins_link_cmd && ret=0 ;;
(ln|link) __rtx_plugins_link_cmd && ret=0 ;;
(list|ls) __rtx_plugins_ls_cmd && ret=0 ;;
(list-all|list-remote|ls-remote) __rtx_plugins_ls_remote_cmd && ret=0 ;;
(remove|rm|uninstall) __rtx_plugins_uninstall_cmd && ret=0 ;;
Expand Down Expand Up @@ -734,7 +736,7 @@ __rtx_cmds() {
'implode:Removes rtx CLI and all related data'
{i,install}':Install a tool version'
'latest:Gets the latest available version for a plugin'
'link:Symlinks a tool version into rtx'
{ln,link}':Symlinks a tool version into rtx'
{list,ls}':List installed and/or currently selected tool versions'
'ls-remote:List runtime versions available for install'
'outdated:Shows outdated tool versions'
Expand All @@ -743,10 +745,10 @@ __rtx_cmds() {
'reshim:rebuilds the shim farm'
'self-update:Updates rtx itself'
'settings:Manage settings'
'shell:Sets a tool version for the current shell session'
{sh,shell}':Sets a tool version for the current shell session'
'sync:Add tool versions from external tools to rtx'
'trust:Marks a config file as trusted'
'uninstall:Removes runtime versions'
{remove,rm,uninstall}':Removes runtime versions'
'upgrade:Upgrades outdated tool versions'
{u,use}':Change the active version of a tool locally or globally.'
'version:Show rtx version'
Expand Down Expand Up @@ -790,12 +792,12 @@ __rtx_direnv_cmds() {
(( $+functions[__rtx_plugins_cmds] )) ||
__rtx_plugins_cmds() {
local commands; commands=(
{a,i,install}':Install a plugin'
'link:Symlinks a plugin into rtx'
{a,add,i,install}':Install a plugin'
{ln,link}':Symlinks a plugin into rtx'
{list,ls}':List installed plugins'
{list-remote,ls-remote}':List all available remote plugins'
'uninstall:Removes a plugin'
'update:Updates a plugin to the latest version'
{list-all,list-remote,ls-remote}':List all available remote plugins'
{remove,rm,uninstall}':Removes a plugin'
{upgrade,update}':Updates a plugin to the latest version'
)
_describe -t commands 'command' commands "$@"
}
Expand Down
34 changes: 32 additions & 2 deletions completions/rtx.bash
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ _rtx() {
rtx,list)
cmd="rtx__ls"
;;
rtx,ln)
cmd="rtx__link"
;;
rtx,local)
cmd="rtx__local"
;;
Expand All @@ -111,6 +114,9 @@ _rtx() {
rtx,prune)
cmd="rtx__prune"
;;
rtx,remove)
cmd="rtx__uninstall"
;;
rtx,render-completion)
cmd="rtx__render__completion"
;;
Expand All @@ -123,12 +129,18 @@ _rtx() {
rtx,reshim)
cmd="rtx__reshim"
;;
rtx,rm)
cmd="rtx__uninstall"
;;
rtx,self-update)
cmd="rtx__self__update"
;;
rtx,settings)
cmd="rtx__settings"
;;
rtx,sh)
cmd="rtx__shell"
;;
rtx,shell)
cmd="rtx__shell"
;;
Expand Down Expand Up @@ -468,6 +480,9 @@ _rtx() {
rtx__plugins,a)
cmd="rtx__plugins__install"
;;
rtx__plugins,add)
cmd="rtx__plugins__install"
;;
rtx__plugins,help)
cmd="rtx__plugins__help"
;;
Expand All @@ -483,21 +498,36 @@ _rtx() {
rtx__plugins,list)
cmd="rtx__plugins__ls"
;;
rtx__plugins,list-all)
cmd="rtx__plugins__ls__remote"
;;
rtx__plugins,list-remote)
cmd="rtx__plugins__ls__remote"
;;
rtx__plugins,ln)
cmd="rtx__plugins__link"
;;
rtx__plugins,ls)
cmd="rtx__plugins__ls"
;;
rtx__plugins,ls-remote)
cmd="rtx__plugins__ls__remote"
;;
rtx__plugins,remove)
cmd="rtx__plugins__uninstall"
;;
rtx__plugins,rm)
cmd="rtx__plugins__uninstall"
;;
rtx__plugins,uninstall)
cmd="rtx__plugins__uninstall"
;;
rtx__plugins,update)
cmd="rtx__plugins__update"
;;
rtx__plugins,upgrade)
cmd="rtx__plugins__update"
;;
rtx__plugins__help,help)
cmd="rtx__plugins__help__help"
;;
Expand Down Expand Up @@ -639,7 +669,7 @@ _rtx() {
return 0
;;
rtx__alias)
opts="-p -q -v -y -h --plugin --debug --log-level --trace --quiet --verbose --yes --help get ls set unset help"
opts="-p -q -v -y -h --plugin --no-header --debug --log-level --trace --quiet --verbose --yes --help get ls set unset help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -767,7 +797,7 @@ _rtx() {
return 0
;;
rtx__alias__ls)
opts="-q -v -y -h --debug --log-level --trace --quiet --verbose --yes --help [PLUGIN]"
opts="-q -v -y -h --no-header --debug --log-level --trace --quiet --verbose --yes --help [PLUGIN]"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down
2 changes: 2 additions & 0 deletions completions/rtx.fish
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ complete -kxc rtx -n "$fssf activate" -a "bash fish nu xonsh zsh" -d 'Shell type
complete -kxc rtx -n "$fssf activate" -l status -d 'Show "rtx: <PLUGIN>@<VERSION>" message when changing directories'

# alias
complete -kxc rtx -n "$fssf alias" -l no-header -d 'Don'\''t show table header'
complete -kxc rtx -n "$fssf alias" -s p -l plugin -a "(__rtx_plugins)" -d 'filter aliases by plugin'
set -l others get ls set unset
complete -xc rtx -n "$fssf alias; and not $fssf $others" -a get -d 'Show an alias for a plugin'
Expand All @@ -57,6 +58,7 @@ complete -kxc rtx -n "$fssf alias; and $fssf get" -a "(__rtx_aliases)" -d 'The a
complete -kxc rtx -n "$fssf alias; and $fssf get" -a "(__rtx_plugins)" -d 'The plugin to show the alias for'
# alias ls
complete -kxc rtx -n "$fssf alias; and $fssf ls" -l no-header -d 'Don'\''t show table header'
complete -kxc rtx -n "$fssf alias; and $fssf ls" -a "(__rtx_plugins)" -d 'Show aliases for <PLUGIN>'
# alias set
Expand Down
1 change: 1 addition & 0 deletions e2e/test_go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rtx use golang@prefix:1.20
assert_contains "rtx x -- go version" "go version go1.20"

rm "$RTX_GO_DEFAULT_PACKAGES_FILE"
chmod -R u+w "$RTX_DATA_DIR/installs/go"
2 changes: 1 addition & 1 deletion e2e/test_link
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euo pipefail
source "$(dirname "$0")/assert.sh"

rtx plugins uninstall tiny
rtx p l "$ROOT/test/data/plugins/tiny"
rtx p ln "$ROOT/test/data/plugins/tiny"
assert_contains "rtx p" "tiny"
assert_fail "rtx p link $ROOT/test/data/plugins/tiny"
rtx plugins link -f "$ROOT/test/data/plugins/tiny"
3 changes: 2 additions & 1 deletion e2e/test_nodejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export RTX_NODE_COREPACK=1
export RTX_NODE_DEFAULT_PACKAGES_FILE="$ROOT/e2e/.default-npm-packages"

rtx plugin uninstall node
rtx i node node@lts/hydrogen
rtx i node@lts/hydrogen
rtx i -f node
assert_contains "rtx x node@lts/hydrogen -- node --version" "v18."
assert "rtx x -- node --version" "v20.0.0"
assert_contains "rtx x -- which yarn" "yarn"
Expand Down
70 changes: 47 additions & 23 deletions src/cli/alias/ls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use color_eyre::eyre::Result;
use tabled::Tabled;

use crate::config::Config;

use crate::plugins::PluginName;
use crate::ui::table;

/// List aliases
/// Shows the aliases that can be specified.
Expand All @@ -18,34 +19,44 @@ pub struct AliasLs {
/// Show aliases for <PLUGIN>
#[clap()]
pub plugin: Option<PluginName>,

/// Don't show table header
#[clap(long)]
pub no_header: bool,
}

impl AliasLs {
pub fn run(self) -> Result<()> {
let config = Config::get();
for (plugin_name, aliases) in config.get_all_aliases() {
if let Some(plugin) = &self.plugin {
if plugin_name != plugin {
continue;
}
}

for (from, to) in aliases.iter() {
if plugin_name == "node" && from.starts_with("lts/") {
// hide the nvm-style aliases so only asdf-style ones display
continue;
}
if self.plugin.is_some() {
rtxprintln!("{:20} {}", from, to);
} else {
rtxprintln!("{:20} {:20} {}", plugin_name, from, to);
}
}
}
let rows = config
.get_all_aliases()
.iter()
.flat_map(|(plugin, aliases)| {
aliases
.iter()
.filter(|(from, _to)| plugin != "node" || !from.starts_with("lts/"))
.map(|(from, to)| Row {
plugin: plugin.clone(),
alias: from.clone(),
version: to.clone(),
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let mut table = tabled::Table::new(rows);
table::default_style(&mut table, self.no_header);
rtxprintln!("{table}");
Ok(())
}
}

#[derive(Tabled)]
struct Row {
plugin: String,
alias: String,
version: String,
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>rtx aliases</bold>
Expand All @@ -55,10 +66,23 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(

#[cfg(test)]
mod tests {

#[test]
fn test_alias_ls() {
let stdout = assert_cli!("aliases");
assert!(stdout.contains("my/alias"));
assert_cli_snapshot!("aliases", @r###"
java lts 21
node lts 20
node lts-argon 4
node lts-boron 6
node lts-carbon 8
node lts-dubnium 10
node lts-erbium 12
node lts-fermium 14
node lts-gallium 16
node lts-hydrogen 18
node lts-iron 20
tiny lts 3.1.0
tiny lts-prev 2.0.0
tiny my/alias 3.0
"###);
}
}
5 changes: 5 additions & 0 deletions src/cli/alias/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub struct Alias {
/// filter aliases by plugin
#[clap(short, long)]
pub plugin: Option<PluginName>,

/// Don't show table header
#[clap(long)]
pub no_header: bool,
}

#[derive(Debug, Subcommand)]
Expand All @@ -42,6 +46,7 @@ impl Alias {
pub fn run(self) -> Result<()> {
let cmd = self.command.unwrap_or(Commands::Ls(ls::AliasLs {
plugin: self.plugin,
no_header: self.no_header,
}));

cmd.run()
Expand Down
Loading

0 comments on commit 151218a

Please sign in to comment.