feat(cli): simplify output for individual songs

This commit is contained in:
Mathis Maquenne
2025-09-10 11:10:52 +02:00
parent 9b23e3a8d9
commit c2b4fcfa11
4 changed files with 27 additions and 25 deletions
+4 -5
View File
@@ -78,12 +78,11 @@ func newDownloadCmd(resourceType string) *cobra.Command {
}, },
} }
if resourceType == "artist" { switch resourceType {
cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 10, "number of songs to download") case "artist":
cmd.Short = "Download top songs from an artist" cmd.Short = "Download top songs from an artist"
} cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 10, "number of songs to download")
case "track":
if resourceType == "track" {
cmd.Short = "Download a single track" cmd.Short = "Download a single track"
} }
+6 -7
View File
@@ -77,18 +77,17 @@ func (c *Client) FetchResource(ctx context.Context, resource Resource, id string
return err return err
} }
if strings.Contains(string(body), `"DATA_ERROR":"playlist::getData"`) { switch {
case strings.Contains(string(body), `"DATA_ERROR":"playlist::getData"`):
return fmt.Errorf("invalid playlist ID") return fmt.Errorf("invalid playlist ID")
} case strings.Contains(string(body), `"DATA_ERROR":"album::getData"`):
if strings.Contains(string(body), `"DATA_ERROR":"album::getData"`) {
return fmt.Errorf("invalid album ID") return fmt.Errorf("invalid album ID")
} case strings.Contains(string(body), `"DATA_ERROR":"artist::getData"`):
if strings.Contains(string(body), `"DATA_ERROR":"artist::getData"`) {
return fmt.Errorf("invalid artist ID") return fmt.Errorf("invalid artist ID")
} case strings.Contains(string(body), `"DATA_ERROR":"song::getData"`):
if strings.Contains(string(body), `"DATA_ERROR":"song::getData"`) {
return fmt.Errorf("invalid track ID") return fmt.Errorf("invalid track ID")
} }
if strings.Contains(string(body), `"results":{}`) { if strings.Contains(string(body), `"results":{}`) {
return fmt.Errorf("unexpected response") return fmt.Errorf("unexpected response")
} }
+1 -5
View File
@@ -57,11 +57,7 @@ func (t *Track) GetSongs() []*Song {
return []*Song{t.Results.Data} return []*Song{t.Results.Data}
} }
func (t *Track) SetSongs(songs []*Song) { func (t *Track) SetSongs(songs []*Song) {}
if len(songs) > 0 {
t.Results.Data = songs[0]
}
}
func (t *Track) GetOutputDir(outputDir string) string { func (t *Track) GetOutputDir(outputDir string) string {
if t.Results.Data == nil { if t.Results.Data == nil {
+16 -8
View File
@@ -75,6 +75,9 @@ func (c *Client) Run(ctx context.Context, opts Options, id string) error {
songs := resource.GetSongs() songs := resource.GetSongs()
if len(songs) == 0 { if len(songs) == 0 {
if c.resourceType == "track" {
return fmt.Errorf("track with ID %s not found", id)
}
return fmt.Errorf("%s has no songs", c.resourceType) return fmt.Errorf("%s has no songs", c.resourceType)
} }
if c.resourceType == "artist" && len(songs) > opts.Limit { if c.resourceType == "artist" && len(songs) > opts.Limit {
@@ -89,7 +92,9 @@ func (c *Client) Run(ctx context.Context, opts Options, id string) error {
} }
startTime := time.Now() startTime := time.Now()
fmt.Printf("%s\n\nStarting download...\n\n", resource) if c.resourceType != "track" {
fmt.Printf("%s\n\nStarting download...\n\n", resource)
}
downloaded := 0 downloaded := 0
skipped := 0 skipped := 0
@@ -147,7 +152,9 @@ func (c *Client) Run(ctx context.Context, opts Options, id string) error {
if downloaded > 0 || failed > 0 { if downloaded > 0 || failed > 0 {
c.Logger.Infof("Playlist %s (%s): %d downloaded, %d skipped, %d failed\n", resource.GetTitle(), id, downloaded, skipped, failed) c.Logger.Infof("Playlist %s (%s): %d downloaded, %d skipped, %d failed\n", resource.GetTitle(), id, downloaded, skipped, failed)
} }
fmt.Printf(`
if c.resourceType != "track" {
fmt.Printf(`
================== [ Summary ] ================== ================== [ Summary ] ==================
Downloaded: %d Downloaded: %d
Skipped: %d Skipped: %d
@@ -156,12 +163,13 @@ Elapsed time: %s
Files saved to: %s Files saved to: %s
================================================= =================================================
`, `,
downloaded, downloaded,
skipped, skipped,
failed, failed,
time.Since(startTime).Round(time.Second), time.Since(startTime).Round(time.Second),
resourceOutputDir, resourceOutputDir,
) )
}
return nil return nil
} }