-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathitem.go
More file actions
83 lines (70 loc) · 1.76 KB
/
item.go
File metadata and controls
83 lines (70 loc) · 1.76 KB
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
package user
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/exercism/cli/api"
)
// Item is a problem that has been fetched from the APIs.
// It contains some data specific to this particular request and user
// in order to give a useful report to the user about what has been fetched.
type Item struct {
*api.Problem
dir string
isNew bool
isUpdated bool
}
// Path is the location of this item on the user's filesystem.
func (it *Item) Path() string {
return filepath.Join(it.dir, it.TrackID, it.Slug)
}
// Matches determines whether or not this item matches the given filter.
func (it *Item) Matches(filter HWFilter) bool {
switch filter {
case HWNew:
return it.isNew
case HWUpdated:
return it.isUpdated
case HWNotSubmitted:
return !it.Submitted
}
return true
}
// Save writes the embedded problem to the filesystem.
func (it *Item) Save() error {
if _, err := os.Stat(it.Path()); err != nil {
if !os.IsNotExist(err) {
return err
}
it.isNew = true
}
for name, text := range it.Files {
file := filepath.Join(it.Path(), name)
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
return err
}
if _, err := os.Stat(file); err != nil {
if !os.IsNotExist(err) {
return err
}
if !it.isNew {
it.isUpdated = true
}
if runtime.GOOS == "windows" {
text = strings.Replace(text, "\n", "\r\n", -1)
}
if err := ioutil.WriteFile(file, []byte(text), 0644); err != nil {
return err
}
}
}
return nil
}
// Report outputs the line's string and path in the format of the passed in template.
func (it *Item) Report(template string, max int) string {
padding := strings.Repeat(" ", max-len(it.String()))
return fmt.Sprintf(template, it.String(), padding, it.Path())
}