Major Update 2

flatpak.go changes:
```
- remove pkg.list reader, move it to search.go
```

pacman.go changes:
```
- replace broken UpdatePacmanPackages function with a working one
```

snap.go changes:
```
- Write completely snap.go
- Include functions for uninstalling and updating Snap packages
```

search.go changes:
```
- add os/user, os/exec, path/filepath, and strings to imports
- Add functions for reading the pkg.list to uninstall packages with their respective package manager
```
This commit is contained in:
VetheonGames 2024-01-04 11:04:38 -07:00
parent 9ef6713567
commit 5d9a6e428a
4 changed files with 102 additions and 43 deletions

View File

@ -4,46 +4,9 @@ package packagemanager
import (
"os/exec"
"encoding/json"
"io/ioutil"
"fmt"
"path/filepath"
"os/user"
)
// PackageList represents the mapping of installed packages to their sources
type PackageList map[string]string
// getPkgListPath returns the file path for the package list
func getPkgListPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("error getting current user: %v", err)
}
return filepath.Join(usr.HomeDir, ".allpac", "pkg.list"), nil
}
// readPackageList reads the package list from the file
func readPackageList() (PackageList, error) {
pkgListPath, err := getPkgListPath()
if err != nil {
return nil, err
}
file, err := ioutil.ReadFile(pkgListPath)
if err != nil {
return nil, fmt.Errorf("error reading package list file: %v", err)
}
var pkgList PackageList
err = json.Unmarshal(file, &pkgList)
if err != nil {
return nil, fmt.Errorf("error decoding package list: %v", err)
}
return pkgList, nil
}
// UpdateFlatpakPackages updates specified Flatpak packages or all if no specific package is provided
func UpdateFlatpakPackages(packageNames ...string) error {
var cmd *exec.Cmd

View File

@ -13,12 +13,12 @@ func UpdatePacmanPackages(packageNames ...string) error {
if len(packageNames) == 0 {
cmd = exec.Command("sudo", "pacman", "-Syu")
} else {
args := append([]string{"-S", "--noconfirm"}, packageNames...)
cmd = exec.Command("sudo", "pacman", args...)
args := append([]string{"sudo", "pacman", "-S", "--noconfirm"}, packageNames...)
cmd = exec.Command(args[0], args[1:]...)
}
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("error updating Pacman packages: %s, %v", output, err)
return fmt.Errorf("error updating Pacman packages: %s, %v", string(output), err)
}
return nil
}

View File

@ -1,3 +1,33 @@
package packagemanager
// This package is responsible for handling updating and uninstalling snapd applications
import (
"os/exec"
"fmt"
)
// 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 {
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 {
return fmt.Errorf("error uninstalling Snap package: %s, %v", string(output), err)
}
return nil
}

View File

@ -3,13 +3,79 @@ package search
// This package is responsible for searching various sources for the availability of the requested package
import (
"os/exec"
"pixelridgesoftworks.com/AllPac/pkg/packagemanager"
"encoding/json"
"io/ioutil"
"fmt"
"net/http"
"strings"
"os/user"
"os/exec"
"path/filepath"
"strings"
"net/http"
)
// PackageList represents the mapping of installed packages to their sources
type PackageList map[string]string
// UninstallPackages uninstalls the provided packages
func UninstallPackages(packageNames []string) error {
pkgList, err := readPackageList()
if err != nil {
return err
}
for _, packageName := range packageNames {
source, exists := pkgList[packageName]
if !exists {
fmt.Printf("Package %s not found in installed packages list\n", packageName)
continue
}
switch source {
case "pacman":
err = packagemanager.UninstallPacmanPackage(packageName)
case "snap":
err = packagemanager.UninstallSnapPackage(packageName)
case "flatpak":
err = packagemanager.UninstallFlatpakPackage(packageName)
// Add cases for other package managers if necessary
default:
fmt.Printf("Unknown source for package %s\n", packageName)
continue
}
if err != nil {
fmt.Printf("Error uninstalling package %s: %v\n", packageName, err)
} else {
fmt.Printf("Successfully uninstalled package %s\n", packageName)
}
}
return nil
}
// readPackageList reads the package list from the pkg.list file
func readPackageList() (PackageList, error) {
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("error getting current user: %v", err)
}
pkgListPath := filepath.Join(usr.HomeDir, ".allpac", "pkg.list")
file, err := ioutil.ReadFile(pkgListPath)
if err != nil {
return nil, fmt.Errorf("error reading package list file: %v", err)
}
var pkgList PackageList
err = json.Unmarshal(file, &pkgList)
if err != nil {
return nil, fmt.Errorf("error decoding package list: %v", err)
}
return pkgList, nil
}
// AURResponse represents the structure of the response from AUR RPC
type AURResponse struct {
Version int `json:"version"`