Bold text, refactoring and AUR dependency grabbing

This commit is contained in:
Jguer 2016-09-07 10:10:04 +01:00
parent bf0c6e4692
commit bacd56083b
3 changed files with 77 additions and 52 deletions

57
aur.go
View file

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -90,25 +91,24 @@ func (r AurSearch) Swap(i, j int) {
r.Results[i], r.Results[j] = r.Results[j], r.Results[i] r.Results[i], r.Results[j] = r.Results[j], r.Results[i]
} }
func searchAurPackages(pkg string) (search AurSearch) { func searchAurPackages(pkg string) (search AurSearch, err error) {
getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &search) err = getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &search)
sort.Sort(search) sort.Sort(search)
return search return
} }
func infoAurPackage(pkg string) (info AurInfo) { func infoAurPackage(pkg string) (info AurInfo, err error) {
fmt.Println("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]=" + pkg) err = getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &info)
getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &info) return
return info
} }
func (r AurSearch) printSearch(index int) (err error) { func (r AurSearch) printSearch(index int) (err error) {
for i, result := range r.Results { for i, result := range r.Results {
if index != SearchMode { if index != SearchMode {
fmt.Printf("%d aur/\x1B[33m%s\033[0m \x1B[36m%s\033[0m (%d)\n %s\n", fmt.Printf("%d \033[1maur/\x1B[33m%s \x1B[36m%s\033[0m (%d)\n %s\n",
i+index, result.Name, result.Version, result.NumVotes, result.Description) i+index, result.Name, result.Version, result.NumVotes, result.Description)
} else { } else {
fmt.Printf("aur/\x1B[33m%s\033[0m \x1B[36m%s\033[0m (%d)\n %s\n", fmt.Printf("\033[1maur/\x1B[33m%s \x1B[36m%s\033[0m (%d)\n %s\n",
result.Name, result.Version, result.NumVotes, result.Description) result.Name, result.Version, result.NumVotes, result.Description)
} }
} }
@ -116,23 +116,6 @@ func (r AurSearch) printSearch(index int) (err error) {
return return
} }
func (r AurSearch) installAurArray(num []int, index int) (err error) {
if len(num) == 0 {
return nil
}
for _, i := range num {
fmt.Printf("%+v\n\n", r.Results[i-index])
err = r.Results[i-index].installResult()
if err != nil {
fmt.Println(err)
return err
}
}
return err
}
func downloadFile(filepath string, url string) (err error) { func downloadFile(filepath string, url string) (err error) {
// Create the file // Create the file
out, err := os.Create(filepath) out, err := os.Create(filepath)
@ -185,29 +168,35 @@ func (a AurResult) getDepsfromFile(pkgbuildLoc string) (err error) {
return nil return nil
} }
func (a AurResult) getDepsFromRPC() (err error) { func (a AurResult) getDepsFromRPC() (final []string, err error) {
info := infoAurPackage(a.Name)
fmt.Printf("%+v\n", info)
f := func(c rune) bool { f := func(c rune) bool {
return c == '>' || c == '<' || c == '=' || c == ' ' return c == '>' || c == '<' || c == '=' || c == ' '
} }
info, err := infoAurPackage(a.Name)
if len(info.Results) == 0 {
return final, errors.New("Failed to get deps from RPC")
}
for _, deps := range info.Results[0].MakeDepends { for _, deps := range info.Results[0].MakeDepends {
fields := strings.FieldsFunc(deps, f) fields := strings.FieldsFunc(deps, f)
fmt.Println(fields[0]) if !isInRepo(fields[0]) {
final = append(final, fields[0])
}
} }
for _, deps := range info.Results[0].Depends { for _, deps := range info.Results[0].Depends {
fields := strings.FieldsFunc(deps, f) fields := strings.FieldsFunc(deps, f)
fmt.Println(fields[0]) if !isInRepo(fields[0]) {
final = append(final, fields[0])
}
} }
return return
} }
func (a AurResult) getAURDependencies() (err error) { func (a AurResult) getAURDependencies() (err error) {
a.getDepsFromRPC() _, err = a.getDepsFromRPC()
return nil return nil
} }
@ -244,7 +233,7 @@ func (a AurResult) installResult() (err error) {
a.getAURDependencies() a.getAURDependencies()
os.Exit(0) os.Exit(0)
fmt.Print("==> Edit PKGBUILD? (y/n)") fmt.Print("\x1b[32m==> Edit PKGBUILD? (y/n)\033[0m")
var response string var response string
fmt.Scanln(&response) fmt.Scanln(&response)
if strings.ContainsAny(response, "y & Y") { if strings.ContainsAny(response, "y & Y") {

22
repo.go
View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
c "github.com/fatih/color"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -33,12 +32,9 @@ func getInstalledPackage(pkg string) (err error) {
// SearchPackages handles repo searches // SearchPackages handles repo searches
func SearchPackages(pkg string) (search RepoSearch, err error) { func SearchPackages(pkg string) (search RepoSearch, err error) {
cmdOutput, err := exec.Command(PacmanBin, "-Ss", pkg).Output() cmdOutput, err := exec.Command(PacmanBin, "-Ss", pkg).Output()
if err != nil {
return
}
outputSlice := strings.Split(string(cmdOutput), "\n") outputSlice := strings.Split(string(cmdOutput), "\n")
if outputSlice[0] == "" { if outputSlice[0] == "" {
return return search, nil
} }
i := true i := true
@ -62,17 +58,23 @@ func SearchPackages(pkg string) (search RepoSearch, err error) {
return return
} }
func isInRepo(pkg string) bool {
if _, err := exec.Command(PacmanBin, "-Sp", pkg).Output(); err != nil {
return false
}
return true
}
func (s RepoSearch) printSearch(index int) (err error) { func (s RepoSearch) printSearch(index int) (err error) {
yellow := c.New(c.FgYellow).SprintFunc()
green := c.New(c.FgGreen).SprintFunc()
for i, result := range s.Results { for i, result := range s.Results {
if index != SearchMode { if index != SearchMode {
fmt.Printf("%d %s/\x1B[33m%s\033[0m \x1B[36m%s\033[0m\n%s\n", fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
i, result.Repository, result.Name, result.Version, result.Description) i, result.Repository, result.Name, result.Version, result.Description)
} else { } else {
fmt.Printf("%s/\x1B[33m%s\033[0m \x1B[36m%s\033[0m\n%s\n", fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
result.Repository, yellow(result.Name), green(result.Version), result.Description) result.Repository, result.Name, result.Version, result.Description)
} }
} }

50
yay.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -55,22 +56,51 @@ func getNums() (numbers []int, err error) {
return return
} }
func defaultMode(pkg string) { func installnumArray(num []int, aurRes AurSearch, repoRes RepoSearch) (err error) {
aurRes := searchAurPackages(pkg) 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)
repoRes, err := SearchPackages(pkg) repoRes, err := SearchPackages(pkg)
if err != nil {
return
}
if repoRes.Resultcount == 0 && aurRes.Resultcount == 0 { if repoRes.Resultcount == 0 && aurRes.Resultcount == 0 {
os.Exit(1) return errors.New("No Packages match search")
} }
repoRes.printSearch(0) repoRes.printSearch(0)
err = aurRes.printSearch(repoRes.Resultcount) err = aurRes.printSearch(repoRes.Resultcount)
nums, err := getNums() nums, err := getNums()
if err != nil { if err != nil {
fmt.Println(err) return
os.Exit(1)
} }
aurRes.installAurArray(nums, repoRes.Resultcount) err = installnumArray(nums, aurRes, repoRes)
if err != nil {
return
}
return
} }
func main() { func main() {
@ -79,5 +109,9 @@ func main() {
Editor = os.Getenv("EDITOR") Editor = os.Getenv("EDITOR")
} }
searchTerm := flag.Args() searchTerm := flag.Args()
defaultMode(searchTerm[0]) err := defaultMode(searchTerm[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} }