Road to AUR installs

This commit is contained in:
Jguer 2016-09-06 22:22:20 +01:00
parent 5787b3e5d3
commit 6671cefef0
4 changed files with 121 additions and 24 deletions

View file

@ -4,7 +4,7 @@
# so that import path resolution will prioritize
# our third party snapshots.
LDFLAGS=-ldflags "-s -w"
GOFILES=$(shell ls ./src/*.go)
GOFILES=$(shell ls *.go)
BINARY=./bin/yay
default: build

View file

@ -3,26 +3,38 @@ package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"sort"
"strings"
)
// AurResult describes an AUR package
type AurResult struct {
Description string `json:"Description"`
FirstSubmitted int `json:"FirstSubmitted"`
ID int `json:"ID"`
LastModified int `json:"LastModified"`
Maintainer string `json:"Maintainer"`
Name string `json:"Name"`
NumVotes int `json:"NumVotes"`
OutOfDate interface{} `json:"OutOfDate"`
PackageBase string `json:"PackageBase"`
PackageBaseID int `json:"PackageBaseID"`
Popularity int `json:"Popularity"`
URL string `json:"URL"`
URLPath string `json:"URLPath"`
PackageBase string `json:"PackageBase"`
Version string `json:"Version"`
Description string `json:"Description"`
URL string `json:"URL"`
NumVotes int `json:"NumVotes"`
Popularity int `json:"Popularity"`
OutOfDate interface{} `json:"OutOfDate"`
Maintainer string `json:"Maintainer"`
FirstSubmitted int `json:"FirstSubmitted"`
LastModified int `json:"LastModified"`
URLPath string `json:"URLPath"`
}
// AurSearch describes an AUR search
type AurSearch struct {
Resultcount int `json:"resultcount"`
Results []AurResult `json:"results"`
Type string `json:"type"`
Version int `json:"version"`
}
// getJSON handles JSON retrieval and decoding to struct
@ -36,14 +48,6 @@ func getJSON(url string, target interface{}) error {
return json.NewDecoder(r.Body).Decode(target)
}
// AurSearch describes an AUR search
type AurSearch struct {
Resultcount int `json:"resultcount"`
Results []AurResult `json:"results"`
Type string `json:"type"`
Version int `json:"version"`
}
func (r AurSearch) Len() int {
return len(r.Results)
}
@ -63,7 +67,6 @@ func searchAurPackages(pkg string) (search AurSearch) {
}
func (r AurSearch) printSearch(index int) (err error) {
for i, result := range r.Results {
if index != SearchMode {
fmt.Printf("%d aur/\x1B[33m%s\033[0m \x1B[36m%s\033[0m (%d)\n %s\n",
@ -78,10 +81,15 @@ func (r AurSearch) printSearch(index int) (err error) {
}
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
}
}
@ -89,6 +97,80 @@ func (r AurSearch) installAurArray(num []int, index int) (err error) {
return err
}
func (a AurResult) installResult() error {
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func (a AurResult) getAURDependencies() {
return
}
func (a AurResult) installResult() (err error) {
// No need to use filepath.separators because it won't run on inferior platforms
err = os.MkdirAll(BuildDir+"builds", 0755)
if err != nil {
fmt.Println(err)
return
}
tarLocation := BuildDir + a.Name + ".tar.gz"
// err = os.MkdirAll(BuildDir+a.Name, 0755)
// if err != nil {
// return
// }
err = downloadFile(tarLocation, BaseURL+a.URLPath)
if err != nil {
return
}
err = exec.Command(TarBin, "-xf", tarLocation, "-C", BuildDir).Run()
if err != nil {
return
}
err = os.Chdir(BuildDir + a.Name)
if err != nil {
return
}
a.getAURDependencies()
fmt.Print("==> Edit PKGBUILD? (y/n)")
var response string
fmt.Scanln(&response)
if strings.ContainsAny(response, "y & Y") {
editcmd := exec.Command(Editor, BuildDir+a.Name+"/"+"PKGBUILD")
editcmd.Stdout = os.Stdout
editcmd.Stderr = os.Stderr
editcmd.Stdin = os.Stdin
err = editcmd.Run()
}
makepkgcmd := exec.Command(MakepkgBin, "-sri")
makepkgcmd.Stdout = os.Stdout
makepkgcmd.Stderr = os.Stderr
makepkgcmd.Stdin = os.Stdin
err = makepkgcmd.Run()
return
}

View file

@ -32,8 +32,10 @@ func getInstalledPackage(pkg string) (err error) {
// SearchPackages handles repo searches
func SearchPackages(pkg string) (search RepoSearch, err error) {
cmd := exec.Command(PacmanBin, "-Ss", pkg)
cmdOutput, _ := cmd.Output()
cmdOutput, err := exec.Command(PacmanBin, "-Ss", pkg).Output()
if err != nil {
return
}
outputSlice := strings.Split(string(cmdOutput), "\n")
if outputSlice[0] == "" {
return

View file

@ -12,15 +12,25 @@ import (
// PacmanBin describes the default installation point of pacman
const PacmanBin string = "/usr/bin/pacman"
// 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"
// SearchMode is search without numbers
const SearchMode int = -1
// BuildDir is the root for package building
const BuildDir string = "/tmp/yay/"
const BuildDir string = "/tmp/yaytmp/"
// BaseURL givers the AUR default address
const BaseURL string = "https://aur.archlinux.org"
// Editor gives the default system editor, uses vi in last case
var Editor = "vi"
func getNums() (numbers []int, err error) {
var numberString string
fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
@ -65,6 +75,9 @@ func defaultMode(pkg string) {
func main() {
flag.Parse()
if os.Getenv("EDITOR") != "" {
Editor = os.Getenv("EDITOR")
}
searchTerm := flag.Args()
defaultMode(searchTerm[0])
}