100 lines
2.0 KiB
Go
100 lines
2.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.jolheiser.com/gpm"
|
|
"go.jolheiser.com/gpm/internal/database"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func New(version string) *cli.App {
|
|
app := cli.NewApp()
|
|
app.Name = "gpm"
|
|
app.Usage = "Go Package Manager"
|
|
app.Version = version
|
|
app.Commands = []*cli.Command{
|
|
&Add,
|
|
&Get,
|
|
&List,
|
|
&Remove,
|
|
&Search,
|
|
&Server,
|
|
&Update,
|
|
}
|
|
app.Flags = []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "server",
|
|
Aliases: []string{"s"},
|
|
Usage: "gpm server to use",
|
|
Value: gpm.DefaultServer,
|
|
EnvVars: []string{"GPM_SERVER"},
|
|
Destination: &serverFlag,
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "token",
|
|
Aliases: []string{"t"},
|
|
Usage: "gpm auth token to use",
|
|
DefaultText: "${GPM_TOKEN}",
|
|
EnvVars: []string{"GPM_TOKEN"},
|
|
Destination: &tokenFlag,
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "database",
|
|
Aliases: []string{"d"},
|
|
Usage: "path to gpm database for server",
|
|
Value: dbPath(),
|
|
DefaultText: "`${USERCONFIG}/gpm.db` or `${BINPATH}/gpm.db`",
|
|
EnvVars: []string{"GPM_DATABASE"},
|
|
Destination: &databaseFlag,
|
|
},
|
|
}
|
|
return app
|
|
}
|
|
|
|
func dbPath() string {
|
|
fn := "gpm.db"
|
|
home, err := os.UserConfigDir()
|
|
if err != nil {
|
|
bin, err := os.Executable()
|
|
if err != nil {
|
|
return fn
|
|
}
|
|
return filepath.Join(filepath.Dir(bin), fn)
|
|
}
|
|
return filepath.Join(home, fn)
|
|
}
|
|
|
|
func localOrToken(_ *cli.Context) error {
|
|
if localFlag && tokenFlag == "" {
|
|
return errors.New("server interaaction requires --token")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func listPackages() ([]gpm.Package, error) {
|
|
var pkgs []gpm.Package
|
|
if localFlag {
|
|
db, err := database.Load(databaseFlag)
|
|
if err != nil {
|
|
return pkgs, err
|
|
}
|
|
pkgs, err = db.Packages()
|
|
if err != nil {
|
|
return pkgs, err
|
|
}
|
|
} else {
|
|
client := gpm.New(tokenFlag, gpm.WithServer(serverFlag))
|
|
info, err := client.Info(context.Background())
|
|
if err != nil {
|
|
return pkgs, err
|
|
}
|
|
pkgs = info.Packages
|
|
}
|
|
return pkgs, nil
|
|
}
|