chore(http): use client for pkg completions

This commit is contained in:
jguer 2021-05-16 22:45:36 +02:00 committed by J Guerreiro
parent c7cf7baa48
commit a37f4efd73
4 changed files with 23 additions and 12 deletions

6
cmd.go
View file

@ -245,9 +245,11 @@ func handlePrint(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
quiet := cmdArgs.ExistsArg("q", "quiet") quiet := cmdArgs.ExistsArg("q", "quiet")
return news.PrintNewsFeed(config.Runtime.HTTPClient, dbExecutor.LastBuildTime(), config.SortMode, double, quiet) return news.PrintNewsFeed(config.Runtime.HTTPClient, dbExecutor.LastBuildTime(), config.SortMode, double, quiet)
case cmdArgs.ExistsDouble("c", "complete"): case cmdArgs.ExistsDouble("c", "complete"):
return completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, true) return completion.Show(config.Runtime.HTTPClient, dbExecutor,
config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, true)
case cmdArgs.ExistsArg("c", "complete"): case cmdArgs.ExistsArg("c", "complete"):
return completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false) return completion.Show(config.Runtime.HTTPClient, dbExecutor,
config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false)
case cmdArgs.ExistsArg("s", "stats"): case cmdArgs.ExistsArg("s", "stats"):
return localStatistics(dbExecutor) return localStatistics(dbExecutor)
} }

View file

@ -351,7 +351,8 @@ func install(cmdArgs *settings.Arguments, dbExecutor db.Executor, ignoreProvider
} }
go func() { go func() {
_ = completion.Update(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false) _ = completion.Update(config.Runtime.HTTPClient, dbExecutor,
config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false)
}() }()
err = downloadPkgbuildsSources(do.Aur, incompatible) err = downloadPkgbuildsSources(do.Aur, incompatible)

View file

@ -2,6 +2,7 @@ package completion
import ( import (
"bufio" "bufio"
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -20,8 +21,8 @@ type PkgSynchronizer interface {
} }
// Show provides completion info for shells // Show provides completion info for shells
func Show(dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error { func Show(httpClient *http.Client, dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error {
err := Update(dbExecutor, aurURL, completionPath, interval, force) err := Update(httpClient, dbExecutor, aurURL, completionPath, interval, force)
if err != nil { if err != nil {
return err return err
} }
@ -37,7 +38,7 @@ func Show(dbExecutor PkgSynchronizer, aurURL, completionPath string, interval in
} }
// Update updates completion cache to be used by Complete // Update updates completion cache to be used by Complete
func Update(dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error { func Update(httpClient *http.Client, dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error {
info, err := os.Stat(completionPath) info, err := os.Stat(completionPath)
if os.IsNotExist(err) || (interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24)) || force { if os.IsNotExist(err) || (interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24)) || force {
@ -50,7 +51,7 @@ func Update(dbExecutor PkgSynchronizer, aurURL, completionPath string, interval
return errf return errf
} }
if createAURList(aurURL, out) != nil { if createAURList(httpClient, aurURL, out) != nil {
defer os.Remove(completionPath) defer os.Remove(completionPath)
} }
@ -64,13 +65,19 @@ func Update(dbExecutor PkgSynchronizer, aurURL, completionPath string, interval
} }
// CreateAURList creates a new completion file // CreateAURList creates a new completion file
func createAURList(aurURL string, out io.Writer) error { func createAURList(client *http.Client, aurURL string, out io.Writer) error {
u, err := url.Parse(aurURL) u, err := url.Parse(aurURL)
if err != nil { if err != nil {
return err return err
} }
u.Path = path.Join(u.Path, "packages.gz") u.Path = path.Join(u.Path, "packages.gz")
resp, err := http.Get(u.String())
req, err := http.NewRequestWithContext(context.Background(), "GET", u.String(), nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil { if err != nil {
return err return err
} }

View file

@ -3,6 +3,7 @@ package completion
import ( import (
"bytes" "bytes"
"errors" "errors"
"net/http"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -39,7 +40,7 @@ func Test_createAURList(t *testing.T) {
Reply(200). Reply(200).
BodyString(samplePackageResp) BodyString(samplePackageResp)
out := &bytes.Buffer{} out := &bytes.Buffer{}
err := createAURList("https://aur.archlinux.org", out) err := createAURList(&http.Client{}, "https://aur.archlinux.org", out)
assert.NoError(t, err) assert.NoError(t, err)
gotOut := out.String() gotOut := out.String()
assert.Equal(t, expectPackageCompletion, gotOut) assert.Equal(t, expectPackageCompletion, gotOut)
@ -52,7 +53,7 @@ func Test_createAURListHTTPError(t *testing.T) {
Get("/packages.gz"). Get("/packages.gz").
ReplyError(errors.New("Not available")) ReplyError(errors.New("Not available"))
out := &bytes.Buffer{} out := &bytes.Buffer{}
err := createAURList("https://aur.archlinux.org", out) err := createAURList(&http.Client{}, "https://aur.archlinux.org", out)
assert.EqualError(t, err, "Get \"https://aur.archlinux.org/packages.gz\": Not available") assert.EqualError(t, err, "Get \"https://aur.archlinux.org/packages.gz\": Not available")
} }
@ -64,6 +65,6 @@ func Test_createAURListStatusError(t *testing.T) {
Reply(503). Reply(503).
BodyString(samplePackageResp) BodyString(samplePackageResp)
out := &bytes.Buffer{} out := &bytes.Buffer{}
err := createAURList("https://aur.archlinux.org", out) err := createAURList(&http.Client{}, "https://aur.archlinux.org", out)
assert.EqualError(t, err, "invalid status code: 503") assert.EqualError(t, err, "invalid status code: 503")
} }