2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-01-20 18:48:40 +01:00
|
|
|
"io"
|
2017-08-02 19:24:03 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-01-20 18:48:40 +01:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2017-08-04 11:26:53 +02:00
|
|
|
|
|
|
|
alpm "github.com/jguer/go-alpm"
|
2017-08-02 19:24:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//CreateAURList creates a new completion file
|
2018-07-24 15:16:12 +02:00
|
|
|
func createAURList(out *os.File) (err error) {
|
2018-08-19 06:53:32 +02:00
|
|
|
resp, err := http.Get(config.AURURL + "/packages.gz")
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
for scanner.Scan() {
|
|
|
|
out.WriteString(scanner.Text())
|
2018-07-24 15:16:12 +02:00
|
|
|
out.WriteString("\tAUR\n")
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//CreatePackageList appends Repo packages to completion cache
|
2018-07-24 15:16:12 +02:00
|
|
|
func createRepoList(out *os.File) (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
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = dbList.ForEach(func(db alpm.Db) error {
|
|
|
|
_ = db.PkgCache().ForEach(func(pkg alpm.Package) error {
|
|
|
|
out.WriteString(pkg.Name())
|
2018-07-24 15:16:12 +02:00
|
|
|
out.WriteString("\t" + pkg.DB().Name() + "\n")
|
2017-08-02 19:24:03 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-20 18:48:40 +01:00
|
|
|
|
2018-07-24 15:16:12 +02:00
|
|
|
func updateCompletion(force bool) error {
|
|
|
|
path := filepath.Join(cacheHome, "completion.cache")
|
2018-01-20 18:48:40 +01:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
|
2018-07-24 15:42:43 +02:00
|
|
|
if os.IsNotExist(err) || (config.CompletionInterval != -1 && time.Since(info.ModTime()).Hours() >= float64(config.CompletionInterval*24)) || force {
|
2018-04-21 17:43:12 +02:00
|
|
|
os.MkdirAll(filepath.Dir(path), 0755)
|
2018-01-20 18:48:40 +01:00
|
|
|
out, errf := os.Create(path)
|
|
|
|
if errf != nil {
|
|
|
|
return errf
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:16:12 +02:00
|
|
|
if createAURList(out) != nil {
|
2018-01-20 18:48:40 +01:00
|
|
|
defer os.Remove(path)
|
|
|
|
}
|
2018-07-24 15:16:12 +02:00
|
|
|
erra := createRepoList(out)
|
2018-01-20 18:48:40 +01:00
|
|
|
|
|
|
|
out.Close()
|
|
|
|
return erra
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:16:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete provides completion info for shells
|
|
|
|
func complete(force bool) error {
|
|
|
|
path := filepath.Join(cacheHome, "completion.cache")
|
|
|
|
|
|
|
|
err := updateCompletion(force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:48:40 +01:00
|
|
|
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(os.Stdout, in)
|
|
|
|
return err
|
|
|
|
}
|