Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add dump option #13

Merged
merged 8 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type Commander interface {
BuildInstallCommand(name string) *exec.Cmd
BuildUninstallCommand(name string) *exec.Cmd
ListPackages() *exec.Cmd
}

// FromName create a commander from a package manager mane
Expand Down
17 changes: 15 additions & 2 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var RootCmd = &cobra.Command{
Long: `
Trouxa is a simple application to install and remove packages at once by just using a simple text file.
`,
Version: "0.0.1",
Version: "0.1.0",
Run: func(command *cobra.Command, args []string) {
installPackage := func(name string, commander commander.Commander) bool {
ok, _ := manager.InstallPackage(name, commander.BuildInstallCommand(name))
Expand All @@ -35,6 +35,15 @@ Trouxa is a simple application to install and remove packages at once by just us
if commander == nil {
log.Fatalln("This package manager is not supported or invalid for your system!")
}
if list, _ := command.Flags().GetBool("list"); list {
log.Debugln("Entered list, will do nothing else")
ok, err := manager.ListPackages(commander.ListPackages())
if !ok {
log.Errorln("Failed to list packages: ", err)
os.Exit(1)
}
os.Exit(0)
}
data, err := input.GetData(pathFilePackages)
if err != nil {
log.WithError(err).Fatalln() // TODO
Expand All @@ -61,7 +70,7 @@ func init() {
"manager",
"m",
"",
"Package manager to download your packages. apt | pacman | yay | apk",
"Package manager to download your packages",
)
err := RootCmd.MarkFlagRequired("manager")
if err != nil {
Expand All @@ -73,4 +82,8 @@ func init() {
"p",
"./packages.txt",
"The file used by Trouxa to download de packages to your system")
RootCmd.Flags().Bool(
"list",
false,
"List all installed packages in this package manager and exit")
}
5 changes: 5 additions & 0 deletions internal/manager/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("apk", "del", name)
}

// ListPackages lists all installed packages from apk
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("apk", "list", "--installed")
}
5 changes: 5 additions & 0 deletions internal/manager/apt/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (a *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (a *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("apt", "remove", name, "-y")
}

// ListPackages lists all installed packages from apt
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("apt", "list", "--installed")
}
5 changes: 5 additions & 0 deletions internal/manager/aptitude/aptitude.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (a *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (a *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("aptitude", "remove", "-y", name)
}

// ListPackages lists all installed packages from aptitude
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("aptitude", "search", "~i")
}
5 changes: 5 additions & 0 deletions internal/manager/dnf/dnf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("dnf", "remove", "-y", name)
}

// ListPackages lists all installed packages from dnf
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("dnf", "list", "installed")
}
5 changes: 5 additions & 0 deletions internal/manager/eopkg/eopkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("eopkg", "remove", "-y", name)
}

// ListPackages lists all installed packages from eopkg
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("eopkg", "li")
}
16 changes: 14 additions & 2 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func IsValid(name string) error {
return nil
}

// InstallPackage install a package with the apt package manager
// InstallPackage install a package with a package manager
func InstallPackage(name string, cmd *exec.Cmd) (bool, error) {
log.Infoln("Trying to install the package: ", name)
if ok, err := runCommand(cmd); !ok {
Expand All @@ -44,7 +44,7 @@ func InstallPackage(name string, cmd *exec.Cmd) (bool, error) {
return true, nil
}

// UninstallPackage uninstall a package with the apt package manager
// UninstallPackage uninstall a package with a package manager
func UninstallPackage(name string, cmd *exec.Cmd) (bool, error) {
log.Infoln("Uninstalling package: ", name)
if ok, err := runCommand(cmd); !ok {
Expand All @@ -56,3 +56,15 @@ func UninstallPackage(name string, cmd *exec.Cmd) (bool, error) {

return true, nil
}

// ListPackages lists all the installed packages via a package manager
func ListPackages(cmd *exec.Cmd) (bool, error) {
log.Infoln("Listing packages")
if ok, err := runCommand(cmd); !ok {
log.Errorln("Could not list packages: ", err)

return false, err
}

return true, nil
}
5 changes: 5 additions & 0 deletions internal/manager/pacman/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("pacman", "-Rs", name, "--noconfirm")
}

// ListPackages lists all installed packages from pacman
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("pacman", "-Qe")
}
5 changes: 5 additions & 0 deletions internal/manager/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("snap", "remove", name)
}

// ListPackages lists all installed packages from snap
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("snap", "list", "--all")
}
8 changes: 8 additions & 0 deletions internal/manager/yay/yay.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("yay", "-Rs", name, "--noconfirm")
}

// ListPackages lists all installed packages from yay
func (a *Commander) ListPackages() *exec.Cmd {
// There seems to be no way of distinguishing between packages installed by pacman and yay:
// https://www.reddit.com/r/archlinux/comments/woh8fr/comment/ikb075w/?utm_source=reddit&utm_medium=web2x&context=3
// So the command ends up being virtually the same here
return exec.Command("yay", "-Qe")
}
5 changes: 5 additions & 0 deletions internal/manager/yum/yum.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("yum", "remove", "-y", name)
}

// ListPackages lists all installed packages from yum
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("yum", "list", "installed")
}
5 changes: 5 additions & 0 deletions internal/manager/zypper/zypper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func (p *Commander) BuildInstallCommand(name string) *exec.Cmd {
func (p *Commander) BuildUninstallCommand(name string) *exec.Cmd {
return exec.Command("zypper", "remove", "-y", name)
}

// ListPackages lists all installed packages from zypper
func (a *Commander) ListPackages() *exec.Cmd {
return exec.Command("zypper", "search", "--installed-only")
}