refactor: unexport internal identifiers and fix naming nits
This commit is contained in:
+2
-2
@@ -15,7 +15,7 @@ import (
|
|||||||
const updateNoticeAnnotation = "godeez:update-notice"
|
const updateNoticeAnnotation = "godeez:update-notice"
|
||||||
|
|
||||||
func Execute(ctx context.Context) error {
|
func Execute(ctx context.Context) error {
|
||||||
root := NewRootCmd()
|
root := newRootCmd()
|
||||||
|
|
||||||
var notice <-chan string
|
var notice <-chan string
|
||||||
if wantsUpdateNotice(root) {
|
if wantsUpdateNotice(root) {
|
||||||
@@ -29,7 +29,7 @@ func Execute(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRootCmd() *cobra.Command {
|
func newRootCmd() *cobra.Command {
|
||||||
root := &cobra.Command{
|
root := &cobra.Command{
|
||||||
Use: "godeez",
|
Use: "godeez",
|
||||||
Short: "GoDeez is a tool to download music from Deezer",
|
Short: "GoDeez is a tool to download music from Deezer",
|
||||||
|
|||||||
@@ -51,6 +51,6 @@ func TestDecryptBlowfishRoundTrip(t *testing.T) {
|
|||||||
|
|
||||||
func TestDecryptBlowfishInvalidKey(t *testing.T) {
|
func TestDecryptBlowfishInvalidKey(t *testing.T) {
|
||||||
if _, err := DecryptBlowfish(make([]byte, 8), nil); err == nil {
|
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
|
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))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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"}]`,
|
"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)))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://media.deezer.com/v1/get_url", bytes.NewBuffer([]byte(reqBody)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type Credentials struct {
|
|||||||
ARL string `json:"arl,omitempty"`
|
ARL string `json:"arl,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadCredentials() (*Credentials, error) {
|
func loadCredentials() (*Credentials, error) {
|
||||||
secret, err := keyring.Get(keyringService, keyringUser)
|
secret, err := keyring.Get(keyringService, keyringUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, keyring.ErrNotFound) {
|
if errors.Is(err, keyring.ErrNotFound) {
|
||||||
@@ -36,7 +36,7 @@ func LoadCredentials() (*Credentials, error) {
|
|||||||
return &creds, nil
|
return &creds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveCredentials(creds *Credentials) error {
|
func saveCredentials(creds *Credentials) error {
|
||||||
data, err := json.Marshal(creds)
|
data, err := json.Marshal(creds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func resolveARL(ctx context.Context, validate func(ctx context.Context, arl string) error) (string, error) {
|
func resolveARL(ctx context.Context, validate func(ctx context.Context, arl string) error) (string, error) {
|
||||||
creds, err := LoadCredentials()
|
creds, err := loadCredentials()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ func Login(ctx context.Context, email, password string) (string, string, error)
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := SaveCredentials(creds); err != nil {
|
if err := saveCredentials(creds); err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ import (
|
|||||||
var ErrInvalidARL = errors.New("invalid or expired ARL cookie")
|
var ErrInvalidARL = errors.New("invalid or expired ARL cookie")
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
APIToken string
|
apiToken string
|
||||||
LicenseToken string
|
licenseToken string
|
||||||
HTTPClient *http.Client
|
HTTPClient *http.Client
|
||||||
Premium bool
|
Premium bool
|
||||||
}
|
}
|
||||||
@@ -79,8 +79,8 @@ func authenticate(ctx context.Context, arlCookie string) (*Session, error) {
|
|||||||
|
|
||||||
opts := res.Results.User.Options
|
opts := res.Results.User.Options
|
||||||
return &Session{
|
return &Session{
|
||||||
APIToken: res.Results.APIToken,
|
apiToken: res.Results.APIToken,
|
||||||
LicenseToken: opts.LicenseToken,
|
licenseToken: opts.LicenseToken,
|
||||||
HTTPClient: client,
|
HTTPClient: client,
|
||||||
Premium: opts.MobileOffline || opts.WebOffline,
|
Premium: opts.MobileOffline || opts.WebOffline,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@@ -69,17 +69,17 @@ func (t *Track) Filename(kind Kind, mediaFormat string) string {
|
|||||||
return base + "." + ext
|
return base + "." + ext
|
||||||
}
|
}
|
||||||
|
|
||||||
func truncateBytes(s string, max int) string {
|
func truncateBytes(s string, maxLen int) string {
|
||||||
if max <= 0 {
|
if maxLen <= 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if len(s) <= max {
|
if len(s) <= maxLen {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
last := 0
|
last := 0
|
||||||
for i := range s {
|
for i := range s {
|
||||||
if i > max {
|
if i > maxLen {
|
||||||
return s[:last]
|
return s[:last]
|
||||||
}
|
}
|
||||||
last = i
|
last = i
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
neturl "net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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) {
|
func findTrackURL(ctx context.Context, httpClient *http.Client, artist, title, duration string) (string, error) {
|
||||||
const rootURL = "https://songbpm.com"
|
const rootURL = "https://songbpm.com"
|
||||||
|
|
||||||
values := neturl.Values{}
|
values := url.Values{}
|
||||||
values.Add("query", fmt.Sprintf("%s %s", artist, title))
|
values.Add("query", fmt.Sprintf("%s %s", artist, title))
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, rootURL+"/searches", bytes.NewBufferString(values.Encode()))
|
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
|
return rootURL + matchURL, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchBPMPage(ctx context.Context, httpClient *http.Client, url string) (string, error) {
|
func fetchBPMPage(ctx context.Context, httpClient *http.Client, pageURL string) (string, error) {
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pageURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ func fetchGenre(ctx context.Context, httpClient *http.Client, artist, title stri
|
|||||||
return formatTags(filtered), nil
|
return formatTags(filtered), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchGenrePage(ctx context.Context, httpClient *http.Client, url string) (*goquery.Document, error) {
|
func fetchGenrePage(ctx context.Context, httpClient *http.Client, pageURL string) (*goquery.Document, error) {
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pageURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type metadataResult struct {
|
|||||||
warnings []string
|
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 {
|
if !opts.BPM && !opts.Genre {
|
||||||
return metadataResult{}
|
return metadataResult{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func (d *Downloader) downloadTrack(ctx context.Context, resource deezer.Resource
|
|||||||
|
|
||||||
metadataChan := make(chan metadataResult, 1)
|
metadataChan := make(chan metadataResult, 1)
|
||||||
go func() {
|
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)
|
dlCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ var managedPrefixes = []string{
|
|||||||
|
|
||||||
func resolveTarget() (string, error) {
|
func resolveTarget() (string, error) {
|
||||||
if buildinfo.IsDev() {
|
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)
|
repoOwner, repoName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ func resolveTarget() (string, error) {
|
|||||||
|
|
||||||
for _, prefix := range managedPrefixes {
|
for _, prefix := range managedPrefixes {
|
||||||
if target == prefix || strings.HasPrefix(target, prefix+"/") {
|
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 {
|
func checkWritable(dir string) error {
|
||||||
f, err := os.CreateTemp(dir, tmpPattern)
|
f, err := os.CreateTemp(dir, tmpPattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hint := "Re-run with sudo"
|
hint := "re-run with sudo"
|
||||||
if runtime.GOOS == "windows" {
|
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()
|
name := f.Name()
|
||||||
@@ -198,7 +198,7 @@ func (u *Updater) replaceBinary(target, tmp string) error {
|
|||||||
|
|
||||||
if err := os.Rename(tmp, target); err != nil {
|
if err := os.Rename(tmp, target); err != nil {
|
||||||
if rollbackErr := os.Rename(old, target); rollbackErr != 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)
|
err, old, rollbackErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func writeCache(version string) error {
|
|||||||
|
|
||||||
func check(ctx context.Context) (string, error) {
|
func check(ctx context.Context) (string, error) {
|
||||||
if entry, ok := readCache(); ok {
|
if entry, ok := readCache(); ok {
|
||||||
return newerThanCurrent(entry.LatestVersion), nil
|
return latestIfNewer(entry.LatestVersion), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := New().Latest(ctx)
|
release, err := New().Latest(ctx)
|
||||||
@@ -84,10 +84,10 @@ func check(ctx context.Context) (string, error) {
|
|||||||
latest := release.Version()
|
latest := release.Version()
|
||||||
_ = writeCache(latest)
|
_ = 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) {
|
if IsNewer(buildinfo.Version(), latest) {
|
||||||
return latest
|
return latest
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user