-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson.go
100 lines (84 loc) · 2.1 KB
/
lesson.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"io"
"os"
"path/filepath"
)
// Lesson contains a lesson name, if it was modified, and its owner (which can be
// nil for stock lessons.)
type Lesson struct {
Name string
Modified bool
// Not included in JSON
Owner *User `json:"-"`
Path string `json:"-"`
AlternateRobotPath string `json:"-"`
file *os.File
}
// Lessons contains a map of Lessons indexed by lesson names. The contained Lesson
// also contains a Name field, but we use a map/keep the duplicate data for easy of use.
type Lessons map[string]*Lesson
// NewLessons makes a new lesson map
func NewLessons() Lessons {
return make(map[string]*Lesson)
}
// NewLesson makes a new lesson from a fileName and directory path. The user
// must set if the lesson has been modified or not!
func NewLesson(name string, path string) (*Lesson, error) {
lesson := &Lesson{
Name: name,
Path: path,
// Modified should be set by the user!
}
file, err := os.OpenFile(lesson.Path, os.O_RDWR, 0666)
if err != nil {
return &Lesson{}, err
}
lesson.file = file
return lesson, nil
}
// Slice returns Lessons as []Lesson
func (l Lessons) Slice() []*Lesson {
s := make([]*Lesson, len(l))
i := 0
for _, v := range l {
s[i] = v
i++
}
return s
}
// Reader returns a reader to read the contents of the lesson.
func (l *Lesson) Reader() io.Reader {
l.file.Seek(0, 0)
return l.file
}
// SaveCode overwrites the lesson file contents with the code argument.
func (l *Lesson) SaveCode(code string) error {
if l.Owner == nil {
return fmt.Errorf("Cannot save code for a lesson with a nil User pointer")
}
if l.Modified == false {
_, filename := filepath.Split(l.Path)
path := filepath.Join(l.Owner.DataDirectory, filename)
newLessonFile, err := os.Create(path)
if err != nil {
return err
}
io.Copy(newLessonFile, l.file)
l.file = newLessonFile
l.Path = path
l.Modified = true
}
// We fully overwrite the file contents
err := l.file.Truncate(0)
if err != nil {
return err
}
_, err = l.file.Seek(0, 0)
if err != nil {
return err
}
_, err = l.file.Write([]byte(code))
return err
}