-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathdocument.go
More file actions
34 lines (29 loc) · 849 Bytes
/
document.go
File metadata and controls
34 lines (29 loc) · 849 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
package workspace
import "path/filepath"
// Document is a file in a directory.
type Document struct {
Root string
RelativePath string
}
// NewDocument creates a document from the filepath.
// The root is typically the root of the exercise, and
// path is the absolute path to the file.
func NewDocument(root, path string) (Document, error) {
path, err := filepath.Rel(root, path)
if err != nil {
return Document{}, err
}
return Document{
Root: root,
RelativePath: path,
}, nil
}
// Filepath is the absolute path to the document on the filesystem.
func (doc Document) Filepath() string {
return filepath.Join(doc.Root, doc.RelativePath)
}
// Path is the normalized path.
// It uses forward slashes regardless of the operating system.
func (doc Document) Path() string {
return filepath.ToSlash(doc.RelativePath)
}