2019-10-05 03:02:30 +02:00
|
|
|
package completion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-06-13 18:47:42 +02:00
|
|
|
"net/url"
|
2019-10-05 03:02:30 +02:00
|
|
|
"os"
|
2020-06-13 18:47:42 +02:00
|
|
|
"path"
|
2019-10-05 03:02:30 +02:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
alpm "github.com/Jguer/go-alpm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Show provides completion info for shells
|
2020-07-05 03:17:35 +02:00
|
|
|
func Show(alpmHandle *alpm.Handle, aurURL, completionPath string, interval int, force bool) error {
|
|
|
|
err := Update(alpmHandle, aurURL, completionPath, interval, force)
|
2019-10-05 03:02:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-06 02:02:12 +02:00
|
|
|
in, err := os.OpenFile(completionPath, os.O_RDWR|os.O_CREATE, 0o644)
|
2019-10-05 03:02:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(os.Stdout, in)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates completion cache to be used by Complete
|
2020-07-05 03:17:35 +02:00
|
|
|
func Update(alpmHandle *alpm.Handle, aurURL, completionPath string, interval int, force bool) error {
|
2020-06-13 18:47:42 +02:00
|
|
|
info, err := os.Stat(completionPath)
|
2019-10-05 03:02:30 +02:00
|
|
|
|
|
|
|
if os.IsNotExist(err) || (interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24)) || force {
|
2020-07-06 02:02:12 +02:00
|
|
|
errd := os.MkdirAll(filepath.Dir(completionPath), 0o755)
|
2019-10-13 13:15:51 +02:00
|
|
|
if errd != nil {
|
|
|
|
return errd
|
|
|
|
}
|
2020-06-13 18:47:42 +02:00
|
|
|
out, errf := os.Create(completionPath)
|
2019-10-05 03:02:30 +02:00
|
|
|
if errf != nil {
|
|
|
|
return errf
|
|
|
|
}
|
|
|
|
|
|
|
|
if createAURList(aurURL, out) != nil {
|
2020-06-13 18:47:42 +02:00
|
|
|
defer os.Remove(completionPath)
|
2019-10-05 03:02:30 +02:00
|
|
|
}
|
|
|
|
erra := createRepoList(alpmHandle, out)
|
|
|
|
|
|
|
|
out.Close()
|
|
|
|
return erra
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
// CreateAURList creates a new completion file
|
2019-10-16 01:03:58 +02:00
|
|
|
func createAURList(aurURL string, out io.Writer) error {
|
2020-06-13 18:47:42 +02:00
|
|
|
u, err := url.Parse(aurURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, "packages.gz")
|
|
|
|
resp, err := http.Get(u.String())
|
2019-10-05 03:02:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
for scanner.Scan() {
|
|
|
|
_, err = io.WriteString(out, scanner.Text()+"\tAUR\n")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
// CreatePackageList appends Repo packages to completion cache
|
2019-10-16 01:03:58 +02:00
|
|
|
func createRepoList(alpmHandle *alpm.Handle, out io.Writer) error {
|
2019-10-05 03:02:30 +02:00
|
|
|
dbList, err := alpmHandle.SyncDBs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = dbList.ForEach(func(db alpm.DB) error {
|
|
|
|
_ = db.PkgCache().ForEach(func(pkg alpm.Package) error {
|
|
|
|
_, err = io.WriteString(out, pkg.Name()+"\t"+pkg.DB().Name()+"\n")
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|