Files
streamrip-go/cmd/rip/help.go
T
JorenandClaude Opus 5 c030d1be09 fix: resource leaks, cancellation, races and path traversal
Six correctness bugs that the existing tests could not see. The suite passed
and `go test -race` was clean throughout, because nothing exercised concurrent
token refresh, shared-folder artwork, or hostile metadata.

main(): extract run() int
  main() was a single 1196-line function that registered 20
  `defer mainApp.Close()` handlers and then called os.Exit 150 times, so none
  of them ever ran. Close() is what closes the SQLite handle, flushes the
  progress bars and removes the artwork temp dirs, so every failed rip leaked
  temp directories. main() is now a wrapper around run() int and the command
  bodies return exit codes.

Ctrl-C now stops a rip
  ctx was threaded everywhere but never checked: zero ctx.Err()/ctx.Done()
  in internal/app and internal/download. Cancellation left Rip() iterating
  the whole remaining queue, and the retry ladder retried on a dead context,
  so one Ctrl-C cost three doomed attempts per remaining track. Adds checks to
  the dispatch loops, the worker-spawn loops and the retry ladder, and routes
  the MarkFailed/MarkDownloaded bookkeeping through a detached context so the
  records survive cancellation. Convert and TagFLAC now take a ctx and use
  exec.CommandContext, so a stalled ffmpeg/ffprobe no longer outlives Ctrl-C.

Config race
  Provider token refresh mutated the shared *config.Config and called
  SaveFile() from inside download workers with no lock, racing the struct
  fields and issuing concurrent os.WriteFile against one path. Adds a mutex to
  config.Config plus UpdateTokens(), and routes all five refresh sites through
  it. Each provider client gets an authMu guarding loggedIn and its
  login/refresh path; beatport needed it most, since ensureToken is reached
  from apiRequest.

Artwork cover race
  artwork.Prepare was check-then-write on a path shared by every worker in a
  playlist rip. Two workers both saw a missing cover.jpg, both created it, and
  the loser's cleanup deleted the winner's finished file. Adds a per-folder
  lock and downloads via a unique temp file + rename. The regression test
  fails against the old code with 15 of 16 workers getting no cover.

Path traversal from remote metadata
  naming.CleanName stripped <>:"/\|?* and control chars but not "." , so a
  title of ".." survived into filepath.Join and escaped the download root.
  SoundCloud and Tidal playlist names are user-generated. Now rejects "." and
  "..", trims trailing dots, handles Windows device names, adds DEL to the
  invalid set and caps components at 255 bytes without splitting a rune.
  app.go gains safeJoin as a second line of defence.

Credentials no longer world-readable
  The config holds plaintext provider passwords, Deezer ARL cookies and OAuth
  tokens and was written 0o644. Now 0o600, with 0o700 directories, and an
  existing 0o644 file is tightened on write.

Also: deletes the 9 duplicated *-rip-smoke dev commands (~300 lines fully
subsumed by `rip id`) and the unreachable second dev-help arm; registers
image/png so PNG covers are downscaled instead of being silently saved as
cover.jpg containing PNG bytes; adds the missing .gitignore, which was keeping
a 21MB build artifact one `git add -A` away from the repo.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-03 16:52:58 +02:00

161 lines
4.0 KiB
Go

package main
import (
"fmt"
"io"
"strings"
)
func isHelpArg(arg string) bool {
return arg == "-h" || arg == "--help"
}
func commandWantsHelp(args []string) bool {
for _, arg := range args {
if isHelpArg(arg) {
return true
}
}
return false
}
func printMainHelp(w io.Writer) {
fmt.Fprint(w, `streamrip-go
Usage:
rip [global options] <command> [command options]
rip help [command]
Commands:
url Rip one or more URLs
file Rip URLs or IDs from a file
id Rip by source, media type, and ID
search Search a provider and download selected results
lastfm Import a Last.fm playlist
config Manage configuration
database Inspect download databases
Global options:
-f, --folder <path> Override downloads folder
-q, --quality <0-4> Override provider quality
-c, --codec <codec> Convert after download: ALAC, FLAC, OGG, MP3, AAC
-ndb, --no-db Ignore download database
--no-progress Disable progress bars
--no-ssl-verify Disable TLS verification
--config-path <path> Use a custom config file
-v, -vv Verbose logging
-h, --help Show help
Run 'rip help <command>' for command-specific help.
`)
}
func printCommandHelp(w io.Writer, command string) bool {
switch strings.ToLower(strings.TrimSpace(command)) {
case "url":
fmt.Fprint(w, `Usage:
rip url <url...> [--force|--ignore-db]
Rip one or more provider URLs.
Examples:
rip url https://www.beatport.com/release/example/123
rip url https://play.qobuz.com/album/abc --force
Options:
--force, --ignore-db Redownload even if already in the database
`)
case "file":
fmt.Fprint(w, `Usage:
rip file <path> [--force|--ignore-db]
Rip URLs or JSON ID entries from a file.
Options:
--force, --ignore-db Redownload even if already in the database
`)
case "id":
fmt.Fprint(w, `Usage:
rip id <source> <media-type> <id> [quality] [--force|--ignore-db]
Rip an item by provider ID.
Sources:
qobuz, tidal, deezer, yandex, beatport, soundcloud
Media types:
track, album, playlist, artist, label, chart, video
Options:
quality Override quality for this rip, 0-4
--force, --ignore-db Redownload even if already in the database
`)
case "search":
fmt.Fprint(w, `Usage:
rip search <source> <media-type> <query...> [options]
Search a provider and optionally download selected results.
Sources:
qobuz, tidal, deezer, yandex, beatport, soundcloud
Media types:
track, album, playlist, artist, label, chart, video
Options:
--limit N Maximum results to show
--first Download the first result without prompting
--no-download Show results only
--output-file <path> Write results as JSON
--force, --ignore-db Redownload even if already in the database
`)
case "lastfm":
fmt.Fprint(w, `Usage:
rip lastfm [--source SOURCE] [--fallback-source SOURCE] <playlist_url>
Import a Last.fm playlist and resolve tracks through configured providers.
Options:
--source SOURCE Primary lookup source
--fallback-source SOURCE Fallback lookup source
`)
case "config":
fmt.Fprint(w, `Usage:
rip config <open|reset|path> [options]
Manage configuration.
Commands:
open Open the config file in an editor
reset Reset config to defaults
path Print the config path
Options:
-v, --vim Use Vim for 'rip config open'
-y, --yes Confirm 'rip config reset' without prompting
`)
case "database":
fmt.Fprint(w, `Usage:
rip database browse <downloads|failed>
Inspect local download databases.
`)
case "dev-help":
printDeveloperHelp(w)
default:
return false
}
return true
}
func printDeveloperHelp(w io.Writer) {
fmt.Fprint(w, `Developer smoke commands:
soundcloud-smoke
qobuz-smoke, qobuz-convert-rip-smoke, qobuz-search-smoke
tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke
The per-media-type rip smoke commands were removed; use the equivalent
rip id <source> <media-type> <id>
`)
}