···6767 chunkSize int64
6868 waitBetweenRetries time.Duration
6969 restartDownloads bool
7070+ progressDir string
7071)
71727273func init() {
···7778 rootCmd.Flags().Int64VarP(&chunkSize, "chunk-size", "s", chunk.DefaultChunkSize, "maximum size of each HTTP request done using the content range header.")
7879 rootCmd.Flags().IntVarP(&concurrencyPerServer, "concurrency-per-server", "c", chunk.DefaultConcurrencyPerServer, "controls the max number of concurrent connections opened to the same server.")
7980 rootCmd.Flags().BoolVarP(&restartDownloads, "force-restart", "f", chunk.DefaultRestartDownload, "restart previous downloads, ignoring where they were stopped")
8181+ rootCmd.Flags().StringVarP(
8282+ &progressDir,
8383+ "progress-directory",
8484+ "p",
8585+ "",
8686+ fmt.Sprintf("directory where to track progress; %s under user's home directory if blank, or CHUNK_DIR environment variable", chunk.DefaultChunkDir),
8787+ )
8088}
81898290func main() {
+5-1
downloader.go
···9595 // RestartDownloads controls whether or not to continue the download of
9696 // previous download attempts, skipping chunks alreadt downloaded.
9797 RestartDownloads bool
9898+9999+ // ProgressDir is the directory where Chunk keeps track of each chunk
100100+ // downloaded of each file.
101101+ ProgressDir string
98102}
99103100104type chunk struct{ start, end int64 }
···248252 return
249253 }
250254 chunks := d.chunks(t)
251251- p, err := newProgress(s.DownloadedFilePath, s.URL, d.ChunkSize, len(chunks), d.RestartDownloads)
255255+ p, err := newProgress(s.DownloadedFilePath, d.ProgressDir, s.URL, d.ChunkSize, len(chunks), d.RestartDownloads)
252256 if err != nil {
253257 s.Error = fmt.Errorf("could not creat a progress file: %w", err)
254258 ch <- s
+17-8
progress.go
···1111 "sync/atomic"
1212)
13131414-const defaultChunkDir = ".chunk"
1414+// DefaultChunkDir is the directory where Chunk keeps track of each chunk
1515+// downloaded of each file. It us created under the user's home directory by
1616+// default. It can be replaced by the environment variable CHUNK_DIR.
1717+const DefaultChunkDir = ".chunk"
15181619// get the chunk directory under user's home directory
1720// TODO: make it configurable (maybe an envvar?)
1818-func getChunkDirectory() (string, error) {
1919- u, err := user.Current()
2020- if err != nil {
2121- return "", fmt.Errorf("could not get current user: %w", err)
2121+func getChunkDirectory(custom string) (string, error) {
2222+ d := os.Getenv("CHUNK_DIR")
2323+ if custom != "" {
2424+ d = custom
2525+ }
2626+ if d == "" {
2727+ u, err := user.Current()
2828+ if err != nil {
2929+ return "", fmt.Errorf("could not get current user: %w", err)
3030+ }
3131+ d = filepath.Join(u.HomeDir, DefaultChunkDir)
2232 }
2323- d := filepath.Join(u.HomeDir, defaultChunkDir)
2433 if err := os.MkdirAll(d, 0755); err != nil {
2534 return "", fmt.Errorf("could not create chunk's directory %s: %w", d, err)
2635 }
···147156 return downloaded
148157}
149158150150-func newProgress(path, url string, chunkSize int64, chunks int, restart bool) (*progress, error) {
151151- dir, err := getChunkDirectory()
159159+func newProgress(path, dir string, url string, chunkSize int64, chunks int, restart bool) (*progress, error) {
160160+ dir, err := getChunkDirectory(dir)
152161 if err != nil {
153162 return nil, fmt.Errorf("could not get chunk's directory: %w", err)
154163 }