-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathassignment.go
More file actions
37 lines (31 loc) · 695 Bytes
/
assignment.go
File metadata and controls
37 lines (31 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
type Assignment struct {
Track string
Slug string
Files map[string]string
}
func SaveAssignment(dir string, a Assignment) (err error) {
root := fmt.Sprintf("%s/%s/%s", dir, a.Track, a.Slug)
for name, text := range a.Files {
file := fmt.Sprintf("%s/%s", root, name)
dir := filepath.Dir(file)
err = os.MkdirAll(dir, 0755)
if err != nil {
err = fmt.Errorf("Error making directory %v: [%v]", dir, err)
return
}
err = ioutil.WriteFile(file, []byte(text), 0644)
if err != nil {
err = fmt.Errorf("Error writing file %v: [%v]", name, err)
return
}
}
fmt.Println(a.Track, "-", a.Slug)
return
}