🧱 Chunk is a download manager for slow and unstable servers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Adds target directory to CLI

+29 -1
+29 -1
cmd/chunk/main.go
··· 10 10 "github.com/spf13/cobra" 11 11 ) 12 12 13 + func checkOutputDir() error { 14 + var err error 15 + if outputDir == "" { 16 + outputDir, err = os.Getwd() 17 + if err != nil { 18 + return fmt.Errorf("could not load current directory: %w", err) 19 + } 20 + } 21 + i, err := os.Stat(outputDir) 22 + if os.IsNotExist(err) { 23 + return fmt.Errorf("directory %s does not exist", outputDir) 24 + } 25 + if err != nil { 26 + return fmt.Errorf("could not get info for %s: %w", outputDir, err) 27 + } 28 + if !i.Mode().IsDir() { 29 + return fmt.Errorf("%s is not a directory", outputDir) 30 + } 31 + return nil 32 + } 33 + 13 34 var rootCmd = &cobra.Command{ 14 35 Use: "chunk", 15 36 Short: "Download tool for slow and unstable servers", 16 37 Long: "Download tool for slow and unstable servers using HTTP range requests, retries per HTTP request (not by file), prevents re-downloading the same content range and supports wait time to give servers time to recover.", 17 - Run: func(cmd *cobra.Command, args []string) { 38 + RunE: func(cmd *cobra.Command, args []string) error { 39 + if err := checkOutputDir(); err != nil { 40 + return fmt.Errorf("error checking %s directory: %w", outputDir, err) 41 + } 18 42 chunk := chunk.DefaultDownloader() 43 + chunk.OutputDir = outputDir 19 44 chunk.Timeout = timeoutChunk 20 45 chunk.ConcurrencyPerServer = concurrencyPerServer 21 46 chunk.MaxRetries = maxRetriesChunk ··· 29 54 prog.update(status) 30 55 } 31 56 fmt.Printf("\r%s\nDownloaded to: %s", prog.String(), os.TempDir()) 57 + return nil 32 58 }, 33 59 } 34 60 35 61 // Flags 36 62 var ( 63 + outputDir string 37 64 timeoutChunk time.Duration 38 65 concurrencyPerServer int 39 66 maxRetriesChunk uint ··· 42 69 ) 43 70 44 71 func init() { 72 + rootCmd.Flags().StringVarP(&outputDir, "output-directory", "d", "", "directory where to save the downloaded files.") 45 73 rootCmd.Flags().DurationVarP(&timeoutChunk, "timeout", "t", chunk.DefaultTimeout, "timeout for the download of each chunk from each URL.") 46 74 rootCmd.Flags().UintVarP(&maxRetriesChunk, "max-retries", "r", chunk.DefaultMaxRetries, "maximum number of retries for each chunk.") 47 75 rootCmd.Flags().DurationVarP(&waitBetweenRetries, "wait-retry", "w", chunk.DefaultWaitRetry, "pause before retrying an HTTP request that has failed.")