2016-11-29 03:21:20 +01:00
|
|
|
package yay
|
2016-09-13 03:06:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2016-10-17 00:02:48 +02:00
|
|
|
|
|
|
|
"github.com/jguer/go-alpm"
|
2016-09-13 03:06:24 +02:00
|
|
|
)
|
|
|
|
|
2016-09-18 03:32:13 +02:00
|
|
|
// 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
|
2016-09-18 03:32:13 +02:00
|
|
|
Description string
|
|
|
|
Installed bool
|
2016-09-13 03:06:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 02:53:17 +01:00
|
|
|
// PacmanConf describes the default pacman config file
|
|
|
|
const PacmanConf string = "/etc/pacman.conf"
|
|
|
|
|
2016-11-29 03:21:20 +01:00
|
|
|
var conf alpm.PacmanConfig
|
2016-11-29 02:53:17 +01:00
|
|
|
|
|
|
|
func init() {
|
2016-11-29 03:21:20 +01:00
|
|
|
conf, _ = readConfig(PacmanConf)
|
2016-11-29 02:53:17 +01:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:32:13 +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
|
|
|
}
|
|
|
|
|
2016-10-02 18:23:55 +02:00
|
|
|
// UpdatePackages handles cache update and upgrade
|
2016-10-05 18:04:16 +02:00
|
|
|
func UpdatePackages(flags []string) error {
|
2016-10-02 18:23:55 +02:00
|
|
|
var cmd *exec.Cmd
|
2016-10-05 18:04:16 +02:00
|
|
|
var args []string
|
|
|
|
|
|
|
|
args = append(args, "pacman", "-Syu")
|
|
|
|
args = append(args, flags...)
|
|
|
|
|
|
|
|
cmd = exec.Command("sudo", args...)
|
2016-10-02 18:23:55 +02:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:02:43 +02:00
|
|
|
// SearchRepos searches and prints packages in repo
|
2016-11-30 14:59:38 +01:00
|
|
|
func SearchRepos(pkgName string, mode int) (err error) {
|
2016-10-11 01:02:43 +02:00
|
|
|
h, err := conf.CreateHandle()
|
|
|
|
defer h.Release()
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
2016-11-29 03:21:20 +01:00
|
|
|
dbList, _ := h.SyncDbs()
|
|
|
|
localdb, _ := h.LocalDb()
|
2016-10-11 01:02:43 +02:00
|
|
|
|
|
|
|
var installed bool
|
|
|
|
var i int
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode != SearchMode && !installed:
|
2016-10-17 00:02:48 +02:00
|
|
|
fmt.Printf("%d \x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[32;40mInstalled\x1b[0m\n%s\n",
|
2016-10-11 01:02:43 +02:00
|
|
|
i, db.Name(), pkg.Name(), pkg.Version(), pkg.Description())
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode != SearchMode && !installed:
|
2016-10-17 00:02:48 +02:00
|
|
|
fmt.Printf("%d \x1b[1m%s/\x1b[33m%s \x1b[36m%s\x1b[0m\n%s\n",
|
2016-10-11 01:02:43 +02:00
|
|
|
i, db.Name(), pkg.Name(), pkg.Version(), pkg.Description())
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode == SearchMode && !installed:
|
2016-10-17 00:02:48 +02:00
|
|
|
fmt.Printf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[32;40mInstalled\x1b[0m\n%s\n",
|
2016-10-11 01:02:43 +02:00
|
|
|
db.Name(), pkg.Name(), pkg.Version(), pkg.Description())
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode == SearchMode && !installed:
|
2016-10-17 00:02:48 +02:00
|
|
|
fmt.Printf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s\x1b[0m\n%s\n",
|
2016-10-11 01:02:43 +02:00
|
|
|
db.Name(), pkg.Name(), pkg.Version(), pkg.Description())
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-18 03:32:13 +02:00
|
|
|
// SearchPackages handles repo searches. Creates a RepoSearch struct.
|
2016-11-30 14:59:38 +01:00
|
|
|
func SearchPackages(pkgName string) (s RepoSearch, err error) {
|
2016-09-13 03:06:24 +02:00
|
|
|
h, err := conf.CreateHandle()
|
|
|
|
defer h.Release()
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
2016-11-29 03:21:20 +01:00
|
|
|
dbList, _ := h.SyncDbs()
|
|
|
|
localdb, _ := h.LocalDb()
|
2016-09-13 03:06:24 +02:00
|
|
|
|
2016-09-18 03:32:13 +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) {
|
2016-09-18 03:32:13 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-18 03:32:13 +02:00
|
|
|
//PrintSearch receives a RepoSearch type and outputs pretty text.
|
2016-10-02 18:23:55 +02:00
|
|
|
func (s *RepoSearch) PrintSearch(mode int) {
|
2016-09-18 03:32:13 +02:00
|
|
|
for i, pkg := range s.Results {
|
2016-10-02 18:23:55 +02:00
|
|
|
switch {
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode != SearchMode && pkg.Installed:
|
2016-10-02 18:23:55 +02:00
|
|
|
fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s \x1B[32;40mInstalled\033[0m\n%s\n",
|
2016-09-18 03:32:13 +02:00
|
|
|
i, pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode != SearchMode && !pkg.Installed:
|
2016-10-02 18:23:55 +02:00
|
|
|
fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
|
2016-09-18 03:32:13 +02:00
|
|
|
i, pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode == SearchMode && pkg.Installed:
|
2016-10-02 18:23:55 +02:00
|
|
|
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)
|
2016-11-29 03:21:20 +01:00
|
|
|
case mode == SearchMode && !pkg.Installed:
|
2016-09-13 03:06:24 +02:00
|
|
|
fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
|
2016-09-18 03:32:13 +02:00
|
|
|
pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
|
2016-09-13 03:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-02 18:23:55 +02:00
|
|
|
|
2016-11-29 03:21:20 +01:00
|
|
|
// PassToPacman outsorces execution to pacman binary without modifications.
|
|
|
|
func PassToPacman(op string, pkgs []string, flags []string) error {
|
2016-10-02 18:23:55 +02:00
|
|
|
var cmd *exec.Cmd
|
2016-10-02 19:19:26 +02:00
|
|
|
var args []string
|
|
|
|
|
|
|
|
args = append(args, op)
|
2016-10-05 18:04:16 +02:00
|
|
|
if len(pkgs) != 0 {
|
|
|
|
args = append(args, pkgs...)
|
2016-10-02 19:19:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:04:16 +02:00
|
|
|
if len(flags) != 0 {
|
|
|
|
args = append(args, flags...)
|
2016-10-02 19:19:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:04:16 +02:00
|
|
|
if strings.Contains(op, "-Q") {
|
2016-10-02 19:19:26 +02:00
|
|
|
cmd = exec.Command("pacman", args...)
|
2016-10-02 18:23:55 +02:00
|
|
|
} 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 18:23:55 +02:00
|
|
|
}
|
2016-10-02 19:19:26 +02:00
|
|
|
|
2016-10-02 18:23:55 +02:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|