gpm/internal/cli/get.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

package cli
import (
"context"
"os"
"os/exec"
"strings"
"go.jolheiser.com/gpm"
"github.com/AlecAivazis/survey/v2"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var Get = cli.Command{
Name: "get",
Aliases: []string{"g"},
Usage: "Get package(s)",
Action: doGet,
}
func doGet(ctx *cli.Context) error {
pkgs := ctx.Args().Slice()
if len(pkgs) == 0 {
pkgsQuestion := &survey.Multiline{
Message: "Enter packages to get, one for each line",
}
var pkgsAnswer string
if err := survey.AskOne(pkgsQuestion, &pkgsAnswer); err != nil {
return err
}
pkgs = strings.Split(pkgsAnswer, "\n")
}
client := gpm.New(tokenFlag, gpm.WithServer(serverFlag))
for _, p := range pkgs {
pkg, err := client.Get(context.Background(), p)
if err != nil {
log.Err(err).Msg("")
continue
}
log.Info().Msgf("getting %q...", pkg.Import)
if err := goGet(pkg.Import); err != nil {
log.Err(err).Msg("")
}
}
return nil
}
func goGet(url string) error {
cmd := exec.Command("go", "get", url)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}