Skip to content

Commit a7b39b6

Browse files
committed
Download: create directory before writing file
A recent change to exercism [1] allows it to preserve directory structures in submissions. However, `exercism download` currently cannot handle such submissions. For example, a submission with `dir/file.txt` would attempt to write `dir/file.txt` without `dir` existing, and fail. To fix this, we create any containing directories before writing files. Fixes #277 [1] exercism/exercism@2dc984d
1 parent b34026c commit a7b39b6

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

cmd/download.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func Download(ctx *cli.Context) {
4646

4747
for name, contents := range submission.SolutionFiles {
4848
filename := strings.TrimPrefix(name, strings.ToLower("/"+submission.TrackID+"/"+submission.Slug+"/"))
49+
dir := filepath.Dir(filename)
50+
if err := os.MkdirAll(fmt.Sprintf("%s/%s", path, dir), 0755); err != nil {
51+
log.Fatal(err)
52+
}
4953
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", path, filename), []byte(contents), 0644); err != nil {
5054
log.Fatalf("Unable to write file %s: %s", name, err)
5155
}

0 commit comments

Comments
 (0)