forked from rhysd/notes-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilepath.go
More file actions
38 lines (35 loc) · 924 Bytes
/
filepath.go
File metadata and controls
38 lines (35 loc) · 924 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
38
package notes
import (
"github.com/pkg/errors"
"os/user"
"path/filepath"
"strings"
)
// canonPath canonicalizes given file path
func canonPath(path string) string {
u, err := user.Current()
if err != nil {
return path // Give up
}
home := filepath.Clean(u.HomeDir)
if !strings.HasPrefix(path, home) {
return path
}
canon := strings.TrimPrefix(path, home)
// Note: home went through filepath.Clean. So it does not have trailing slash and canon is
// always prefixed with slash.
return "~" + canon
}
func validateDirname(name string) error {
if name == "" {
return errors.New("Cannot be empty")
}
if strings.HasPrefix(name, ".") {
return errors.New("Cannot start from '.'")
}
// https://en.wikipedia.org/wiki/Filename
if strings.ContainsAny(name, "/\\?%*:|\"<>") {
return errors.New("Cannot contain '/', '\\', '?', '%', '*', ':', '|', '\"', '<', '>' since they are reserved")
}
return nil
}