yay/actions.go

109 lines
2.3 KiB
Go
Raw Normal View History

2016-09-13 01:06:24 +00:00
package main
import (
"bufio"
2016-10-02 17:52:48 +00:00
"bytes"
2016-09-13 01:06:24 +00:00
"errors"
"fmt"
"github.com/Jguer/go-alpm"
"github.com/Jguer/yay/aur"
"os"
2016-10-02 20:50:23 +00:00
"os/exec"
2016-09-13 01:06:24 +00:00
"strconv"
"strings"
)
func searchAndInstall(pkgName string, conf *alpm.PacmanConfig, flags string) (err error) {
2016-09-13 01:06:24 +00:00
var num int
var numberString string
2016-10-02 17:52:48 +00:00
var pacBuffer bytes.Buffer
2016-09-13 01:06:24 +00:00
a, err := aur.Search(pkgName, true)
r, err := SearchPackages(pkgName, conf)
2016-09-13 01:06:24 +00:00
if err != nil {
return
}
if len(r.Results) == 0 && a.Resultcount == 0 {
2016-09-13 01:06:24 +00:00
return errors.New("No Packages match search")
}
r.PrintSearch(0)
a.PrintSearch(len(r.Results))
2016-09-13 01:06:24 +00:00
fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
reader := bufio.NewReader(os.Stdin)
numberString, err = reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
2016-10-02 17:52:48 +00:00
var aurInstall []aur.Result
2016-09-13 01:06:24 +00:00
result := strings.Fields(numberString)
for _, numS := range result {
num, err = strconv.Atoi(numS)
if err != nil {
fmt.Println(err)
continue
}
// Install package
if num > len(r.Results)-1 {
2016-10-02 17:52:48 +00:00
aurInstall = append(aurInstall, a.Results[num-len(r.Results)])
2016-09-13 01:06:24 +00:00
} else {
2016-10-02 17:52:48 +00:00
pacBuffer.WriteString(r.Results[num].Name)
pacBuffer.WriteString(" ")
2016-09-13 01:06:24 +00:00
}
}
2016-10-02 20:50:23 +00:00
if pacBuffer.String() != "" {
var cmd *exec.Cmd
if flags == "" {
cmd = exec.Command("sudo", "pacman", "-S", strings.TrimSpace(pacBuffer.String()))
} else {
cmd = exec.Command("sudo", "pacman", "-S", strings.TrimSpace(pacBuffer.String()), flags)
}
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err = cmd.Run()
}
2016-10-02 17:52:48 +00:00
for _, aurpkg := range aurInstall {
err = aurpkg.Install(BuildDir, conf, flags)
if err != nil {
// Do not abandon program, we might still be able to install the rest
fmt.Println(err)
}
}
2016-09-13 01:06:24 +00:00
return
}
// updateAndInstall handles updating the cache and installing updates
func updateAndInstall(conf *alpm.PacmanConfig, flags string) error {
errp := UpdatePackages(flags)
erra := aur.UpdatePackages(BuildDir, conf, flags)
if errp != nil {
return errp
}
return erra
}
func searchMode(pkg string, conf *alpm.PacmanConfig) (err error) {
a, err := aur.Search(pkg, true)
2016-09-13 01:06:24 +00:00
if err != nil {
return err
}
r, err := SearchPackages(pkg, conf)
2016-09-13 01:06:24 +00:00
if err != nil {
return err
}
r.PrintSearch(SearchMode)
a.PrintSearch(SearchMode)
2016-09-13 01:06:24 +00:00
return nil
}