Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,51 +78,56 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
}

for _, sf := range download.payload.files() {
url, err := sf.url()
if err != nil {
if err := downloadSolutionFile(client, sf, metadata.Dir); err != nil {
return err
}
}

req, err := client.NewRequest("GET", url, nil)
if err != nil {
return err
}
fmt.Fprintf(Err, "\nDownloaded to\n")
fmt.Fprintf(Out, "%s\n", metadata.Dir)
return nil
}

res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
func downloadSolutionFile(client *api.Client, sf solutionFile, destDir string) error {
url, err := sf.url()
if err != nil {
return err
}

if res.StatusCode != http.StatusOK {
// TODO: deal with it
continue
}
// Don't bother with empty files.
if res.Header.Get("Content-Length") == "0" {
continue
}
req, err := client.NewRequest("GET", url, nil)
if err != nil {
return err
}

// TODO: handle collisions
path := sf.relativePath()
dir := filepath.Join(metadata.Dir, filepath.Dir(path))
if err = os.MkdirAll(dir, os.FileMode(0755)); err != nil {
return err
}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

f, err := os.Create(filepath.Join(metadata.Dir, path))
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
// TODO: deal with it
return nil
}
fmt.Fprintf(Err, "\nDownloaded to\n")
fmt.Fprintf(Out, "%s\n", metadata.Dir)
return nil
// Don't bother with empty files.
if res.Header.Get("Content-Length") == "0" {
return nil
}

// TODO: handle collisions
path := sf.relativePath()
dir := filepath.Join(destDir, filepath.Dir(path))
if err = os.MkdirAll(dir, os.FileMode(0755)); err != nil {
return err
}

f, err := os.Create(filepath.Join(destDir, path))
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, res.Body)
return err
}

type download struct {
Expand Down