2020-02-19 05:18:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-20 19:09:56 +00:00
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
"go.jolheiser.com/gpm/modules/config"
|
2020-02-19 05:18:36 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Get = cli.Command{
|
|
|
|
Name: "get",
|
2020-02-19 16:48:55 +00:00
|
|
|
Usage: "Get package(s)",
|
2020-02-19 05:18:36 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "ignore-local",
|
|
|
|
Usage: "Ignore local packages",
|
|
|
|
},
|
2020-02-19 17:29:42 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "offline",
|
|
|
|
Usage: "Offline mode, return error instead of querying server",
|
|
|
|
},
|
2020-02-19 05:18:36 +00:00
|
|
|
},
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
local := config.PackageMap()
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
var url string
|
2020-02-19 17:29:42 +00:00
|
|
|
if u, ok := local[pkg]; ok && !ctx.Bool("ignore-local") {
|
2020-02-20 05:46:44 +00:00
|
|
|
url = u.Import
|
2020-02-19 17:29:42 +00:00
|
|
|
} else if !ctx.Bool("offline") {
|
2020-02-19 05:18:36 +00:00
|
|
|
u, err := queryServer(ctx.String("url"), pkg)
|
|
|
|
if err != nil {
|
|
|
|
beaver.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
url = u
|
|
|
|
}
|
2020-02-19 05:39:54 +00:00
|
|
|
|
2020-02-19 17:29:42 +00:00
|
|
|
if url == "" {
|
|
|
|
beaver.Errorf("no package found for `%s`", pkg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-19 05:39:54 +00:00
|
|
|
beaver.Infof("getting `%s`...", pkg)
|
2020-02-19 05:18:36 +00:00
|
|
|
if err := goGet(url); err != nil {
|
|
|
|
beaver.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryServer(server, name string) (string, error) {
|
2020-02-20 05:46:44 +00:00
|
|
|
endpoint := fmt.Sprintf("%s/package/%s", server, name)
|
2020-02-19 05:18:36 +00:00
|
|
|
resp, err := http.Get(endpoint)
|
|
|
|
if err != nil {
|
2020-02-19 17:29:42 +00:00
|
|
|
return "", fmt.Errorf("could not query server at `%s`", endpoint)
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("could not find server package for `%s`", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-02-20 05:46:44 +00:00
|
|
|
var pkg config.Package
|
|
|
|
if err := json.Unmarshal(body, &pkg); err != nil {
|
2020-02-19 05:18:36 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-20 05:46:44 +00:00
|
|
|
return pkg.Import, nil
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func goGet(url string) error {
|
|
|
|
cmd := exec.Command("go", "get", url)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
|
|
|
}
|