diff --git a/cmd/.version b/cmd/.version index fdf0e6d..213152e 100644 --- a/cmd/.version +++ b/cmd/.version @@ -1 +1 @@ -AllPac V0.8 +AllPac V0.9 diff --git a/cmd/cli_utils.go b/cmd/cli_utils.go index ebc5b25..4dc9b12 100644 --- a/cmd/cli_utils.go +++ b/cmd/cli_utils.go @@ -1,10 +1,12 @@ package main import ( - "fmt" "embed" + "fmt" "io/fs" + "pixelridgesoftworks.com/AllPac/pkg/logger" + "pixelridgesoftworks.com/AllPac/pkg/packagemanager" ) func handleUpdateError(updateOption string, err error) { @@ -25,3 +27,13 @@ func handleVersion(args []string) { } fmt.Println(string(content)) } + +func handleRepair(args []string) { + // Assuming GetPkgListPath() returns a string path + pkgListPath, _ := packagemanager.GetPkgListPath() + + err :=packagemanager.InitializePkgListFile(pkgListPath) + if err != nil { + logger.Errorf("Error initializing version file: %v", err) + } +} diff --git a/cmd/main.go b/cmd/main.go index 5c936ff..b6c22cd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -45,6 +45,8 @@ func main() { handleToolCheck(args) case "version": handleVersion(args) + case "repair": + handleRepair(args) default: fmt.Printf("Unknown subcommand: %s\n", command) os.Exit(1) @@ -57,6 +59,11 @@ func handleUpdate(args []string) { return } + _, err := packagemanager.ReadPackageList() + if err != nil { + fmt.Printf("error reading package list. Consider running 'allpac repair': %v", err) + } + updateFuncs := map[string]func() error{ "everything": packagemanager.UpdateAllPackages, "snaps": func() error { return packagemanager.UpdateSnapPackages() }, diff --git a/pkg/packagemanager/install.go b/pkg/packagemanager/install.go index ccc9f57..380408a 100644 --- a/pkg/packagemanager/install.go +++ b/pkg/packagemanager/install.go @@ -14,9 +14,9 @@ import ( "pixelridgesoftworks.com/AllPac/pkg/logger" ) -// InstallPackagePacman installs a package using Pacman and logs the installation +// installs a package using Pacman and logs the installation func InstallPackagePacman(packageName string) error { - cmd := exec.Command("sudo", "pacman", "-S", "--noconfirm", packageName) + cmd := exec.Command("sudo", "pacman", "-Syu", "--noconfirm", packageName) if output, err := cmd.CombinedOutput(); err != nil { logger.Errorf("error installing package with Pacman: %s, %v", output, err) return fmt.Errorf("error installing package with Pacman: %s, %v", output, err) @@ -35,7 +35,7 @@ func InstallPackagePacman(packageName string) error { return nil } -// InstallPackageSnap installs a package using Snap and logs the installation +// installs a package using Snap and logs the installation func InstallPackageSnap(packageName string) error { cmd := exec.Command("sudo", "snap", "install", packageName) output, err := cmd.CombinedOutput() @@ -77,7 +77,7 @@ func InstallPackageSnap(packageName string) error { return nil } -// InstallPackageFlatpak installs a package using Flatpak and logs the installation +// installs a package using Flatpak and logs the installation func InstallPackageFlatpak(packageName string) error { cmd := exec.Command("flatpak", "install", "-y", packageName) if output, err := cmd.CombinedOutput(); err != nil { @@ -98,14 +98,19 @@ func InstallPackageFlatpak(packageName string) error { return nil } -// cloneAndInstallFromAUR clones the given AUR repository and installs it +// clones the given AUR repository and installs it func CloneAndInstallFromAUR(repoURL string, skipConfirmation bool) (string, error) { - // Request root permissions - if !skipConfirmation && !requestRootPermissions() { - logger.Warnf("root permissions denied") - return "", fmt.Errorf("root permissions denied") + // System update + if !skipConfirmation && !confirmAction("Do you want to update the system before proceeding? (skipping this step may result in partial updates, and break your system)") { + logger.Warnf("user aborted the system update") + return "", fmt.Errorf("user aborted the system update") } + cmdUpdate := exec.Command("sudo", "pacman", "-Syu", "--noconfirm") + if output, err := cmdUpdate.CombinedOutput(); err != nil { + logger.Errorf("error updating system: %s, %v", output, err) + return "", fmt.Errorf("error updating system: %s, %v", output, err) + } // Confirm before proceeding with each step if !skipConfirmation && !confirmAction("Do you want to download and build package from " + repoURL + "?") { logger.Warnf("user aborted the action") @@ -172,7 +177,7 @@ func CloneAndInstallFromAUR(repoURL string, skipConfirmation bool) (string, erro return version, nil } -// InstallSnap installs Snap manually from the AUR +// installs Snap manually from the AUR func InstallSnap() error { version, err := CloneAndInstallFromAUR("https://aur.archlinux.org/snapd.git", true) if err != nil { @@ -187,7 +192,7 @@ func InstallSnap() error { return nil } -// InstallGit installs Git using Pacman +// installs Git using Pacman func InstallGit() error { if err := InstallPackagePacman("git"); err != nil { logger.Errorf("error installing Git: %v", err) @@ -196,7 +201,7 @@ func InstallGit() error { return nil } -// InstallBaseDevel installs the base-devel group using Pacman +// installs the base-devel group using Pacman func InstallBaseDevel() error { if err := InstallPackagePacman("base-devel"); err != nil { logger.Errorf("error installing base-devel: %v", err) @@ -205,7 +210,7 @@ func InstallBaseDevel() error { return nil } -// InstallFlatpak installs the Flatpak package using Pacman +// installs the Flatpak package using Pacman func InstallFlatpak() error { if err := InstallPackagePacman("flatpak"); err != nil { logger.Errorf("error installing flatpak: %v", err) diff --git a/pkg/packagemanager/installer_utils.go b/pkg/packagemanager/installer_utils.go index 70918c1..40cdc32 100644 --- a/pkg/packagemanager/installer_utils.go +++ b/pkg/packagemanager/installer_utils.go @@ -9,7 +9,7 @@ import ( "pixelridgesoftworks.com/AllPac/pkg/logger" ) -// extractVersionFromPKGBUILD reads the PKGBUILD file and extracts the package version +// reads the PKGBUILD file and extracts the package version func ExtractVersionFromPKGBUILD(repoDir string) (string, error) { pkgbuildPath := filepath.Join(repoDir, "PKGBUILD") file, err := os.Open(pkgbuildPath) @@ -36,13 +36,7 @@ func ExtractVersionFromPKGBUILD(repoDir string) (string, error) { return "", fmt.Errorf("pkgver not found in PKGBUILD") } -// requestRootPermissions prompts the user for root permissions -func requestRootPermissions() bool { - fmt.Println("Root permissions are required to install AUR packages.") - return confirmAction("Do you want to continue with root permissions?") -} - -// confirmAction prompts the user with a yes/no question and returns true if the answer is yes +// prompts the user with a yes/no question and returns true if the answer is yes func confirmAction(question string) bool { reader := bufio.NewReader(os.Stdin) for { diff --git a/pkg/packagemanager/packagelist-manager.go b/pkg/packagemanager/packagelist-manager.go index 4c6bada..3f5e866 100644 --- a/pkg/packagemanager/packagelist-manager.go +++ b/pkg/packagemanager/packagelist-manager.go @@ -41,7 +41,7 @@ func GetPkgListPath() (string, error) { if _, err := os.Stat(pkgListPath); os.IsNotExist(err) { logger.Infof("pkg.list file does not exist, initializing: %s", pkgListPath) // Create and initialize the file if it doesn't exist - if err := initializePkgListFile(pkgListPath); err != nil { + if err := InitializePkgListFile(pkgListPath); err != nil { return "", err // Error already logged in initializePkgListFile } } else if err != nil { @@ -55,7 +55,7 @@ func GetPkgListPath() (string, error) { } // creates a new pkg.list file with an empty JSON object -func initializePkgListFile(filePath string) error { +func InitializePkgListFile(filePath string) error { file, err := os.Create(filePath) if err != nil { logger.Errorf("error creating package list file: %v", err) diff --git a/pkg/packagemanager/pacman.go b/pkg/packagemanager/pacman.go index aa8f7a0..4121b0b 100644 --- a/pkg/packagemanager/pacman.go +++ b/pkg/packagemanager/pacman.go @@ -48,7 +48,7 @@ func UpdatePacmanPackages(packageNames ...string) error { } if len(packagesToUpdate) > 0 { - args := append([]string{"sudo", "pacman", "-S", "--noconfirm"}, packagesToUpdate...) + args := append([]string{"sudo", "pacman", "-Syu", "--noconfirm"}, packagesToUpdate...) 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)