refactor: change default output directory
This commit is contained in:
+23
-4
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/mathismqn/godeez/internal/config"
|
||||
"github.com/mathismqn/godeez/internal/deezer"
|
||||
"github.com/mathismqn/godeez/internal/tags"
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -23,11 +24,31 @@ var downloadCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(downloadCmd)
|
||||
downloadCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "output directory (default is current directory)")
|
||||
downloadCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "output directory (default is $HOME/Music/GoDeez)")
|
||||
downloadCmd.PersistentFlags().StringVarP(&quality, "quality", "q", "", "download quality [mp3_128, mp3_320, flac, best] (default is best)")
|
||||
}
|
||||
|
||||
func validateInput() {
|
||||
if outputDir == "" {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
musicDir := path.Join(homeDir, "Music")
|
||||
if err := utils.EnsureDir(musicDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create music directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
outputDir = path.Join(musicDir, "GoDeez")
|
||||
if err := utils.EnsureDir(outputDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create GoDeez directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if quality == "" {
|
||||
quality = "best"
|
||||
}
|
||||
@@ -91,12 +112,10 @@ func downloadContent(contentType string, args []string) {
|
||||
fmt.Printf("\r[%d/%d] Getting data for %s %s... DONE\n", i+1, nArgs, contentType, id)
|
||||
|
||||
output := resource.GetOutputPath(outputDir)
|
||||
if _, err := os.Stat(output); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(output, 0755); err != nil {
|
||||
if err := utils.EnsureDir(output); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create output directory: %v\n", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
title := resource.GetTitle()
|
||||
fmt.Printf("Starting download of %s: %s\n", contentType, title)
|
||||
|
||||
@@ -40,7 +40,6 @@ func (a *Album) GetOutputPath(outputDir string) string {
|
||||
base := fmt.Sprintf("%s - %s", a.Results.Data.Artist, a.Results.Data.Title)
|
||||
base, _ = filenamify.Filenamify(base, filenamify.Options{})
|
||||
outputPath := path.Join(outputDir, base)
|
||||
outputPath, _ = filenamify.Filenamify(outputPath, filenamify.Options{})
|
||||
|
||||
return outputPath
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ func (p *Playlist) GetSongs() []*Song {
|
||||
func (p *Playlist) GetOutputPath(outputDir string) string {
|
||||
p.Results.Data.Title, _ = filenamify.Filenamify(p.Results.Data.Title, filenamify.Options{})
|
||||
outputPath := path.Join(outputDir, p.Results.Data.Title)
|
||||
outputPath, _ = filenamify.Filenamify(outputPath, filenamify.Options{})
|
||||
|
||||
return outputPath
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func EnsureDir(path string) error {
|
||||
info, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return os.MkdirAll(path, 0755)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("file already exists at %s", path)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user