2024-01-04 09:49:03 -07:00
|
|
|
package packagemanager
|
|
|
|
|
|
|
|
// This package is responsible for handling updating and uninstalling flatpak applications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"fmt"
|
2024-01-04 13:08:32 -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
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateFlatpakPackages updates specified Flatpak packages or all if no specific package is provided
|
|
|
|
func UpdateFlatpakPackages(packageNames ...string) error {
|
2024-01-05 12:53:27 -07:00
|
|
|
// 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
|
|
|
// Determine which packages need updating
|
|
|
|
var packagesToUpdate []string
|
|
|
|
for _, packageName := range packageNames {
|
|
|
|
installedInfo, ok := pkgList[packageName]
|
|
|
|
if !ok {
|
|
|
|
logger.Infof("Package %s not managed by AllPac, skipping", packageName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
availableVersion, err := GetFlatpakPackageVersion(packageName)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error getting available version for Flatpak package %s: %v", packageName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if installedInfo.Version != availableVersion {
|
|
|
|
packagesToUpdate = append(packagesToUpdate, packageName)
|
|
|
|
}
|
2024-01-04 09:49:03 -07:00
|
|
|
}
|
2024-01-05 12:53:27 -07:00
|
|
|
|
|
|
|
// Update the packages
|
|
|
|
if len(packagesToUpdate) > 0 {
|
|
|
|
args := append([]string{"update", "-y"}, packagesToUpdate...)
|
|
|
|
cmd := exec.Command("flatpak", args...)
|
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
logger.Errorf("error updating Flatpak packages: %s, %v", output, err)
|
|
|
|
return fmt.Errorf("error updating Flatpak packages: %s, %v", output, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the package list with the new versions
|
|
|
|
for _, packageName := range packagesToUpdate {
|
|
|
|
newVersion, err := GetFlatpakPackageVersion(packageName)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error getting new version for Flatpak package %s after update: %v", packageName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := UpdatePackageInList(packageName, "flatpak", 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 Flatpak packages need updating")
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:49:03 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UninstallFlatpakPackage uninstalls a specified Flatpak package
|
|
|
|
func UninstallFlatpakPackage(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 Flatpak package
|
2024-01-04 09:49:03 -07:00
|
|
|
cmd := exec.Command("flatpak", "uninstall", "-y", packageName)
|
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
2024-01-05 12:53:27 -07:00
|
|
|
logger.Errorf("error uninstalling Flatpak package: %s, %v", output, err)
|
2024-01-04 09:49:03 -07:00
|
|
|
return fmt.Errorf("error uninstalling Flatpak 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-04 13:08:32 -07:00
|
|
|
|
|
|
|
// GetVersionFromFlatpak gets the installed version of a Flatpak package
|
|
|
|
func GetVersionFromFlatpak(applicationID string) (string, error) {
|
|
|
|
cmd := exec.Command("flatpak", "info", applicationID)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
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
|
|
|
logger.Errorf("error getting flatpak package info: %v", err)
|
2024-01-04 13:08:32 -07:00
|
|
|
return "", fmt.Errorf("error getting flatpak package info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(output), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "Version:") {
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if len(parts) >= 2 {
|
|
|
|
return parts[1], nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
logger.Errorf("version not found for flatpak package: %s", applicationID)
|
2024-01-04 13:08:32 -07:00
|
|
|
return "", fmt.Errorf("version not found for flatpak package: %s", applicationID)
|
|
|
|
}
|