yay/pacman.go

189 lines
4.0 KiB
Go
Raw Normal View History

2016-09-13 03:06:24 +02:00
package main
import (
"fmt"
"github.com/Jguer/go-alpm"
"github.com/Jguer/yay/aur"
"os"
"os/exec"
"strings"
)
// RepoSearch describes a Repository search.
type RepoSearch struct {
Results []Result
}
// Result describes a pkg.
type Result struct {
Name string
2016-09-13 03:06:24 +02:00
Repository string
Version string
Description string
Installed bool
2016-09-13 03:06:24 +02:00
}
func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
file, err := os.Open(pacmanconf)
if err != nil {
return
}
conf, err = alpm.ParseConfig(file)
if err != nil {
return
}
return
2016-09-13 03:06:24 +02:00
}
// InstallPackage handles package install
2016-10-02 22:50:23 +02:00
func InstallPackage(pkgs []string, conf *alpm.PacmanConfig, flags string) error {
h, err := conf.CreateHandle()
defer h.Release()
if err != nil {
return err
}
dbList, err := h.SyncDbs()
if err != nil {
return err
}
var foreign []string
var args []string
repocnt := 0
args = append(args, "pacman")
args = append(args, "-S")
for _, pkg := range pkgs {
found := false
for _, db := range dbList.Slice() {
_, err = db.PkgByName(pkg)
if err == nil {
found = true
args = append(args, pkg)
repocnt++
break
}
2016-09-13 03:06:24 +02:00
}
2016-10-02 22:50:23 +02:00
if !found {
foreign = append(foreign, pkg)
2016-09-13 03:06:24 +02:00
}
2016-10-02 22:50:23 +02:00
}
if flags != "" {
args = append(args, flags)
}
if repocnt != 0 {
var cmd *exec.Cmd
cmd = exec.Command("sudo", args...)
2016-09-13 03:06:24 +02:00
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err = cmd.Run()
2016-10-02 22:50:23 +02:00
}
for _, aurpkg := range foreign {
err = aur.Install(aurpkg, BuildDir, conf, flags)
2016-09-13 03:06:24 +02:00
}
return nil
}
// UpdatePackages handles cache update and upgrade
func UpdatePackages(flags string) error {
var cmd *exec.Cmd
if flags == "" {
cmd = exec.Command("sudo", "pacman", "-Syu")
} else {
cmd = exec.Command("sudo", "pacman", "-Syu", flags)
}
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
// SearchPackages handles repo searches. Creates a RepoSearch struct.
func SearchPackages(pkgName string, conf *alpm.PacmanConfig) (s RepoSearch, err error) {
2016-09-13 03:06:24 +02:00
h, err := conf.CreateHandle()
defer h.Release()
if err != nil {
}
dbList, err := h.SyncDbs()
localdb, err := h.LocalDb()
2016-09-13 03:06:24 +02:00
var installed bool
2016-09-13 03:06:24 +02:00
for _, db := range dbList.Slice() {
for _, pkg := range db.PkgCache().Slice() {
if strings.Contains(pkg.Name(), pkgName) {
if r, _ := localdb.PkgByName(pkg.Name()); r != nil {
installed = true
} else {
installed = false
}
s.Results = append(s.Results, Result{
Name: pkg.Name(),
Description: pkg.Description(),
Version: pkg.Version(),
Repository: db.Name(),
Installed: installed,
})
2016-09-13 03:06:24 +02:00
}
}
}
return
}
//PrintSearch receives a RepoSearch type and outputs pretty text.
func (s *RepoSearch) PrintSearch(mode int) {
for i, pkg := range s.Results {
switch {
case mode != SearchMode && pkg.Installed == true:
fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s \x1B[32;40mInstalled\033[0m\n%s\n",
i, pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
case mode != SearchMode && pkg.Installed != true:
fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
i, pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
case mode == SearchMode && pkg.Installed == true:
fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s \x1B[32;40mInstalled\033[0m\n%s\n",
pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
case mode == SearchMode && pkg.Installed != true:
2016-09-13 03:06:24 +02:00
fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
2016-09-13 03:06:24 +02:00
}
}
}
2016-10-02 19:19:26 +02:00
func passToPacman(op string, pkg string, flags string) error {
var cmd *exec.Cmd
2016-10-02 19:19:26 +02:00
var args []string
args = append(args, op)
if pkg != "" {
args = append(args, pkg)
}
if flags != "" {
args = append(args, flags)
}
if strings.Contains(op, "Q") {
cmd = exec.Command("pacman", args...)
} else {
2016-10-02 19:52:48 +02:00
args = append([]string{"pacman"}, args...)
2016-10-02 19:19:26 +02:00
cmd = exec.Command("sudo", args...)
}
2016-10-02 19:19:26 +02:00
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}