lure/upgrade.go

132 lines
3.3 KiB
Go
Raw Permalink Normal View History

2022-09-26 19:28:21 +00:00
/*
* LURE - Linux User REpository
2023-09-20 22:38:22 +00:00
* Copyright (C) 2023 Elara Musayelyan
2022-09-26 19:28:21 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-09-25 21:00:15 +00:00
package main
import (
"context"
"fmt"
"github.com/urfave/cli/v2"
2023-10-08 00:34:39 +00:00
"lure.sh/lure/internal/config"
"lure.sh/lure/internal/db"
"lure.sh/lure/internal/types"
"lure.sh/lure/pkg/build"
"lure.sh/lure/pkg/distro"
"lure.sh/lure/pkg/loggerctx"
"lure.sh/lure/pkg/manager"
"lure.sh/lure/pkg/repos"
2023-06-22 21:45:05 +00:00
"go.elara.ws/vercmp"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
2022-09-25 21:00:15 +00:00
)
2023-09-19 21:28:05 +00:00
var upgradeCmd = &cli.Command{
Name: "upgrade",
Usage: "Upgrade all installed packages",
Aliases: []string{"up"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "clean",
Aliases: []string{"c"},
Usage: "Build package from scratch even if there's an already built package available",
},
},
Action: func(c *cli.Context) error {
2023-10-06 21:21:12 +00:00
ctx := c.Context
log := loggerctx.From(ctx)
info, err := distro.ParseOSRelease(ctx)
2023-09-19 21:28:05 +00:00
if err != nil {
log.Fatal("Error parsing os-release file").Err(err).Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
mgr := manager.Detect()
if mgr == nil {
log.Fatal("Unable to detect a supported package manager on the system").Send()
}
2022-09-25 21:00:15 +00:00
2023-10-06 21:21:12 +00:00
err = repos.Pull(ctx, config.Config(ctx).Repos)
2023-09-19 21:28:05 +00:00
if err != nil {
log.Fatal("Error pulling repos").Err(err).Send()
}
2023-10-06 21:21:12 +00:00
updates, err := checkForUpdates(ctx, mgr, info)
2023-09-19 21:28:05 +00:00
if err != nil {
log.Fatal("Error checking for updates").Err(err).Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
if len(updates) > 0 {
2023-10-06 21:21:12 +00:00
build.InstallPkgs(ctx, updates, nil, types.BuildOpts{
2023-09-19 21:28:05 +00:00
Manager: mgr,
Clean: c.Bool("clean"),
Interactive: c.Bool("interactive"),
})
} else {
log.Info("There is nothing to do.").Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
return nil
},
2022-09-25 21:00:15 +00:00
}
func checkForUpdates(ctx context.Context, mgr manager.Manager, info *distro.OSRelease) ([]db.Package, error) {
2022-10-01 08:30:13 +00:00
installed, err := mgr.ListInstalled(nil)
2022-09-25 21:00:15 +00:00
if err != nil {
return nil, err
}
pkgNames := maps.Keys(installed)
2023-10-06 21:21:12 +00:00
found, _, err := repos.FindPkgs(ctx, pkgNames)
if err != nil {
return nil, err
}
2022-09-25 21:00:15 +00:00
var out []db.Package
for pkgName, pkgs := range found {
2023-10-06 21:21:12 +00:00
if slices.Contains(config.Config(ctx).IgnorePkgUpdates, pkgName) {
2022-12-19 01:46:40 +00:00
continue
}
if len(pkgs) > 1 {
// Puts the element with the highest version first
2023-09-19 22:40:20 +00:00
slices.SortFunc(pkgs, func(a, b db.Package) int {
return vercmp.Compare(a.Version, b.Version)
})
2022-09-25 21:00:15 +00:00
}
// First element is the package we want to install
pkg := pkgs[0]
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)
2022-09-25 21:00:15 +00:00
}
c := vercmp.Compare(repoVer, installed[pkgName])
2022-09-25 21:00:15 +00:00
if c == 0 || c == -1 {
continue
} else if c == 1 {
out = append(out, pkg)
2022-09-25 21:00:15 +00:00
}
}
return out, nil
}