asdf and Nushell: Functions to Install and Switch Versions

I use asdf to manage tool versions, fzf to save typing, and shell functions to bolster my memory. My recent switch to Nushell reminded me that I haven’t memorized the asdf command to install or switch tool versions. I could have created flash cards and learned the commands, but instead I wrote Nushell functions.

Here’s the Nushell function to install a new version of a tool for which the plugin is already installed. You can specify the plugin on the command line or select it via fzf:

# asdf install
def ai [name: string = ''] {
  mut plugin = $name
  if $name == '' {
    $plugin = (
      asdf plugin list |
      get name |
      str join (char nl) |
      fzf --reverse
    )
  }

  if $plugin != '' {
    let version = (
      asdf list all $plugin |
      get version |
      append "latest" |
      str join (char nl) |
      fzf
    )
    if $version != '' {
      asdf install $plugin $version
    }
  }
}

Here’s a function to switch tool versions. I think of it as asdf use <tool-version>, although it calls asdf global. Maybe that’s why I can’t memorize the actual command.

# asdf use
def au [name: string = ''] {
  mut plugin = $name
  if $name == '' {
    $plugin = (
      asdf plugin list |
      get name |
      str join (char nl) |
      fzf --reverse
    )
  }

  if $plugin != '' {
    let version = (
      asdf list $plugin |
      split row "\n" |
      str trim |
      where ($it !~ '\*') |
      where ($it != '') |
      append "latest" |
      str join (char nl) |
      fzf
    )
    if $version != '' {
      asdf global $plugin $version
    }
  }
}

I’ve tested these with Nushell 0.78.1, compiled from source. YMMV. I should probably refactor the code to get the plugin name into its own function. If I can remember how to refactor….

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.