mirror of
https://github.com/Jguer/yay
synced 2024-08-31 05:39:29 +00:00
Merge pull request #1046 from Jguer/module-completion
Move completion to submodule
This commit is contained in:
commit
769aee728a
2
Makefile
2
Makefile
|
@ -33,7 +33,7 @@ clean:
|
|||
|
||||
.PHONY: test
|
||||
test: test-vendor
|
||||
$(GO) vet .
|
||||
$(GO) vet ./...
|
||||
@test -z "$$(gofmt -l *.go)" || (echo "Files need to be linted. Use make fmt" && false)
|
||||
$(GO) test -mod=vendor --race -covermode=atomic -v . ./pkg/...
|
||||
|
||||
|
|
5
cmd.go
5
cmd.go
|
@ -7,6 +7,7 @@ import (
|
|||
"os"
|
||||
|
||||
alpm "github.com/Jguer/go-alpm"
|
||||
"github.com/Jguer/yay/v9/pkg/completion"
|
||||
)
|
||||
|
||||
var cmdArgs = makeArguments()
|
||||
|
@ -205,9 +206,9 @@ func handlePrint() (err error) {
|
|||
case cmdArgs.existsArg("w", "news"):
|
||||
err = printNewsFeed()
|
||||
case cmdArgs.existsDouble("c", "complete"):
|
||||
complete(true)
|
||||
err = completion.Show(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, true)
|
||||
case cmdArgs.existsArg("c", "complete"):
|
||||
complete(false)
|
||||
err = completion.Show(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, false)
|
||||
case cmdArgs.existsArg("s", "stats"):
|
||||
err = localStatistics()
|
||||
default:
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
alpm "github.com/Jguer/go-alpm"
|
||||
)
|
||||
|
||||
//CreateAURList creates a new completion file
|
||||
func createAURList(out *os.File) (err error) {
|
||||
resp, err := http.Get(config.AURURL + "/packages.gz")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
|
||||
scanner.Scan()
|
||||
for scanner.Scan() {
|
||||
out.WriteString(scanner.Text())
|
||||
out.WriteString("\tAUR\n")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//CreatePackageList appends Repo packages to completion cache
|
||||
func createRepoList(out *os.File) (err error) {
|
||||
dbList, err := alpmHandle.SyncDBs()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_ = dbList.ForEach(func(db alpm.DB) error {
|
||||
_ = db.PkgCache().ForEach(func(pkg alpm.Package) error {
|
||||
out.WriteString(pkg.Name())
|
||||
out.WriteString("\t" + pkg.DB().Name() + "\n")
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateCompletion(force bool) error {
|
||||
path := filepath.Join(cacheHome, "completion.cache")
|
||||
info, err := os.Stat(path)
|
||||
|
||||
if os.IsNotExist(err) || (config.CompletionInterval != -1 && time.Since(info.ModTime()).Hours() >= float64(config.CompletionInterval*24)) || force {
|
||||
os.MkdirAll(filepath.Dir(path), 0755)
|
||||
out, errf := os.Create(path)
|
||||
if errf != nil {
|
||||
return errf
|
||||
}
|
||||
|
||||
if createAURList(out) != nil {
|
||||
defer os.Remove(path)
|
||||
}
|
||||
erra := createRepoList(out)
|
||||
|
||||
out.Close()
|
||||
return erra
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
|
@ -10,6 +10,7 @@ import (
|
|||
"sync"
|
||||
|
||||
alpm "github.com/Jguer/go-alpm"
|
||||
"github.com/Jguer/yay/v9/pkg/completion"
|
||||
gosrc "github.com/Morganamilo/go-srcinfo"
|
||||
)
|
||||
|
||||
|
@ -315,7 +316,7 @@ func install(parser *arguments) (err error) {
|
|||
}
|
||||
}
|
||||
|
||||
go updateCompletion(false)
|
||||
go completion.Update(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, false)
|
||||
|
||||
err = downloadPkgbuildsSources(do.Aur, incompatible)
|
||||
if err != nil {
|
||||
|
|
93
pkg/completion/completion.go
Normal file
93
pkg/completion/completion.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package completion
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
alpm "github.com/Jguer/go-alpm"
|
||||
)
|
||||
|
||||
// Show provides completion info for shells
|
||||
func Show(alpmHandle *alpm.Handle, aurURL string, cacheDir string, interval int, force bool) error {
|
||||
path := filepath.Join(cacheDir, "completion.cache")
|
||||
|
||||
err := Update(alpmHandle, aurURL, cacheDir, interval, force)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Update updates completion cache to be used by Complete
|
||||
func Update(alpmHandle *alpm.Handle, aurURL string, cacheDir string, interval int, force bool) error {
|
||||
path := filepath.Join(cacheDir, "completion.cache")
|
||||
info, err := os.Stat(path)
|
||||
|
||||
if os.IsNotExist(err) || (interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24)) || force {
|
||||
os.MkdirAll(filepath.Dir(path), 0755)
|
||||
out, errf := os.Create(path)
|
||||
if errf != nil {
|
||||
return errf
|
||||
}
|
||||
|
||||
if createAURList(aurURL, out) != nil {
|
||||
defer os.Remove(path)
|
||||
}
|
||||
erra := createRepoList(alpmHandle, out)
|
||||
|
||||
out.Close()
|
||||
return erra
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//CreateAURList creates a new completion file
|
||||
func createAURList(aurURL string, out io.ReadWriteSeeker) error {
|
||||
resp, err := http.Get(aurURL + "/packages.gz")
|
||||
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
|
||||
}
|
||||
|
||||
//CreatePackageList appends Repo packages to completion cache
|
||||
func createRepoList(alpmHandle *alpm.Handle, out io.ReadWriteSeeker) error {
|
||||
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
|
||||
}
|
Loading…
Reference in a new issue