2017-08-02 17:24:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
rpc "github.com/mikkeloscar/aur"
|
|
|
|
)
|
|
|
|
|
|
|
|
func downloadFile(path string, url string) (err error) {
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(path)
|
|
|
|
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)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadAndUnpack downloads url tgz and extracts to path.
|
|
|
|
func downloadAndUnpack(url string, path string, trim bool) (err error) {
|
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := strings.Split(url, "/")
|
|
|
|
fileName := tokens[len(tokens)-1]
|
|
|
|
|
|
|
|
tarLocation := path + fileName
|
|
|
|
defer os.Remove(tarLocation)
|
|
|
|
|
|
|
|
err = downloadFile(tarLocation, url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if trim {
|
|
|
|
err = exec.Command("/bin/sh", "-c",
|
2017-08-04 09:26:53 +00:00
|
|
|
config.TarBin+" --strip-components 2 --include='*/"+fileName[:len(fileName)-7]+"/trunk/' -xf "+tarLocation+" -C "+path).Run()
|
2017-08-02 17:24:03 +00:00
|
|
|
os.Rename(path+"trunk", path+fileName[:len(fileName)-7]) // kurwa
|
|
|
|
} else {
|
2017-08-04 09:26:53 +00:00
|
|
|
err = exec.Command(config.TarBin, "-xf", tarLocation, "-C", path).Run()
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPkgbuild(pkg string) (err error) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wd = wd + "/"
|
|
|
|
|
|
|
|
err = getPkgbuildfromABS(pkg, wd)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:26:53 +00:00
|
|
|
err = getPkgbuildfromAUR(pkg, wd)
|
2017-08-02 17:24:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the ABS.
|
|
|
|
func getPkgbuildfromABS(pkgN string, path string) (err error) {
|
2017-10-18 02:38:19 +00:00
|
|
|
dbList, err := alpmHandle.SyncDbs()
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, db := range dbList.Slice() {
|
|
|
|
pkg, err := db.PkgByName(pkgN)
|
|
|
|
if err == nil {
|
|
|
|
var url string
|
|
|
|
if db.Name() == "core" || db.Name() == "extra" {
|
2018-02-22 14:47:08 +00:00
|
|
|
url = "https://projects.archlinux.org/svntogit/packages.git/snapshot/packages/" + pkg.Base() + ".tar.gz"
|
2017-08-02 17:24:03 +00:00
|
|
|
} else if db.Name() == "community" {
|
2018-02-22 14:47:08 +00:00
|
|
|
url = "https://projects.archlinux.org/svntogit/community.git/snapshot/community-packages/" + pkg.Base() + ".tar.gz"
|
2017-08-02 17:24:03 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Not in standard repositories")
|
|
|
|
}
|
2018-01-26 15:04:10 +00:00
|
|
|
fmt.Println(boldGreenFg(arrow), boldYellowFg(pkgN), boldGreenFg("found in ABS."))
|
2017-08-04 09:26:53 +00:00
|
|
|
errD := downloadAndUnpack(url, path, true)
|
2017-08-02 17:24:03 +00:00
|
|
|
return errD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("package not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the AUR.
|
|
|
|
func getPkgbuildfromAUR(pkgN string, dir string) (err error) {
|
|
|
|
aq, err := rpc.Info([]string{pkgN})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(aq) == 0 {
|
|
|
|
return fmt.Errorf("no results")
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:04:10 +00:00
|
|
|
fmt.Println(boldGreenFg(arrow), boldYellowFg(pkgN), boldGreenFg("found in AUR."))
|
2017-08-04 09:26:53 +00:00
|
|
|
downloadAndUnpack(baseURL+aq[0].URLPath, dir, false)
|
2017-08-02 17:24:03 +00:00
|
|
|
return
|
|
|
|
}
|