2024-02-16 13:21:19 -07:00
|
|
|
package cmd
|
2024-02-12 18:00:41 -07:00
|
|
|
|
|
|
|
import (
|
2024-02-29 18:59:12 -07:00
|
|
|
"asdf/config"
|
|
|
|
"asdf/plugins"
|
2024-02-21 17:52:34 -07:00
|
|
|
"log"
|
2024-02-12 18:00:41 -07:00
|
|
|
"os"
|
|
|
|
|
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-02-21 17:52:34 -07:00
|
|
|
func Execute() {
|
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{
|
|
|
|
// TODO: Flesh out all these commands
|
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-02-21 17:52:34 -07:00
|
|
|
log.Print("Foobar")
|
|
|
|
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-03-20 18:05:03 -07:00
|
|
|
Action: func(_ *cli.Context) error {
|
2024-02-21 17:52:34 -07:00
|
|
|
log.Print("Ipsum")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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)
|
2024-02-12 18:00:41 -07:00
|
|
|
|
2024-02-21 17:52:34 -07:00
|
|
|
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)
|
|
|
|
} else if pluginRepo == "" {
|
|
|
|
// add from plugin repo
|
|
|
|
// TODO: implement
|
|
|
|
return cli.Exit("Not implemented yet", 1)
|
|
|
|
}
|
2024-03-20 18:05:03 -07:00
|
|
|
|
|
|
|
err := plugins.Add(conf, pluginName, pluginRepo)
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("error adding plugin: %s", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
logger.Printf("error removing plugin: %s", err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|