2017-08-02 17:24:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-03-28 14:52:20 +00:00
|
|
|
"path/filepath"
|
2017-08-02 17:24:03 +00:00
|
|
|
)
|
|
|
|
|
2018-03-28 14:52:20 +00:00
|
|
|
// Decide what download method to use:
|
|
|
|
// Use the config option when the destination does not already exits
|
|
|
|
// If .git exists in the destination uer git
|
|
|
|
// Otherwise use a tarrball
|
|
|
|
func shouldUseGit(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return config.GitClone
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(filepath.Join(path, ".git"))
|
2018-04-27 00:25:40 +00:00
|
|
|
return os.IsExist(err)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:52:20 +00:00
|
|
|
func gitDownload(url string, path string, name string) error {
|
|
|
|
_, err := os.Stat(filepath.Join(path, name, ".git"))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = passToGit(path, "clone", url, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error cloning %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = passToGit(filepath.Join(path, name), "fetch")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error fetching %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD")
|
|
|
|
if err != nil {
|
2018-04-23 17:06:56 +00:00
|
|
|
return fmt.Errorf("error resetting %s", name)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error merging %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// DownloadAndUnpack downloads url tgz and extracts to path.
|
2018-05-14 17:33:48 +00:00
|
|
|
func downloadAndUnpack(url string, path string) (err error) {
|
2017-08-02 17:24:03 +00:00
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:33:48 +00:00
|
|
|
fileName := filepath.Base(url)
|
2017-08-02 17:24:03 +00:00
|
|
|
|
2018-04-17 00:32:31 +00:00
|
|
|
tarLocation := filepath.Join(path, fileName)
|
2017-08-02 17:24:03 +00:00
|
|
|
defer os.Remove(tarLocation)
|
|
|
|
|
|
|
|
err = downloadFile(tarLocation, url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:33:48 +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
|
|
|
|
}
|
|
|
|
|
2018-02-21 08:41:25 +00:00
|
|
|
func getPkgbuilds(pkgs []string) error {
|
2018-02-17 17:44:30 +00:00
|
|
|
//possibleAurs := make([]string, 0, 0)
|
2017-08-02 17:24:03 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-02-17 17:44:30 +00:00
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 17:44:30 +00:00
|
|
|
missing, err := getPkgbuildsfromABS(pkgs, wd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 17:44:30 +00:00
|
|
|
err = getPkgbuildsfromAUR(missing, wd)
|
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the ABS.
|
2018-02-17 17:44:30 +00:00
|
|
|
func getPkgbuildsfromABS(pkgs []string, path string) (missing []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
|
|
|
|
}
|
|
|
|
|
2018-02-21 08:41:25 +00:00
|
|
|
nextPkg:
|
2018-02-17 17:44:30 +00:00
|
|
|
for _, pkgN := range pkgs {
|
|
|
|
for _, db := range dbList.Slice() {
|
|
|
|
pkg, err := db.PkgByName(pkgN)
|
|
|
|
if err == nil {
|
|
|
|
var url string
|
2018-03-11 23:00:00 +00:00
|
|
|
name := pkg.Base()
|
|
|
|
if name == "" {
|
|
|
|
name = pkg.Name()
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:33:48 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(path, name)); err == nil {
|
|
|
|
fmt.Println(bold(red(arrow)), bold(cyan(name)), "directory already exists")
|
|
|
|
continue nextPkg
|
|
|
|
}
|
|
|
|
|
|
|
|
switch db.Name() {
|
|
|
|
case "core", "extra":
|
|
|
|
url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
|
|
|
|
case "community", "multilib":
|
|
|
|
url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
|
|
|
|
default:
|
|
|
|
fmt.Println(name + " not in standard repositories")
|
2018-03-11 23:00:00 +00:00
|
|
|
continue nextPkg
|
2018-02-17 17:44:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:33:48 +00:00
|
|
|
errD := downloadAndUnpack(url, cacheHome)
|
2018-02-17 17:44:30 +00:00
|
|
|
if errD != nil {
|
2018-05-14 17:33:48 +00:00
|
|
|
fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
|
|
|
|
}
|
|
|
|
|
|
|
|
errD = exec.Command("mv", filepath.Join(cacheHome, "packages", name, "trunk"), filepath.Join(path, name)).Run()
|
|
|
|
if errD != nil {
|
|
|
|
fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
|
|
|
|
} else {
|
|
|
|
fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.Name()), "from ABS")
|
2018-02-17 17:44:30 +00:00
|
|
|
}
|
2018-02-21 08:41:25 +00:00
|
|
|
|
2018-02-17 17:44:30 +00:00
|
|
|
continue nextPkg
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-17 17:44:30 +00:00
|
|
|
|
|
|
|
missing = append(missing, pkgN)
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
2018-02-17 17:44:30 +00:00
|
|
|
|
2018-05-14 17:33:48 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(cacheHome, "packages")); err == nil {
|
|
|
|
os.RemoveAll(filepath.Join(cacheHome, "packages"))
|
|
|
|
}
|
|
|
|
|
2018-02-17 17:44:30 +00:00
|
|
|
return
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the AUR.
|
2018-02-17 17:44:30 +00:00
|
|
|
func getPkgbuildsfromAUR(pkgs []string, dir string) (err error) {
|
2018-04-11 03:43:51 +00:00
|
|
|
aq, err := aurInfoPrint(pkgs)
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-17 17:44:30 +00:00
|
|
|
for _, pkg := range aq {
|
2018-03-28 14:52:20 +00:00
|
|
|
var err error
|
|
|
|
if shouldUseGit(filepath.Join(dir, pkg.PackageBase)) {
|
|
|
|
err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", dir, pkg.PackageBase)
|
|
|
|
} else {
|
2018-05-14 17:33:48 +00:00
|
|
|
err = downloadAndUnpack(baseURL+aq[0].URLPath, dir)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
2018-05-14 17:33:48 +00:00
|
|
|
fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.PackageBase), "from AUR")
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 12:45:11 +00:00
|
|
|
if len(aq) != len(pkgs) {
|
|
|
|
return fmt.Errorf("Could not find all required packages")
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
return
|
|
|
|
}
|