2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-03-28 16:52:20 +02:00
|
|
|
"path/filepath"
|
2018-05-31 21:31:45 +02:00
|
|
|
"strings"
|
2017-08-02 19:24:03 +02:00
|
|
|
)
|
|
|
|
|
2018-03-28 16:52:20 +02: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-05-31 21:31:45 +02:00
|
|
|
return err == nil || os.IsExist(err)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:24:03 +02: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-06-10 02:57:17 +02:00
|
|
|
func gitHasDiff(path string, name string) (bool, error) {
|
2018-07-19 19:37:28 +02:00
|
|
|
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
|
2018-05-31 21:31:45 +02:00
|
|
|
if err != nil {
|
2018-06-10 02:57:17 +02:00
|
|
|
return false, fmt.Errorf("%s%s", stderr, err)
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 19:37:28 +02:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
head := lines[0]
|
|
|
|
upstream := lines[1]
|
2018-06-10 02:57:17 +02:00
|
|
|
|
|
|
|
return head != upstream, nil
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
func gitDownload(url string, path string, name string) (bool, error) {
|
2018-03-28 16:52:20 +02:00
|
|
|
_, err := os.Stat(filepath.Join(path, name, ".git"))
|
|
|
|
if os.IsNotExist(err) {
|
2018-08-03 00:07:36 +02:00
|
|
|
cmd := passToGit(path, "clone", "--no-progress", url, name)
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return false, fmt.Errorf("error cloning %s: stderr", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
return true, nil
|
2018-03-28 16:52:20 +02:00
|
|
|
} else if err != nil {
|
2018-06-10 04:41:25 +02:00
|
|
|
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 00:07:36 +02:00
|
|
|
cmd := passToGit(filepath.Join(path, name), "fetch")
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return false, fmt.Errorf("error fetching %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
return false, nil
|
2018-06-10 01:42:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 15:14:16 +02:00
|
|
|
func gitMerge(path string, name string) error {
|
2018-08-02 17:11:37 +02:00
|
|
|
_, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return fmt.Errorf("error resetting %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:11:37 +02:00
|
|
|
_, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return fmt.Errorf("error merging %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:31:45 +02:00
|
|
|
func gitDiff(path string, name string) error {
|
2018-07-19 19:37:28 +02:00
|
|
|
err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))
|
2018-05-31 21:31:45 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// DownloadAndUnpack downloads url tgz and extracts to path.
|
2018-05-14 19:33:48 +02:00
|
|
|
func downloadAndUnpack(url string, path string) (err error) {
|
2017-08-02 19:24:03 +02:00
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:33:48 +02:00
|
|
|
fileName := filepath.Base(url)
|
2017-08-02 19:24:03 +02:00
|
|
|
|
2018-04-17 02:32:31 +02:00
|
|
|
tarLocation := filepath.Join(path, fileName)
|
2017-08-02 19:24:03 +02:00
|
|
|
defer os.Remove(tarLocation)
|
|
|
|
|
|
|
|
err = downloadFile(tarLocation, url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:33:48 +02:00
|
|
|
err = exec.Command(config.TarBin, "-xf", tarLocation, "-C", path).Run()
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:41:25 +01:00
|
|
|
func getPkgbuilds(pkgs []string) error {
|
2018-06-01 06:04:15 +02:00
|
|
|
missing := false
|
2017-08-02 19:24:03 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-02-17 18:44:30 +01:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 06:04:15 +02:00
|
|
|
pkgs = removeInvalidTargets(pkgs)
|
|
|
|
|
|
|
|
aur, repo, err := packageSlices(pkgs)
|
|
|
|
|
|
|
|
if len(repo) > 0 {
|
|
|
|
missing, err = getPkgbuildsfromABS(repo, wd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(aur) > 0 {
|
|
|
|
_missing, err := getPkgbuildsfromAUR(aur, wd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
missing = missing || _missing
|
|
|
|
}
|
|
|
|
|
|
|
|
if missing {
|
|
|
|
err = fmt.Errorf("")
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the ABS.
|
2018-06-02 14:52:18 +02:00
|
|
|
func getPkgbuildsfromABS(pkgs []string, path string) (missing bool, err error) {
|
2017-10-18 04:38:19 +02:00
|
|
|
dbList, err := alpmHandle.SyncDbs()
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:41:25 +01:00
|
|
|
nextPkg:
|
2018-02-17 18:44:30 +01:00
|
|
|
for _, pkgN := range pkgs {
|
2018-06-01 06:04:15 +02:00
|
|
|
pkgDb, name := splitDbFromName(pkgN)
|
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
for _, db := range dbList.Slice() {
|
2018-06-01 06:04:15 +02:00
|
|
|
if pkgDb != "" && db.Name() != pkgDb {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg, err := db.PkgByName(name)
|
2018-02-17 18:44:30 +01:00
|
|
|
if err == nil {
|
|
|
|
var url string
|
2018-03-12 00:00:00 +01:00
|
|
|
name := pkg.Base()
|
|
|
|
if name == "" {
|
|
|
|
name = pkg.Name()
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:33:48 +02: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:
|
2018-06-01 06:04:15 +02:00
|
|
|
fmt.Println(pkgN, "not in standard repositories")
|
2018-03-12 00:00:00 +01:00
|
|
|
continue nextPkg
|
2018-02-17 18:44:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 19:33:48 +02:00
|
|
|
errD := downloadAndUnpack(url, cacheHome)
|
2018-02-17 18:44:30 +01:00
|
|
|
if errD != nil {
|
2018-05-14 19:33:48 +02: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 18:44:30 +01:00
|
|
|
}
|
2018-02-21 09:41:25 +01:00
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
continue nextPkg
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-06-01 06:04:15 +02:00
|
|
|
fmt.Println(pkgN, "could not find package in database")
|
|
|
|
missing = true
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-05-14 19:33:48 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(cacheHome, "packages")); err == nil {
|
|
|
|
os.RemoveAll(filepath.Join(cacheHome, "packages"))
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
return
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the AUR.
|
2018-06-01 06:04:15 +02:00
|
|
|
func getPkgbuildsfromAUR(pkgs []string, dir string) (bool, error) {
|
|
|
|
missing := false
|
|
|
|
strippedPkgs := make([]string, 0)
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
_, name := splitDbFromName(pkg)
|
|
|
|
strippedPkgs = append(strippedPkgs, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
aq, err := aurInfoPrint(strippedPkgs)
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-06-01 06:04:15 +02:00
|
|
|
return missing, err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
for _, pkg := range aq {
|
2018-06-01 06:04:15 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(dir, pkg.PackageBase)); err == nil {
|
|
|
|
fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name)), "directory already exists")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-03-28 16:52:20 +02:00
|
|
|
if shouldUseGit(filepath.Join(dir, pkg.PackageBase)) {
|
2018-06-10 04:41:25 +02:00
|
|
|
_, err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", dir, pkg.PackageBase)
|
2018-03-28 16:52:20 +02:00
|
|
|
} else {
|
2018-05-14 19:33:48 +02:00
|
|
|
err = downloadAndUnpack(baseURL+aq[0].URLPath, dir)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
2018-05-14 19:33:48 +02:00
|
|
|
fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.PackageBase), "from AUR")
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-05-28 14:45:11 +02:00
|
|
|
if len(aq) != len(pkgs) {
|
2018-06-01 06:04:15 +02:00
|
|
|
missing = true
|
2018-05-28 14:45:11 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 06:04:15 +02:00
|
|
|
return missing, err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|