2024-01-04 09:49:03 -07:00
|
|
|
package packagemanager
|
|
|
|
|
|
|
|
// This package is responsible for handling updating and uninstalling pacman packages
|
|
|
|
|
|
|
|
import (
|
Major update 4
This should be the final code update before we start testing things.
I think all the code is now in place to have the program function, barring any bugs in my code.
So, with that said, here's the changelog:
global changes:
```
- implement logger.go across the whole program
```
changes to main.go:
```
- add an import for strings
- implement the roughed in handling functions
```
changes to logger.go:
```
- create this little helper package to just handle all our logging nice and gracefully
```
changes to all_updater.go:
```
- basically completely redone. Accomplishes the same thing, just in a different, more efficient way.
```
changes to aur.go:
```
- add a function to clear the AllPac build cache for aur
```
changes to install.go:
```
- removed a duplicate function, set install.go to call the right one
```
changes to pacman.go:
```
- removed GetVersionFromPacman function (it shouldn't be here, it should be in search.go)
```
changes to search.go:
```
- add functions for getting info from, and parsing output from Snap, Pacman, Flatpak, and aur
```
2024-01-04 19:17:43 -07:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2024-01-05 12:53:27 -07:00
|
|
|
"strings"
|
Major update 4
This should be the final code update before we start testing things.
I think all the code is now in place to have the program function, barring any bugs in my code.
So, with that said, here's the changelog:
global changes:
```
- implement logger.go across the whole program
```
changes to main.go:
```
- add an import for strings
- implement the roughed in handling functions
```
changes to logger.go:
```
- create this little helper package to just handle all our logging nice and gracefully
```
changes to all_updater.go:
```
- basically completely redone. Accomplishes the same thing, just in a different, more efficient way.
```
changes to aur.go:
```
- add a function to clear the AllPac build cache for aur
```
changes to install.go:
```
- removed a duplicate function, set install.go to call the right one
```
changes to pacman.go:
```
- removed GetVersionFromPacman function (it shouldn't be here, it should be in search.go)
```
changes to search.go:
```
- add functions for getting info from, and parsing output from Snap, Pacman, Flatpak, and aur
```
2024-01-04 19:17:43 -07:00
|
|
|
"pixelridgesoftworks.com/AllPac/pkg/logger"
|
2024-01-04 09:49:03 -07:00
|
|
|
)
|
|
|
|
|
2024-01-05 12:53:27 -07:00
|
|
|
// updates specified Pacman packages or all if no specific package is provided
|
2024-01-04 09:49:03 -07:00
|
|
|
func UpdatePacmanPackages(packageNames ...string) error {
|
2024-01-05 12:53:27 -07:00
|
|
|
// If no specific packages are provided, update all packages
|
2024-01-04 09:49:03 -07:00
|
|
|
if len(packageNames) == 0 {
|
2024-01-05 12:53:27 -07:00
|
|
|
logger.Info("No specific package names provided, updating all Pacman packages")
|
|
|
|
cmd := exec.Command("sudo", "pacman", "-Syu", "--noconfirm")
|
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
logger.Errorf("error updating all Pacman packages: %s, %v", string(output), err)
|
|
|
|
return fmt.Errorf("error updating all Pacman packages: %s, %v", string(output), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Read the current package list
|
|
|
|
pkgList, err := ReadPackageList()
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error reading package list: %v", err)
|
|
|
|
return fmt.Errorf("error reading package list: %v", err)
|
2024-01-04 09:49:03 -07:00
|
|
|
}
|
|
|
|
|
2024-01-05 12:53:27 -07:00
|
|
|
var packagesToUpdate []string
|
|
|
|
for _, packageName := range packageNames {
|
|
|
|
installedInfo, ok := pkgList[packageName]
|
|
|
|
if !ok {
|
|
|
|
logger.Infof("Package %s not managed by AllPac, skipping", packageName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
latestVersion, err := GetPacmanLatestVersion(packageName)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error getting latest version for Pacman package %s: %v", packageName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if installedInfo.Version != latestVersion {
|
|
|
|
packagesToUpdate = append(packagesToUpdate, packageName)
|
|
|
|
}
|
2024-01-04 09:49:03 -07:00
|
|
|
}
|
2024-01-05 12:53:27 -07:00
|
|
|
|
|
|
|
if len(packagesToUpdate) > 0 {
|
2024-01-08 14:30:51 -07:00
|
|
|
args := append([]string{"sudo", "pacman", "-Syu", "--noconfirm"}, packagesToUpdate...)
|
2024-01-05 12:53:27 -07:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
logger.Errorf("error updating Pacman packages: %s, %v", string(output), err)
|
|
|
|
return fmt.Errorf("error updating Pacman packages: %s, %v", string(output), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the package list with the new versions
|
|
|
|
for _, packageName := range packagesToUpdate {
|
|
|
|
newVersion, err := GetPacmanLatestVersion(packageName)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error getting new version for Pacman package %s after update: %v", packageName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := UpdatePackageInList(packageName, "pacman", newVersion); err != nil {
|
|
|
|
logger.Errorf("error updating package list for %s: %v", packageName, err)
|
|
|
|
return fmt.Errorf("error updating package list for %s: %v", packageName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Info("No Pacman packages need updating")
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:49:03 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:53:27 -07:00
|
|
|
// uninstalls a specified Pacman package
|
2024-01-04 09:49:03 -07:00
|
|
|
func UninstallPacmanPackage(packageName string) error {
|
2024-01-05 12:53:27 -07:00
|
|
|
// Read the current package list
|
|
|
|
pkgList, err := ReadPackageList()
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("An error has occurred while reading the package list: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the package is managed by AllPac
|
|
|
|
if _, exists := pkgList[packageName]; !exists {
|
|
|
|
logger.Infof("Package %s not found in the package list, may not be managed by AllPac", packageName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninstalling the Pacman package
|
2024-01-04 09:49:03 -07:00
|
|
|
cmd := exec.Command("sudo", "pacman", "-Rns", "--noconfirm", packageName)
|
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
2024-01-05 12:53:27 -07:00
|
|
|
logger.Errorf("error uninstalling Pacman package: %s, %v", output, err)
|
2024-01-04 09:49:03 -07:00
|
|
|
return fmt.Errorf("error uninstalling Pacman package: %s, %v", output, err)
|
|
|
|
}
|
2024-01-05 12:53:27 -07:00
|
|
|
|
|
|
|
// Remove the package from the list after successful uninstallation
|
|
|
|
if err := RemovePackageFromList(packageName); err != nil {
|
|
|
|
logger.Errorf("An error has occurred while removing the package from the list: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("Package %s successfully uninstalled and removed from the package list", packageName)
|
2024-01-04 09:49:03 -07:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-05 12:53:27 -07:00
|
|
|
|
|
|
|
// retrieves the latest available version of a package from Pacman
|
|
|
|
func GetPacmanLatestVersion(packageName string) (string, error) {
|
|
|
|
cmd := exec.Command("pacman", "-Si", packageName)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error getting package info from Pacman: %v", err)
|
|
|
|
}
|
|
|
|
// Parse the output to find the version
|
|
|
|
versionLine := strings.Split(string(output), "\n")[2]
|
|
|
|
version := strings.Fields(versionLine)[2]
|
|
|
|
return version, nil
|
|
|
|
}
|