refactor: unexport internal identifiers and fix naming nits
This commit is contained in:
@@ -51,6 +51,6 @@ func TestDecryptBlowfishRoundTrip(t *testing.T) {
|
||||
|
||||
func TestDecryptBlowfishInvalidKey(t *testing.T) {
|
||||
if _, err := DecryptBlowfish(make([]byte, 8), nil); err == nil {
|
||||
t.Error("expected error for empty key")
|
||||
t.Error("expected error for nil key")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func (c *Client) FetchResource(ctx context.Context, kind Kind, id string) (Resou
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://www.deezer.com/ajax/gw-light.php?method=deezer.page%s&input=3&api_version=1.0&api_token=%s", kind.pageMethod(), c.Session.APIToken)
|
||||
url := fmt.Sprintf("https://www.deezer.com/ajax/gw-light.php?method=deezer.page%s&input=3&api_version=1.0&api_token=%s", kind.pageMethod(), c.Session.apiToken)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -132,7 +132,7 @@ func (c *Client) FetchMedia(ctx context.Context, track *Track, quality string) (
|
||||
"flac": `[{"cipher":"BF_CBC_STRIPE","format":"FLAC"},{"cipher":"BF_CBC_STRIPE","format":"MP3_320"},{"cipher":"BF_CBC_STRIPE","format":"MP3_128"}]`,
|
||||
}
|
||||
|
||||
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":%s}],"track_tokens":["%s"]}`, c.Session.LicenseToken, qualityFormats[quality], track.TrackToken)
|
||||
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":%s}],"track_tokens":["%s"]}`, c.Session.licenseToken, qualityFormats[quality], track.TrackToken)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://media.deezer.com/v1/get_url", bytes.NewBuffer([]byte(reqBody)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -19,7 +19,7 @@ type Credentials struct {
|
||||
ARL string `json:"arl,omitempty"`
|
||||
}
|
||||
|
||||
func LoadCredentials() (*Credentials, error) {
|
||||
func loadCredentials() (*Credentials, error) {
|
||||
secret, err := keyring.Get(keyringService, keyringUser)
|
||||
if err != nil {
|
||||
if errors.Is(err, keyring.ErrNotFound) {
|
||||
@@ -36,7 +36,7 @@ func LoadCredentials() (*Credentials, error) {
|
||||
return &creds, nil
|
||||
}
|
||||
|
||||
func SaveCredentials(creds *Credentials) error {
|
||||
func saveCredentials(creds *Credentials) error {
|
||||
data, err := json.Marshal(creds)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func resolveARL(ctx context.Context, validate func(ctx context.Context, arl string) error) (string, error) {
|
||||
creds, err := LoadCredentials()
|
||||
creds, err := loadCredentials()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func Login(ctx context.Context, email, password string) (string, string, error)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
if err := SaveCredentials(creds); err != nil {
|
||||
if err := saveCredentials(creds); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
var ErrInvalidARL = errors.New("invalid or expired ARL cookie")
|
||||
|
||||
type Session struct {
|
||||
APIToken string
|
||||
LicenseToken string
|
||||
apiToken string
|
||||
licenseToken string
|
||||
HTTPClient *http.Client
|
||||
Premium bool
|
||||
}
|
||||
@@ -79,8 +79,8 @@ func authenticate(ctx context.Context, arlCookie string) (*Session, error) {
|
||||
|
||||
opts := res.Results.User.Options
|
||||
return &Session{
|
||||
APIToken: res.Results.APIToken,
|
||||
LicenseToken: opts.LicenseToken,
|
||||
apiToken: res.Results.APIToken,
|
||||
licenseToken: opts.LicenseToken,
|
||||
HTTPClient: client,
|
||||
Premium: opts.MobileOffline || opts.WebOffline,
|
||||
}, nil
|
||||
|
||||
@@ -69,17 +69,17 @@ func (t *Track) Filename(kind Kind, mediaFormat string) string {
|
||||
return base + "." + ext
|
||||
}
|
||||
|
||||
func truncateBytes(s string, max int) string {
|
||||
if max <= 0 {
|
||||
func truncateBytes(s string, maxLen int) string {
|
||||
if maxLen <= 0 {
|
||||
return ""
|
||||
}
|
||||
if len(s) <= max {
|
||||
if len(s) <= maxLen {
|
||||
return s
|
||||
}
|
||||
|
||||
last := 0
|
||||
for i := range s {
|
||||
if i > max {
|
||||
if i > maxLen {
|
||||
return s[:last]
|
||||
}
|
||||
last = i
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
neturl "net/url"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -38,7 +38,7 @@ func fetchBPM(ctx context.Context, httpClient *http.Client, artist, title, durat
|
||||
func findTrackURL(ctx context.Context, httpClient *http.Client, artist, title, duration string) (string, error) {
|
||||
const rootURL = "https://songbpm.com"
|
||||
|
||||
values := neturl.Values{}
|
||||
values := url.Values{}
|
||||
values.Add("query", fmt.Sprintf("%s %s", artist, title))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, rootURL+"/searches", bytes.NewBufferString(values.Encode()))
|
||||
@@ -109,8 +109,8 @@ func findTrackURL(ctx context.Context, httpClient *http.Client, artist, title, d
|
||||
return rootURL + matchURL, nil
|
||||
}
|
||||
|
||||
func fetchBPMPage(ctx context.Context, httpClient *http.Client, url string) (string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
func fetchBPMPage(ctx context.Context, httpClient *http.Client, pageURL string) (string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pageURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ func fetchGenre(ctx context.Context, httpClient *http.Client, artist, title stri
|
||||
return formatTags(filtered), nil
|
||||
}
|
||||
|
||||
func fetchGenrePage(ctx context.Context, httpClient *http.Client, url string) (*goquery.Document, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
func fetchGenrePage(ctx context.Context, httpClient *http.Client, pageURL string) (*goquery.Document, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pageURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ type metadataResult struct {
|
||||
warnings []string
|
||||
}
|
||||
|
||||
func fetchMetadata(httpClient *http.Client, ctx context.Context, track *deezer.Track, opts Options) metadataResult {
|
||||
func fetchMetadata(ctx context.Context, httpClient *http.Client, track *deezer.Track, opts Options) metadataResult {
|
||||
if !opts.BPM && !opts.Genre {
|
||||
return metadataResult{}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (d *Downloader) downloadTrack(ctx context.Context, resource deezer.Resource
|
||||
|
||||
metadataChan := make(chan metadataResult, 1)
|
||||
go func() {
|
||||
metadataChan <- fetchMetadata(d.deezerClient.Session.HTTPClient, ctx, track, opts)
|
||||
metadataChan <- fetchMetadata(ctx, d.deezerClient.Session.HTTPClient, track, opts)
|
||||
}()
|
||||
|
||||
dlCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
|
||||
|
||||
@@ -27,7 +27,7 @@ var managedPrefixes = []string{
|
||||
|
||||
func resolveTarget() (string, error) {
|
||||
if buildinfo.IsDev() {
|
||||
return "", fmt.Errorf("development build cannot self-update. Install a release from https://github.com/%s/%s/releases",
|
||||
return "", fmt.Errorf("development build cannot self-update; install a release from https://github.com/%s/%s/releases",
|
||||
repoOwner, repoName)
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func resolveTarget() (string, error) {
|
||||
|
||||
for _, prefix := range managedPrefixes {
|
||||
if target == prefix || strings.HasPrefix(target, prefix+"/") {
|
||||
return "", fmt.Errorf("%s was installed by a package manager. Update it with that instead", target)
|
||||
return "", fmt.Errorf("%s was installed by a package manager; update it with that instead", target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,12 +59,12 @@ func CheckUpdatable() error {
|
||||
func checkWritable(dir string) error {
|
||||
f, err := os.CreateTemp(dir, tmpPattern)
|
||||
if err != nil {
|
||||
hint := "Re-run with sudo"
|
||||
hint := "re-run with sudo"
|
||||
if runtime.GOOS == "windows" {
|
||||
hint = "Re-run from an elevated prompt"
|
||||
hint = "re-run from an elevated prompt"
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot write to %s: %w. %s", dir, err, hint)
|
||||
return fmt.Errorf("cannot write to %s: %w; %s", dir, err, hint)
|
||||
}
|
||||
|
||||
name := f.Name()
|
||||
@@ -198,7 +198,7 @@ func (u *Updater) replaceBinary(target, tmp string) error {
|
||||
|
||||
if err := os.Rename(tmp, target); err != nil {
|
||||
if rollbackErr := os.Rename(old, target); rollbackErr != nil {
|
||||
return fmt.Errorf("failed to install the new binary: %w. The previous one could not be restored from %s: %v",
|
||||
return fmt.Errorf("failed to install the new binary: %w; the previous one could not be restored from %s: %v",
|
||||
err, old, rollbackErr)
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ func writeCache(version string) error {
|
||||
|
||||
func check(ctx context.Context) (string, error) {
|
||||
if entry, ok := readCache(); ok {
|
||||
return newerThanCurrent(entry.LatestVersion), nil
|
||||
return latestIfNewer(entry.LatestVersion), nil
|
||||
}
|
||||
|
||||
release, err := New().Latest(ctx)
|
||||
@@ -84,10 +84,10 @@ func check(ctx context.Context) (string, error) {
|
||||
latest := release.Version()
|
||||
_ = writeCache(latest)
|
||||
|
||||
return newerThanCurrent(latest), nil
|
||||
return latestIfNewer(latest), nil
|
||||
}
|
||||
|
||||
func newerThanCurrent(latest string) string {
|
||||
func latestIfNewer(latest string) string {
|
||||
if IsNewer(buildinfo.Version(), latest) {
|
||||
return latest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user