2022-09-26 19:28:21 +00:00
|
|
|
/*
|
|
|
|
* LURE - Linux User REpository
|
|
|
|
* Copyright (C) 2022 Arsen Musayelyan
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
"go.arsenm.dev/lure/distro"
|
|
|
|
"go.arsenm.dev/lure/manager"
|
|
|
|
)
|
|
|
|
|
|
|
|
func upgradeCmd(c *cli.Context) error {
|
|
|
|
info, err := distro.ParseOSRelease(c.Context)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error parsing os-release file").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
mgr := manager.Detect()
|
|
|
|
if mgr == nil {
|
|
|
|
log.Fatal("Unable to detect supported package manager on system").Send()
|
|
|
|
}
|
|
|
|
|
2022-10-03 22:51:46 +00:00
|
|
|
err = pullRepos(c.Context)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error pulling repos").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
2022-09-25 21:00:15 +00:00
|
|
|
updates, err := checkForUpdates(c.Context, mgr, info)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error checking for updates").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(updates) > 0 {
|
2022-10-03 22:51:46 +00:00
|
|
|
installPkgs(c.Context, updates, mgr, false)
|
2022-09-25 21:00:15 +00:00
|
|
|
} else {
|
|
|
|
log.Info("There is nothing to do.").Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkForUpdates(ctx context.Context, mgr manager.Manager, info *distro.OSRelease) ([]string, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
for name, version := range installed {
|
|
|
|
scripts, err := findPkg(name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// since we're not using a glob, we can assume a single item
|
|
|
|
script := scripts[0]
|
|
|
|
|
2022-09-30 23:05:50 +00:00
|
|
|
vars, err := getBuildVars(ctx, script, info)
|
2022-09-25 21:00:15 +00:00
|
|
|
if err != nil {
|
2022-09-30 23:05:50 +00:00
|
|
|
log.Fatal("Error getting build variables").Err(err).Send()
|
2022-09-25 21:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(repoVer, version)
|
|
|
|
if c == 0 || c == -1 {
|
|
|
|
continue
|
|
|
|
} else if c == 1 {
|
|
|
|
out = append(out, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|