diff --git a/Makefile b/Makefile index 438b2913..f46c56e7 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/aur.go b/aur.go similarity index 59% rename from src/aur.go rename to aur.go index fb88dff0..2b631428 100644 --- a/src/aur.go +++ b/aur.go @@ -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 +} diff --git a/src/repo.go b/repo.go similarity index 94% rename from src/repo.go rename to repo.go index 007564cf..ae8a85c5 100644 --- a/src/repo.go +++ b/repo.go @@ -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 diff --git a/src/main.go b/yay.go similarity index 74% rename from src/main.go rename to yay.go index f4b8501e..c0169c74 100644 --- a/src/main.go +++ b/yay.go @@ -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]) }