Switch all LURE operations to use new DB
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2022-11-30 19:10:17 -08:00
parent 3663a8ef8f
commit a88adb43fe
7 changed files with 125 additions and 127 deletions

View File

@ -44,6 +44,7 @@ import (
"go.arsenm.dev/lure/download"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/cpu"
"go.arsenm.dev/lure/internal/repos"
"go.arsenm.dev/lure/internal/shutils"
"go.arsenm.dev/lure/internal/shutils/decoder"
"go.arsenm.dev/lure/manager"
@ -220,15 +221,25 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
}
if len(buildDeps) > 0 {
found, notFound, err := repos.FindPkgs(gdb, buildDeps)
if err != nil {
return nil, nil, err
}
log.Info("Installing build dependencies").Send()
installPkgs(ctx, buildDeps, mgr, false)
installPkgs(ctx, flattenFoundPkgs(found), notFound, mgr)
}
var builtDeps, builtNames, repoDeps []string
if len(vars.Depends) > 0 {
log.Info("Installing dependencies").Send()
scripts, notFound := findPkgs(vars.Depends)
found, notFound, err := repos.FindPkgs(gdb, vars.Depends)
if err != nil {
return nil, nil, err
}
scripts := getScriptPaths(flattenFoundPkgs(found))
for _, script := range scripts {
pkgPaths, pkgNames, err := buildPackage(ctx, script, mgr)
if err != nil {

32
info.go
View File

@ -19,11 +19,13 @@
package main
import (
"go.arsenm.dev/logger/log"
"fmt"
"os"
"go.arsenm.dev/logger/log"
"github.com/urfave/cli/v2"
"go.arsenm.dev/lure/distro"
"go.arsenm.dev/lure/internal/repos"
"gopkg.in/yaml.v3"
)
@ -33,27 +35,23 @@ func infoCmd(c *cli.Context) error {
log.Fatalf("Command info expected at least 1 argument, got %d", args.Len()).Send()
}
info, err := distro.ParseOSRelease(c.Context)
found, _, err := repos.FindPkgs(gdb, args.Slice())
if err != nil {
log.Fatal("Error parsing os-release").Err(err).Send()
log.Fatal("Error finding packages").Err(err).Send()
}
found, err := findPkg(args.First())
if err != nil {
log.Fatal("Error finding package").Err(err).Send()
if len(found) == 0 {
os.Exit(1)
}
// if multiple are matched, only use the first one
script := found[0]
pkgs := flattenFoundPkgs(found)
vars, err := getBuildVars(c.Context, script, info)
if err != nil {
log.Fatal("Error getting build variables").Err(err).Send()
}
err = yaml.NewEncoder(os.Stdout).Encode(vars)
if err != nil {
log.Fatal("Error encoding script variables").Err(err).Send()
for _, pkg := range pkgs {
err = yaml.NewEncoder(os.Stdout).Encode(pkg)
if err != nil {
log.Fatal("Error encoding script variables").Err(err).Send()
}
fmt.Println("---")
}
return nil

View File

@ -20,10 +20,13 @@ package main
import (
"context"
"path/filepath"
"go.arsenm.dev/logger/log"
"github.com/urfave/cli/v2"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/db"
"go.arsenm.dev/lure/internal/repos"
"go.arsenm.dev/lure/manager"
)
@ -39,21 +42,21 @@ func installCmd(c *cli.Context) error {
log.Fatal("Unable to detect supported package manager on system").Send()
}
installPkgs(c.Context, args.Slice(), mgr, true)
err := repos.Pull(c.Context, gdb, cfg.Repos)
if err != nil {
log.Fatal("Error pulling repositories").Err(err).Send()
}
found, notFound, err := repos.FindPkgs(gdb, args.Slice())
if err != nil {
log.Fatal("Error finding packages").Err(err).Send()
}
installPkgs(c.Context, flattenFoundPkgs(found), notFound, mgr)
return nil
}
func installPkgs(ctx context.Context, pkgs []string, mgr manager.Manager, pull bool) {
if pull {
err := repos.Pull(ctx, gdb, cfg.Repos)
if err != nil {
log.Fatal("Error pulling repositories").Err(err).Send()
}
}
scripts, notFound := findPkgs(pkgs)
func installPkgs(ctx context.Context, pkgs []db.Package, notFound []string, mgr manager.Manager) {
if len(notFound) > 0 {
err := mgr.Install(nil, notFound...)
if err != nil {
@ -61,7 +64,32 @@ func installPkgs(ctx context.Context, pkgs []string, mgr manager.Manager, pull b
}
}
installScripts(ctx, mgr, scripts)
installScripts(ctx, mgr, getScriptPaths(pkgs))
}
func getScriptPaths(pkgs []db.Package) []string {
var scripts []string
for _, pkg := range pkgs {
scriptPath := filepath.Join(config.RepoDir, pkg.Repository, pkg.Name, "lure.sh")
scripts = append(scripts, scriptPath)
}
return scripts
}
func flattenFoundPkgs(found map[string][]db.Package) []db.Package {
var outPkgs []db.Package
for _, pkgs := range found {
if len(pkgs) > 1 {
choices, err := pkgPrompt(pkgs)
if err != nil {
log.Fatal("Error prompting for choice of package").Send()
}
outPkgs = append(outPkgs, choices...)
} else if len(pkgs) == 1 {
outPkgs = append(outPkgs, pkgs[0])
}
}
return outPkgs
}
func installScripts(ctx context.Context, mgr manager.Manager, scripts []string) {

View File

@ -7,13 +7,14 @@ import (
"go.arsenm.dev/lure/internal/db"
)
func FindPkgs(gdb *genji.DB, pkgs []string) (map[string][]db.Package, error) {
func FindPkgs(gdb *genji.DB, pkgs []string) (map[string][]db.Package, []string, error) {
found := map[string][]db.Package{}
notFound := []string(nil)
for _, pkgName := range pkgs {
result, err := db.GetPkgs(gdb, "name LIKE ?", pkgName)
if err != nil {
return nil, err
return nil, nil, err
}
added := 0
@ -30,13 +31,13 @@ func FindPkgs(gdb *genji.DB, pkgs []string) (map[string][]db.Package, error) {
})
result.Close()
if err != nil {
return nil, err
return nil, nil, err
}
if added == 0 {
result, err := db.GetPkgs(gdb, "? IN provides", pkgName)
if err != nil {
return nil, err
return nil, nil, err
}
err = result.Iterate(func(d types.Document) error {
@ -52,10 +53,14 @@ func FindPkgs(gdb *genji.DB, pkgs []string) (map[string][]db.Package, error) {
})
result.Close()
if err != nil {
return nil, err
return nil, nil, err
}
}
if added == 0 {
notFound = append(notFound, pkgName)
}
}
return found, nil
return found, notFound, nil
}

27
list.go
View File

@ -21,29 +21,32 @@ package main
import (
"fmt"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/types"
"github.com/urfave/cli/v2"
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/distro"
"go.arsenm.dev/lure/internal/db"
)
func listCmd(c *cli.Context) error {
info, err := distro.ParseOSRelease(c.Context)
result, err := db.GetPkgs(gdb, "true")
if err != nil {
log.Fatal("Error parsing os-release").Err(err).Send()
log.Fatal("Error getting packages").Err(err).Send()
}
defer result.Close()
pkgs, err := findPkg("*")
if err != nil {
log.Fatal("Error finding packages").Err(err).Send()
}
for _, script := range pkgs {
vars, err := getBuildVars(c.Context, script, info)
err = result.Iterate(func(d types.Document) error {
var pkg db.Package
err := document.StructScan(d, &pkg)
if err != nil {
log.Fatal("Error getting build variables").Err(err).Send()
return err
}
fmt.Println(vars.Name, vars.Version)
fmt.Printf("%s/%s %s\n", pkg.Repository, pkg.Name, pkg.Version)
return nil
})
if err != nil {
log.Fatal("Error iterating over packages").Err(err).Send()
}
return nil

60
repo.go
View File

@ -27,6 +27,7 @@ import (
"github.com/urfave/cli/v2"
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/db"
"go.arsenm.dev/lure/internal/repos"
"go.arsenm.dev/lure/internal/types"
"golang.org/x/exp/slices"
@ -111,41 +112,10 @@ func refreshCmd(c *cli.Context) error {
return nil
}
func findPkg(pkg string) ([]string, error) {
var out []string
for _, repo := range cfg.Repos {
repoDir := filepath.Join(config.RepoDir, repo.Name)
err := os.MkdirAll(repoDir, 0o755)
if err != nil {
return nil, err
}
glob := filepath.Join(repoDir, pkg, "lure.sh")
matches, err := filepath.Glob(glob)
if err != nil {
return nil, err
}
if len(matches) == 0 {
continue
}
out = append(out, matches...)
}
if len(out) == 0 {
return nil, PkgNotFoundError{pkgName: pkg}
}
return out, nil
}
func pkgPrompt(options []string) ([]string, error) {
func pkgPrompt(options []db.Package) ([]db.Package, error) {
names := make([]string, len(options))
for i, option := range options {
pkgDir := filepath.Dir(option)
repoDir := filepath.Dir(pkgDir)
names[i] = filepath.Base(repoDir) + "/" + filepath.Base(pkgDir)
names[i] = option.Repository + "/" + option.Name + " " + option.Version
}
prompt := &survey.MultiSelect{
@ -159,32 +129,10 @@ func pkgPrompt(options []string) ([]string, error) {
return nil, err
}
out := make([]string, len(choices))
out := make([]db.Package, len(choices))
for i, choiceIndex := range choices {
out[i] = options[choiceIndex]
}
return out, nil
}
func findPkgs(pkgs []string) (scripts, notFound []string) {
for _, pkg := range pkgs {
found, err := findPkg(pkg)
if _, ok := err.(PkgNotFoundError); ok {
notFound = append(notFound, pkg)
continue
}
if len(found) == 1 {
scripts = append(scripts, found...)
} else {
choices, err := pkgPrompt(found)
if err != nil {
log.Fatal("Error prompting for package choices").Err(err).Send()
}
scripts = append(scripts, choices...)
}
}
return
}

View File

@ -25,9 +25,12 @@ import (
"github.com/urfave/cli/v2"
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/distro"
"go.arsenm.dev/lure/internal/db"
"go.arsenm.dev/lure/internal/repos"
"go.arsenm.dev/lure/manager"
"go.arsenm.dev/lure/vercmp"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
func upgradeCmd(c *cli.Context) error {
@ -52,7 +55,7 @@ func upgradeCmd(c *cli.Context) error {
}
if len(updates) > 0 {
installPkgs(c.Context, updates, mgr, false)
installPkgs(c.Context, updates, nil, mgr)
} else {
log.Info("There is nothing to do.").Send()
}
@ -60,41 +63,43 @@ func upgradeCmd(c *cli.Context) error {
return nil
}
func checkForUpdates(ctx context.Context, mgr manager.Manager, info *distro.OSRelease) ([]string, error) {
func checkForUpdates(ctx context.Context, mgr manager.Manager, info *distro.OSRelease) ([]db.Package, error) {
installed, err := mgr.ListInstalled(nil)
if err != nil {
return nil, err
}
var out []string
for name, version := range installed {
scripts, err := findPkg(name)
if err != nil {
continue
pkgNames := maps.Keys(installed)
found, _, err := repos.FindPkgs(gdb, pkgNames)
if err != nil {
return nil, err
}
var out []db.Package
for pkgName, pkgs := range found {
if len(pkgs) > 1 {
// Puts the element with the highest version first
slices.SortFunc(pkgs, func(a, b db.Package) bool {
return vercmp.Compare(a.Version, b.Version) == 1
})
}
// since we're not using a glob, we can assume a single item
script := scripts[0]
// First element is the package we want to install
pkg := pkgs[0]
vars, err := getBuildVars(ctx, script, info)
if err != nil {
log.Fatal("Error getting build variables").Err(err).Send()
repoVer := pkg.Version
if pkg.Release != 0 && pkg.Epoch == 0 {
repoVer = fmt.Sprintf("%s-%d", pkg.Version, pkg.Release)
} else if pkg.Release != 0 && pkg.Epoch != 0 {
repoVer = fmt.Sprintf("%d:%s-%d", pkg.Epoch, pkg.Version, pkg.Release)
}
repoVer := vars.Version
if vars.Release != 0 && vars.Epoch == 0 {
repoVer = fmt.Sprintf("%s-%d", vars.Version, vars.Release)
} else if vars.Release != 0 && vars.Epoch != 0 {
repoVer = fmt.Sprintf("%d:%s-%d", vars.Epoch, vars.Version, vars.Release)
}
c := vercmp.Compare(repoVer, version)
c := vercmp.Compare(repoVer, installed[pkgName])
if c == 0 || c == -1 {
continue
} else if c == 1 {
out = append(out, name)
out = append(out, pkg)
}
}
return out, nil
}