forked from go-llvm/llgo
/
paths.go
45 lines (40 loc) · 997 Bytes
/
paths.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
// Copyright 2012 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
import (
"os/user"
"path/filepath"
"strings"
)
// userpath takes a path and resolves a leading "~" pattern to
// the current or specified user's home directory. Note that the
// path will not be converted to an absolute path if there is no
// leading "~" pattern.
func userpath(path string) (string, error) {
if len(path) > 0 && path[0] == '~' {
slashindex := strings.IndexRune(path, filepath.Separator)
var username string
if slashindex == -1 {
username = path[1:]
} else {
username = path[1:slashindex]
}
var u *user.User
var err error
if username == "" {
u, err = user.Current()
} else {
u, err = user.Lookup(username)
}
if err != nil {
return "", err
}
if slashindex == -1 {
path = u.HomeDir
} else {
path = filepath.Join(u.HomeDir, path[slashindex+1:])
}
}
return path, nil
}