50af1b9613
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 ```
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package packagemanager
|
|
|
|
// This package is responsible for handling updating and uninstalling snapd applications
|
|
|
|
import (
|
|
"os/exec"
|
|
"fmt"
|
|
"strings"
|
|
"pixelridgesoftworks.com/AllPac/pkg/logger"
|
|
)
|
|
|
|
// UpdateSnapPackages updates specified Snap packages or all if no specific package is provided
|
|
func UpdateSnapPackages(packageNames ...string) error {
|
|
var cmd *exec.Cmd
|
|
if len(packageNames) == 0 {
|
|
cmd = exec.Command("sudo", "snap", "refresh")
|
|
} else {
|
|
args := append([]string{"refresh"}, packageNames...)
|
|
cmd = exec.Command(args[0], args[1:]...)
|
|
}
|
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
logger.Errorf("error updating Snap packages: %s, %v", string(output), err)
|
|
return fmt.Errorf("error updating Snap packages: %s, %v", string(output), err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UninstallSnapPackage uninstalls a specified Snap package
|
|
func UninstallSnapPackage(packageName string) error {
|
|
cmd := exec.Command("sudo", "snap", "remove", packageName)
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
logger.Errorf("error uninstalling Snap package: %s, %v", string(output), err)
|
|
return fmt.Errorf("error uninstalling Snap package: %s, %v", string(output), err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetVersionFromSnap gets the installed version of a Snap package
|
|
func GetVersionFromSnap(packageName string) (string, error) {
|
|
cmd := exec.Command("snap", "info", packageName)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
logger.Errorf("error getting snap package info: %v", err)
|
|
return "", fmt.Errorf("error getting snap package info: %v", err)
|
|
}
|
|
|
|
lines := strings.Split(string(output), "\n")
|
|
for _, line := range lines {
|
|
if strings.HasPrefix(line, "installed:") {
|
|
parts := strings.Fields(line)
|
|
if len(parts) >= 2 {
|
|
return parts[1], nil
|
|
}
|
|
break
|
|
}
|
|
}
|
|
logger.Errorf("version not found for snap package: %s", packageName)
|
|
return "", fmt.Errorf("version not found for snap package: %s", packageName)
|
|
}
|