yay/pkg/completion/completion.go

125 lines
2.5 KiB
Go
Raw Normal View History

2019-10-05 03:02:30 +02:00
package completion
import (
"bufio"
"context"
2020-07-25 00:55:46 +02:00
"fmt"
2019-10-05 03:02:30 +02:00
"io"
"net/http"
"net/url"
2019-10-05 03:02:30 +02:00
"os"
"path"
2019-10-05 03:02:30 +02:00
"path/filepath"
2020-07-25 00:55:46 +02:00
"strings"
2019-10-05 03:02:30 +02:00
"time"
2021-09-08 22:28:08 +02:00
"github.com/Jguer/yay/v11/pkg/db"
2019-10-05 03:02:30 +02:00
)
2021-02-16 15:32:31 +01:00
type PkgSynchronizer interface {
SyncPackages(...string) []db.IPackage
}
2021-08-11 20:13:28 +02:00
// Show provides completion info for shells.
2021-08-12 18:56:23 +02:00
func Show(ctx context.Context, httpClient *http.Client,
dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error {
err := Update(ctx, httpClient, dbExecutor, aurURL, completionPath, interval, force)
2019-10-05 03:02:30 +02:00
if err != nil {
return err
}
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)
2021-08-11 20:13:28 +02:00
2019-10-05 03:02:30 +02:00
return err
}
2021-08-11 20:13:28 +02:00
// Update updates completion cache to be used by Complete.
2021-08-12 18:56:23 +02:00
func Update(ctx context.Context, httpClient *http.Client,
dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error {
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 {
errd := os.MkdirAll(filepath.Dir(completionPath), 0o755)
if errd != nil {
return errd
}
2021-08-11 20:13:28 +02:00
out, errf := os.Create(completionPath)
2019-10-05 03:02:30 +02:00
if errf != nil {
return errf
}
2021-08-12 18:56:23 +02:00
if createAURList(ctx, httpClient, aurURL, out) != nil {
defer os.Remove(completionPath)
2019-10-05 03:02:30 +02:00
}
2020-07-25 00:55:46 +02:00
2020-08-01 01:20:00 +02:00
erra := createRepoList(dbExecutor, out)
2019-10-05 03:02:30 +02:00
out.Close()
2021-08-11 20:13:28 +02:00
2019-10-05 03:02:30 +02:00
return erra
}
return nil
}
2021-08-11 20:13:28 +02:00
// CreateAURList creates a new completion file.
2021-08-12 18:56:23 +02:00
func createAURList(ctx context.Context, client *http.Client, aurURL string, out io.Writer) error {
u, err := url.Parse(aurURL)
if err != nil {
return err
}
2021-08-11 20:13:28 +02:00
u.Path = path.Join(u.Path, "packages.gz")
2021-08-12 18:56:23 +02:00
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return err
}
resp, err := client.Do(req)
2019-10-05 03:02:30 +02:00
if err != nil {
return err
}
defer resp.Body.Close()
2021-08-11 20:13:28 +02:00
2020-07-25 00:55:46 +02:00
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("invalid status code: %d", resp.StatusCode)
}
2019-10-05 03:02:30 +02:00
scanner := bufio.NewScanner(resp.Body)
scanner.Scan()
2021-08-11 20:13:28 +02:00
2019-10-05 03:02:30 +02:00
for scanner.Scan() {
2020-07-25 00:55:46 +02:00
text := scanner.Text()
if strings.HasPrefix(text, "#") {
continue
}
2021-08-11 20:13:28 +02:00
if _, err := io.WriteString(out, text+"\tAUR\n"); err != nil {
2019-10-05 03:02:30 +02:00
return err
}
}
return nil
}
2021-08-11 20:13:28 +02:00
// CreatePackageList appends Repo packages to completion cache.
2021-02-16 15:32:31 +01:00
func createRepoList(dbExecutor PkgSynchronizer, out io.Writer) error {
2020-08-01 01:20:00 +02:00
for _, pkg := range dbExecutor.SyncPackages() {
_, err := io.WriteString(out, pkg.Name()+"\t"+pkg.DB().Name()+"\n")
if err != nil {
2019-10-05 03:02:30 +02:00
return err
2020-08-01 01:20:00 +02:00
}
}
2021-08-11 20:13:28 +02:00
2019-10-05 03:02:30 +02:00
return nil
}