yay/yay.go

118 lines
2.5 KiB
Go
Raw Normal View History

2016-09-05 02:17:51 +00:00
package main
import (
"bufio"
"errors"
2016-09-05 02:17:51 +00:00
"flag"
"fmt"
"os"
"strconv"
"strings"
)
// PacmanBin describes the default installation point of pacman
const PacmanBin string = "/usr/bin/pacman"
2016-09-06 21:22:20 +00:00
// MakepkgBin describes the default installation point of makepkg command
const MakepkgBin string = "/usr/bin/makepkg"
// TarBin describes the default installation point of tar command
// Probably will replace untar with code solution
const TarBin string = "/usr/bin/tar"
2016-09-05 02:17:51 +00:00
// SearchMode is search without numbers
const SearchMode int = -1
2016-09-05 22:47:36 +00:00
// BuildDir is the root for package building
2016-09-06 21:22:20 +00:00
const BuildDir string = "/tmp/yaytmp/"
2016-09-05 22:47:36 +00:00
// BaseURL givers the AUR default address
const BaseURL string = "https://aur.archlinux.org"
2016-09-06 21:22:20 +00:00
// Editor gives the default system editor, uses vi in last case
var Editor = "vi"
2016-09-05 02:17:51 +00:00
func getNums() (numbers []int, err error) {
var numberString string
2016-09-05 22:47:36 +00:00
fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
2016-09-05 02:17:51 +00:00
reader := bufio.NewReader(os.Stdin)
numberString, err = reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
result := strings.Fields(numberString)
var num int
for _, numS := range result {
num, err = strconv.Atoi(numS)
if err != nil {
fmt.Println(err)
return
}
numbers = append(numbers, num)
}
return
}
func installnumArray(num []int, aurRes AurSearch, repoRes RepoSearch) (err error) {
if len(num) == 0 {
return errors.New("Installing AUR array: No nums selected")
}
var index int
for _, i := range num {
if i > repoRes.Resultcount-1 {
index = i - repoRes.Resultcount
fmt.Printf("%+v\n\n", aurRes.Results[i-index])
err = aurRes.Results[i-index].installResult()
if err != nil {
// Do not abandon program, we might still be able to install the rest
fmt.Println(err)
}
} else {
}
}
return err
}
func defaultMode(pkg string) (err error) {
aurRes, err := searchAurPackages(pkg)
2016-09-05 02:17:51 +00:00
repoRes, err := SearchPackages(pkg)
if err != nil {
return
}
2016-09-05 22:47:36 +00:00
if repoRes.Resultcount == 0 && aurRes.Resultcount == 0 {
return errors.New("No Packages match search")
2016-09-05 22:47:36 +00:00
}
2016-09-05 02:17:51 +00:00
repoRes.printSearch(0)
err = aurRes.printSearch(repoRes.Resultcount)
2016-09-05 22:47:36 +00:00
2016-09-05 02:17:51 +00:00
nums, err := getNums()
if err != nil {
return
2016-09-05 02:17:51 +00:00
}
err = installnumArray(nums, aurRes, repoRes)
if err != nil {
return
}
return
2016-09-05 02:17:51 +00:00
}
func main() {
flag.Parse()
2016-09-06 21:22:20 +00:00
if os.Getenv("EDITOR") != "" {
Editor = os.Getenv("EDITOR")
}
2016-09-05 02:17:51 +00:00
searchTerm := flag.Args()
err := defaultMode(searchTerm[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2016-09-05 02:17:51 +00:00
}