package cmd import ( "encoding/json" "fmt" "io" "net/http" "go.jolheiser.com/gpm/config" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" "go.jolheiser.com/beaver" ) var Search = cli.Command{ Name: "search", Aliases: []string{"s"}, Usage: "Search packages", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "local", Usage: "Search locally", }, }, Action: doSearch, } func doSearch(ctx *cli.Context) error { cfg, err := config.Load() if err != nil { return err } packageMap := cfg.Packages.Map() packageSlice := cfg.Packages.Slice() if !ctx.Bool("local") { export, err := queryExport(ctx.String("url")) if err != nil { return err } packageMap = export.Map() packageSlice = export.Slice() } q := &survey.MultiSelect{ Message: "Select packages", Options: packageSlice, } var a []string if err := survey.AskOne(q, &a); err != nil { return err } for _, name := range a { pkg, ok := packageMap[name] if !ok { beaver.Errorf("could not find package for `%s`", name) continue } beaver.Infof("getting `%s`...", name) if err := goGet(pkg.Import); err != nil { beaver.Error(err) } } return nil } func queryExport(server string) (config.Packages, error) { resp, err := http.Get(fmt.Sprintf("%s/export", server)) if err != nil { return nil, err } data, err := io.ReadAll(resp.Body) if err != nil { return nil, err } defer resp.Body.Close() var importPkgs config.Packages if err := json.Unmarshal(data, &importPkgs); err != nil { return nil, err } return importPkgs, nil }