2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
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-01-20 18:48:40 +01:00
|
|
|
func createAURList(out *os.File, shell string) (err error) {
|
2017-08-02 19:24:03 +02:00
|
|
|
resp, err := http.Get("https://aur.archlinux.org/packages.gz")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
for scanner.Scan() {
|
|
|
|
fmt.Print(scanner.Text())
|
|
|
|
out.WriteString(scanner.Text())
|
2018-01-20 18:48:40 +01:00
|
|
|
if shell == "fish" {
|
2017-08-02 19:24:03 +02:00
|
|
|
fmt.Print("\tAUR\n")
|
|
|
|
out.WriteString("\tAUR\n")
|
|
|
|
} else {
|
|
|
|
fmt.Print("\n")
|
|
|
|
out.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//CreatePackageList appends Repo packages to completion cache
|
2018-01-20 18:48:40 +01:00
|
|
|
func createRepoList(out *os.File, shell string) (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 {
|
|
|
|
fmt.Print(pkg.Name())
|
|
|
|
out.WriteString(pkg.Name())
|
2018-01-20 18:48:40 +01:00
|
|
|
if shell == "fish" {
|
2017-08-02 19:24:03 +02:00
|
|
|
fmt.Print("\t" + pkg.DB().Name() + "\n")
|
|
|
|
out.WriteString("\t" + pkg.DB().Name() + "\n")
|
|
|
|
} else {
|
|
|
|
fmt.Print("\n")
|
|
|
|
out.WriteString("\n")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-20 18:48:40 +01:00
|
|
|
|
|
|
|
// Complete provides completion info for shells
|
|
|
|
func complete(shell string) error {
|
|
|
|
var path string
|
|
|
|
|
|
|
|
if shell == "fish" {
|
2018-04-17 02:32:31 +02:00
|
|
|
path = filepath.Join(completionFile, "fish"+".cache")
|
2018-01-20 18:48:40 +01:00
|
|
|
} else {
|
2018-04-17 02:32:31 +02:00
|
|
|
path = filepath.Join(completionFile, "sh"+".cache")
|
2018-01-20 18:48:40 +01:00
|
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
|
|
|
|
os.MkdirAll(filepath.Dir(completionFile), 0755)
|
|
|
|
out, errf := os.Create(path)
|
|
|
|
if errf != nil {
|
|
|
|
return errf
|
|
|
|
}
|
|
|
|
|
|
|
|
if createAURList(out, shell) != nil {
|
|
|
|
defer os.Remove(path)
|
|
|
|
}
|
|
|
|
erra := createRepoList(out, shell)
|
|
|
|
|
|
|
|
out.Close()
|
|
|
|
return erra
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|