/
fs.go
167 lines (139 loc) · 3.8 KB
/
fs.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2016 The nvim-go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fs
import (
"go/build"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/neovim/go-client/nvim"
)
// Chdir changes the vim current working directory.
// The returned function restores working directory to `getcwd()` result path
// and unlocks the mutex.
func Chdir(v *nvim.Nvim, dir string) func() {
var m sync.Mutex
var cwd interface{}
m.Lock()
v.Eval("getcwd()", &cwd)
v.SetCurrentDirectory(dir)
return func() {
v.SetCurrentDirectory(cwd.(string))
m.Unlock()
}
}
// JoinGoPath joins the $GOPATH + "src" to p
func JoinGoPath(p string) string {
return filepath.Join(build.Default.GOPATH, "src", p)
}
// TrimGoPath trims the GOPATH and {bin,pkg,src}, basically for the converts
// the package ID
func TrimGoPath(p string) string {
// Separate trim work for p equal GOPATH
p = strings.TrimPrefix(p, build.Default.GOPATH)
p = strings.TrimPrefix(p, string(filepath.Separator))
if len(p) >= 4 {
switch p[:3] {
case "bin", "pkg", "src":
return filepath.Clean(p[4:])
}
}
return p
}
// ToGoPath trims the GOPATH and {bin,pkg,src}, basically for the converts
// the package ID.
func ToGoPath(p string) string {
if idx := strings.Index(p, build.Default.GOPATH); idx > 0 {
// Separate trim work for p equal GOPATH
p = strings.TrimPrefix(p, build.Default.GOPATH+string(filepath.Separator))
}
if len(p) >= 4 {
switch p[:3] {
case "bin", "pkg", "src":
return filepath.Clean(p[4:])
}
}
return p
}
// VendorPath finds the package root and returens the vendor directory path.
func VendorPath(p string) string {
if idx := strings.Index(p, build.Default.GOPATH); idx > 0 {
// Separate trim work for p equal GOPATH
p = strings.TrimPrefix(p, build.Default.GOPATH+string(filepath.Separator))
}
if len(p) >= 4 {
switch p[:3] {
case "bin", "pkg", "src":
return filepath.Clean(p[4:])
}
}
p = filepath.Join(p, "vendor")
return p
}
// ExpandGoRoot expands the "$GOROOT" include from p.
func ExpandGoRoot(p string) string {
if strings.Index(p, "$GOROOT") != -1 {
return strings.Replace(p, "$GOROOT", runtime.GOROOT(), 1)
}
return p // Not hit
}
// ShortFilePath return the simply trim cwd into p.
func ShortFilePath(p, cwd string) string {
return strings.Replace(p, cwd, ".", 1)
}
// Rel wrapper of filepath.Rel function that return only one variable.
func Rel(cwd, f string) string {
rel, err := filepath.Rel(cwd, f)
if err != nil {
return f
}
return rel
}
// ToWildcard returns the path with wildcard(...) suffix.
func ToWildcard(path string) string {
return filepath.Join(path, string(filepath.Separator), "...")
}
func Create(filename string) error {
if IsNotExist(filename) {
if _, err := os.Create(filename); err != nil {
return err
}
}
return nil
}
func Mkdir(dir string, perm os.FileMode) error {
if !IsDirExist(dir) {
if err := os.MkdirAll(dir, perm); err != nil {
return err
}
}
return nil
}
// IsDir returns whether the filename is directory.
func IsDir(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.IsDir()
}
// IsExist returns whether the filename is exists.
func IsExist(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err) || err == nil
}
// IsNotExist returns whether the filename is exists.
func IsNotExist(filename string) bool {
_, err := os.Stat(filename)
return os.IsNotExist(err)
}
// IsDirExist reports whether dir exists and which is directory.
func IsDirExist(dir string) bool {
fi, err := os.Stat(dir)
return err == nil && fi.IsDir()
}
// IsGoFile returns whether the filename is exists.
func IsGoFile(filename string) bool {
f, err := os.Stat(filename)
return err == nil && filepath.Ext(f.Name()) == ".go"
}