gpm/cmd/import.go

53 lines
990 B
Go

package cmd
import (
"encoding/json"
"errors"
"gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/urfave/cli/v2"
"io/ioutil"
)
var Import = cli.Command{
Name: "import",
Usage: "Import JSON for local packages",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite any existing packages without prompt",
},
},
Action: doImport,
}
func doImport(ctx *cli.Context) error {
files := ctx.Args().Slice()
if len(files) == 0 {
return errors.New("must provide a list of JSON files to import from")
}
for _, file := range files {
body, err := ioutil.ReadFile(file)
if err != nil {
beaver.Error(err)
continue
}
var cfg config.Config
if err := json.Unmarshal(body, &cfg); err != nil {
beaver.Error(err)
continue
}
config.AddPackages(ctx.Bool("force"), cfg.Packages...)
}
if err := config.Save(); err != nil {
return err
}
beaver.Info("Import complete")
return nil
}