Skip to content

Commit

Permalink
Some Prerun validation + search flag used for indication of search no…
Browse files Browse the repository at this point in the history
…t just check
  • Loading branch information
AYehia0 committed Jan 14, 2023
1 parent 3810f53 commit 298b408
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions cmd/soundcloud-dl/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package soundclouddl
import (
"fmt"
"log"
"os"
"strings"

"github.com/AYehia0/soundcloud-dl/internal"
"github.com/AYehia0/soundcloud-dl/pkg/theme"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var rootCmd = &cobra.Command{
Expand All @@ -14,20 +18,31 @@ var rootCmd = &cobra.Command{
Long: `A blazingly fast go program to download tracks from soundcloud
using just the URL, with some cool features and beautiful UI.
`,
Args: cobra.ArbitraryArgs,
Args: cobra.ArbitraryArgs,
Version: "v1.0.0",
Run: func(cmd *cobra.Command, args []string) {
// get the URL
// TODO: check cobra docs for a cleaner way to do this
if len(args) < 1 {
if len(args) < 1 && !Search {
if err := cmd.Usage(); err != nil {
log.Fatal(err)
}
return
}
// run the core app
// FIXME: Probably not the best thing to do lol, it's better to just pass it to the function, who cares.
args = append(args, DownloadPath)
internal.Sc(args, BestQuality)
internal.Sc(args, DownloadPath, BestQuality, Search)
},
PreRun: func(cmd *cobra.Command, args []string) {
cmd.Flags().Visit(func(f *pflag.Flag) {
// check if <url> is passed with --search-and-download flag
if len(args) != 0 {
if strings.HasPrefix(args[0], "https") && Search {
fmt.Printf("Can't use/pass a %s with --%s flag\n\n", theme.Green("<url>"), theme.Red(f.Name))
cmd.Usage()
os.Exit(0)
}
}
})
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/soundcloud-dl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var BestQuality bool
// define flags and handle configuration
func InitConfigVars() {
tmpDLdir, _ := os.Getwd()
rootCmd.PersistentFlags().BoolVarP(&Search, "search", "s", false, "Check if the track exists or not.")
rootCmd.PersistentFlags().BoolVarP(&Search, "search-and-download", "s", false, "Search for tracks by title and prompt one for download ")
rootCmd.PersistentFlags().StringVarP(&DownloadPath, "download-path", "p", tmpDLdir, "The download path where tracks are stored.")
rootCmd.PersistentFlags().BoolVarP(&BestQuality, "best", "b", false, "Download with the best available quality.")
}

0 comments on commit 298b408

Please sign in to comment.