forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.go
More file actions
119 lines (108 loc) · 3.23 KB
/
solution.go
File metadata and controls
119 lines (108 loc) · 3.23 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package workspace
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
const ignoreSubdir = ".exercism"
const solutionFilename = "solution.json"
// Solution contains metadata about a user's solution.
type Solution struct {
Track string `json:"track"`
Exercise string `json:"exercise"`
ID string `json:"id"`
URL string `json:"url"`
Handle string `json:"handle"`
IsRequester bool `json:"is_requester"`
SubmittedAt *time.Time `json:"submitted_at,omitempty"`
Dir string `json:"-"`
AutoApprove bool `json:"auto_approve"`
}
// NewSolution reads solution metadata from a file in the given directory.
func NewSolution(dir string) (*Solution, error) {
path := filepath.Join(dir, SolutionMetadataFilepath())
b, err := ioutil.ReadFile(path)
if err != nil {
return &Solution{}, err
}
var s Solution
if err := json.Unmarshal(b, &s); err != nil {
return &Solution{}, err
}
s.Dir = dir
return &s, nil
}
// Suffix is the serial numeric value appended to an exercise directory.
// This is appended to avoid name conflicts, and does not indicate a particular
// iteration.
func (s *Solution) Suffix() string {
return strings.Trim(strings.Replace(filepath.Base(s.Dir), s.Exercise, "", 1), "-.")
}
func (s *Solution) String() string {
str := fmt.Sprintf("%s/%s", s.Track, s.Exercise)
if s.Suffix() != "" {
str = fmt.Sprintf("%s (%s)", str, s.Suffix())
}
if !s.IsRequester && s.Handle != "" {
str = fmt.Sprintf("%s by @%s", str, s.Handle)
}
return str
}
// Write stores solution metadata to a file.
func (s *Solution) Write(dir string) error {
b, err := json.Marshal(s)
if err != nil {
return err
}
if err = createIgnoreSubdir(dir); err != nil {
return err
}
if err = ioutil.WriteFile(filepath.Join(dir, SolutionMetadataFilepath()), b, os.FileMode(0600)); err != nil {
return err
}
s.Dir = dir
return nil
}
// PathToParent is the relative path from the workspace to the parent dir.
func (s *Solution) PathToParent() string {
var dir string
if !s.IsRequester {
dir = filepath.Join("users")
}
return filepath.Join(dir, s.Track)
}
// SolutionMetadataFilepath is the path of the solution metadata file relative to the workspace.
func SolutionMetadataFilepath() string {
return filepath.Join(ignoreSubdir, solutionFilename)
}
func createIgnoreSubdir(path string) error {
path = filepath.Join(path, ignoreSubdir)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, os.FileMode(0755)); err != nil {
return err
}
}
return nil
}
func migrateLegacySolutionFile(legacySolutionPath string, solutionPath string) error {
if _, err := os.Lstat(legacySolutionPath); err != nil {
return err
}
if err := createIgnoreSubdir(filepath.Dir(legacySolutionPath)); err != nil {
return err
}
if _, err := os.Lstat(solutionPath); err != nil {
if err := os.Rename(legacySolutionPath, solutionPath); err != nil {
return err
}
fmt.Fprintf(os.Stderr, "\nMigrated solution metadata to %s\n", solutionPath)
} else {
// TODO: decide how to handle case where both legacy and modern metadata files exist
fmt.Fprintf(os.Stderr, "\nAttempted to migrate solution metadata to %s but file already exists\n", solutionPath)
}
return nil
}