2024-04-30 07:13:05 -07:00
|
|
|
// Package cmd contains the asdf CLI command code
|
2024-02-16 13:21:19 -07:00
|
|
|
package cmd
|
2024-02-12 18:00:41 -07:00
|
|
|
|
|
|
|
import (
|
2024-06-04 18:03:03 -07:00
|
|
|
"errors"
|
2024-08-17 13:27:18 -07:00
|
|
|
"fmt"
|
2024-02-21 17:52:34 -07:00
|
|
|
"log"
|
2024-02-12 18:00:41 -07:00
|
|
|
"os"
|
2024-08-17 13:27:18 -07:00
|
|
|
"strings"
|
2024-02-12 18:00:41 -07:00
|
|
|
|
2024-04-30 07:13:05 -07:00
|
|
|
"asdf/config"
|
2024-07-05 17:55:29 -07:00
|
|
|
"asdf/internal/info"
|
2024-08-17 13:27:18 -07:00
|
|
|
"asdf/internal/versions"
|
2024-04-30 07:13:05 -07:00
|
|
|
"asdf/plugins"
|
|
|
|
|
2024-02-21 17:52:34 -07:00
|
|
|
"github.com/urfave/cli/v2"
|
2024-02-12 18:00:41 -07:00
|
|
|
)
|
|
|
|
|
2024-02-21 17:52:34 -07:00
|
|
|
const usageText = `The Multiple Runtime Version Manager.
|
2024-02-12 18:00:41 -07:00
|
|
|
|
|
|
|
Manage all your runtime versions with one tool!
|
|
|
|
|
2024-02-21 17:52:34 -07:00
|
|
|
Complete documentation is available at https://asdf-vm.com/`
|
2024-02-12 18:00:41 -07:00
|
|
|
|
2024-04-30 07:13:05 -07:00
|
|
|
// Execute defines the full CLI API and then runs it
|
2024-07-05 17:55:29 -07:00
|
|
|
func Execute(version string) {
|
2024-02-29 18:59:12 -07:00
|
|
|
logger := log.New(os.Stderr, "", 0)
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
2024-02-21 17:52:34 -07:00
|
|
|
app := &cli.App{
|
|
|
|
Name: "asdf",
|
|
|
|
Version: "0.1.0",
|
|
|
|
// Not really sure what I should put here, but all the new Golang code will
|
|
|
|
// likely be written by me.
|
|
|
|
Copyright: "(c) 2024 Trevor Brown",
|
|
|
|
Authors: []*cli.Author{
|
2024-03-20 18:05:03 -07:00
|
|
|
{
|
2024-02-21 17:52:34 -07:00
|
|
|
Name: "Trevor Brown",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Usage: "The multiple runtime version manager",
|
|
|
|
UsageText: usageText,
|
|
|
|
Commands: []*cli.Command{
|
2024-07-05 17:55:29 -07:00
|
|
|
{
|
|
|
|
Name: "info",
|
|
|
|
Action: func(_ *cli.Context) error {
|
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return infoCommand(conf, version)
|
|
|
|
},
|
|
|
|
},
|
2024-08-17 13:27:18 -07:00
|
|
|
{
|
|
|
|
Name: "install",
|
|
|
|
Action: func(cCtx *cli.Context) error {
|
|
|
|
args := cCtx.Args()
|
|
|
|
return installCommand(logger, args.Get(0), args.Get(1))
|
|
|
|
},
|
|
|
|
},
|
2024-08-20 05:15:05 -07:00
|
|
|
{
|
|
|
|
Name: "latest",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Usage: "Show latest version of all tools",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cCtx *cli.Context) error {
|
|
|
|
tool := cCtx.Args().Get(0)
|
|
|
|
pattern := cCtx.Args().Get(1)
|
|
|
|
all := cCtx.Bool("all")
|
|
|
|
|
|
|
|
return latestCommand(logger, all, tool, pattern)
|
|
|
|
},
|
|
|
|
},
|
2024-03-20 18:05:03 -07:00
|
|
|
{
|
2024-02-21 17:52:34 -07:00
|
|
|
Name: "plugin",
|
2024-03-20 18:05:03 -07:00
|
|
|
Action: func(_ *cli.Context) error {
|
2024-07-05 17:55:29 -07:00
|
|
|
logger.Println("Unknown command: `asdf plugin`")
|
|
|
|
os.Exit(1)
|
2024-02-21 17:52:34 -07:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Subcommands: []*cli.Command{
|
2024-03-20 18:05:03 -07:00
|
|
|
{
|
2024-02-21 17:52:34 -07:00
|
|
|
Name: "add",
|
|
|
|
Action: func(cCtx *cli.Context) error {
|
2024-02-29 18:59:12 -07:00
|
|
|
args := cCtx.Args()
|
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
2024-03-01 06:43:52 -07:00
|
|
|
return err
|
2024-02-29 18:59:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return pluginAddCommand(cCtx, conf, logger, args.Get(0), args.Get(1))
|
2024-02-21 17:52:34 -07:00
|
|
|
},
|
|
|
|
},
|
2024-03-20 18:05:03 -07:00
|
|
|
{
|
2024-02-21 17:52:34 -07:00
|
|
|
Name: "list",
|
2024-03-01 06:43:52 -07:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "urls",
|
|
|
|
Usage: "Show URLs",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "refs",
|
|
|
|
Usage: "Show Refs",
|
|
|
|
},
|
|
|
|
},
|
2024-02-21 17:52:34 -07:00
|
|
|
Action: func(cCtx *cli.Context) error {
|
2024-03-01 06:43:52 -07:00
|
|
|
return pluginListCommand(cCtx, logger)
|
2024-02-21 17:52:34 -07:00
|
|
|
},
|
|
|
|
},
|
2024-03-20 18:05:03 -07:00
|
|
|
{
|
2024-03-07 12:15:24 -07:00
|
|
|
Name: "remove",
|
2024-02-21 17:52:34 -07:00
|
|
|
Action: func(cCtx *cli.Context) error {
|
2024-03-07 12:15:24 -07:00
|
|
|
args := cCtx.Args()
|
|
|
|
return pluginRemoveCommand(cCtx, logger, args.Get(0))
|
2024-02-21 17:52:34 -07:00
|
|
|
},
|
|
|
|
},
|
2024-03-20 18:05:03 -07:00
|
|
|
{
|
2024-02-21 17:52:34 -07:00
|
|
|
Name: "update",
|
2024-04-27 17:20:07 -07:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Usage: "Update all installed plugins",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cCtx *cli.Context) error {
|
|
|
|
args := cCtx.Args()
|
|
|
|
return pluginUpdateCommand(cCtx, logger, args.Get(0), args.Get(1))
|
2024-02-21 17:52:34 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-03-20 18:05:03 -07:00
|
|
|
Action: func(_ *cli.Context) error {
|
2024-02-21 17:52:34 -07:00
|
|
|
// TODO: flesh this out
|
|
|
|
log.Print("Late but latest -- Rajinikanth")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-12 18:00:41 -07:00
|
|
|
|
2024-02-21 17:52:34 -07:00
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
2024-02-12 18:00:41 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 18:59:12 -07:00
|
|
|
|
2024-03-20 18:05:03 -07:00
|
|
|
func pluginAddCommand(_ *cli.Context, conf config.Config, logger *log.Logger, pluginName, pluginRepo string) error {
|
2024-02-29 18:59:12 -07:00
|
|
|
if pluginName == "" {
|
|
|
|
// Invalid arguments
|
|
|
|
// Maybe one day switch this to show the generated help
|
|
|
|
// cli.ShowSubcommandHelp(cCtx)
|
|
|
|
return cli.Exit("usage: asdf plugin add <name> [<git-url>]", 1)
|
|
|
|
}
|
2024-03-20 18:05:03 -07:00
|
|
|
|
|
|
|
err := plugins.Add(conf, pluginName, pluginRepo)
|
|
|
|
if err != nil {
|
2024-06-04 18:03:03 -07:00
|
|
|
logger.Printf("%s", err)
|
|
|
|
|
|
|
|
var existsErr plugins.PluginAlreadyExists
|
|
|
|
if errors.As(err, &existsErr) {
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
return nil
|
2024-03-20 18:05:03 -07:00
|
|
|
}
|
|
|
|
|
2024-06-04 18:03:03 -07:00
|
|
|
os.Exit(0)
|
2024-02-29 18:59:12 -07:00
|
|
|
return nil
|
|
|
|
}
|
2024-03-01 06:43:52 -07:00
|
|
|
|
2024-03-20 18:05:03 -07:00
|
|
|
func pluginRemoveCommand(_ *cli.Context, logger *log.Logger, pluginName string) error {
|
2024-03-07 12:15:24 -07:00
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugins.Remove(conf, pluginName)
|
|
|
|
if err != nil {
|
2024-08-22 06:16:28 -07:00
|
|
|
// Needed to match output of old version
|
|
|
|
logger.Printf("%s", err)
|
2024-03-07 12:15:24 -07:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-01 06:43:52 -07:00
|
|
|
func pluginListCommand(cCtx *cli.Context, logger *log.Logger) error {
|
|
|
|
urls := cCtx.Bool("urls")
|
|
|
|
refs := cCtx.Bool("refs")
|
|
|
|
|
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins, err := plugins.List(conf, urls, refs)
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading plugin list: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add some sort of presenter logic in another file so we
|
|
|
|
// don't clutter up this cmd code with conditional presentation
|
|
|
|
// logic
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
if urls && refs {
|
2024-03-20 18:05:03 -07:00
|
|
|
logger.Printf("%s\t\t%s\t%s\n", plugin.Name, plugin.URL, plugin.Ref)
|
2024-03-01 06:43:52 -07:00
|
|
|
} else if refs {
|
|
|
|
logger.Printf("%s\t\t%s\n", plugin.Name, plugin.Ref)
|
|
|
|
} else if urls {
|
2024-03-20 18:05:03 -07:00
|
|
|
logger.Printf("%s\t\t%s\n", plugin.Name, plugin.URL)
|
2024-03-01 06:43:52 -07:00
|
|
|
} else {
|
|
|
|
logger.Printf("%s\n", plugin.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-27 17:20:07 -07:00
|
|
|
|
2024-07-05 17:55:29 -07:00
|
|
|
func infoCommand(conf config.Config, version string) error {
|
|
|
|
return info.Print(conf, version)
|
|
|
|
}
|
|
|
|
|
2024-04-27 17:20:07 -07:00
|
|
|
func pluginUpdateCommand(cCtx *cli.Context, logger *log.Logger, pluginName, ref string) error {
|
|
|
|
updateAll := cCtx.Bool("all")
|
|
|
|
if !updateAll && pluginName == "" {
|
|
|
|
return cli.Exit("usage: asdf plugin-update {<name> [git-ref] | --all}", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateAll {
|
|
|
|
installedPlugins, err := plugins.List(conf, false, false)
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to get plugin list: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, plugin := range installedPlugins {
|
|
|
|
updatedToRef, err := plugins.Update(conf, plugin.Name, "")
|
|
|
|
formatUpdateResult(logger, plugin.Name, updatedToRef, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedToRef, err := plugins.Update(conf, pluginName, ref)
|
|
|
|
formatUpdateResult(logger, pluginName, updatedToRef, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatUpdateResult(logger *log.Logger, pluginName, updatedToRef string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to update %s due to error: %s\n", pluginName, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Printf("updated %s to ref %s\n", pluginName, updatedToRef)
|
|
|
|
}
|
2024-08-17 13:27:18 -07:00
|
|
|
|
|
|
|
func installCommand(logger *log.Logger, toolName, version string) error {
|
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to fetch current directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if toolName == "" {
|
|
|
|
// Install all versions
|
|
|
|
errs := versions.InstallAll(conf, dir, os.Stdout, os.Stderr)
|
|
|
|
if len(errs) > 0 {
|
|
|
|
for _, err := range errs {
|
|
|
|
// write error stderr
|
|
|
|
os.Stderr.Write([]byte(err.Error()))
|
|
|
|
os.Stderr.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs[0]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Install specific version
|
|
|
|
plugin := plugins.New(conf, toolName)
|
|
|
|
|
|
|
|
if version == "" {
|
|
|
|
err = versions.Install(conf, plugin, dir, os.Stdout, os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parsedVersion, query := parseInstallVersion(version)
|
|
|
|
|
|
|
|
if parsedVersion == "latest" {
|
|
|
|
err = versions.InstallVersion(conf, plugin, version, query, os.Stdout, os.Stderr)
|
|
|
|
} else {
|
|
|
|
err = versions.InstallOneVersion(conf, plugin, version, os.Stdout, os.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error installing version: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInstallVersion(version string) (string, string) {
|
|
|
|
segments := strings.Split(version, ":")
|
|
|
|
if len(segments) > 1 && segments[0] == "latest" {
|
|
|
|
// Must be latest with filter
|
|
|
|
return "latest", segments[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return version, ""
|
|
|
|
}
|
2024-08-20 05:15:05 -07:00
|
|
|
|
|
|
|
func latestCommand(logger *log.Logger, all bool, toolName, pattern string) (err error) {
|
|
|
|
conf, err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error loading config: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !all {
|
|
|
|
err = latestForPlugin(conf, toolName, pattern, false)
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins, err := plugins.List(conf, false, false)
|
|
|
|
if err != nil {
|
2024-08-22 06:16:28 -07:00
|
|
|
logger.Printf("error loading plugin list: %s", err)
|
2024-08-20 05:15:05 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var maybeErr error
|
|
|
|
// loop over all plugins and show latest for each one.
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
maybeErr = latestForPlugin(conf, plugin.Name, "", true)
|
|
|
|
if maybeErr != nil {
|
|
|
|
err = maybeErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
return maybeErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func latestForPlugin(conf config.Config, toolName, pattern string, showStatus bool) error {
|
|
|
|
// show single plugin
|
|
|
|
plugin := plugins.New(conf, toolName)
|
|
|
|
latest, err := versions.Latest(plugin, pattern)
|
|
|
|
if err != nil && err.Error() != "no latest version found" {
|
|
|
|
fmt.Printf("unable to load latest version: %s\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if latest == "" {
|
|
|
|
err := fmt.Errorf("No compatible versions available (%s %s)", toolName, pattern)
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if showStatus {
|
|
|
|
installed := versions.Installed(conf, plugin, latest)
|
|
|
|
fmt.Printf("%s\t%s\t%s\n", plugin.Name, latest, installedStatus(installed))
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s\n", latest)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func installedStatus(installed bool) string {
|
|
|
|
if installed {
|
|
|
|
return "installed"
|
|
|
|
}
|
|
|
|
return "missing"
|
|
|
|
}
|