-
Notifications
You must be signed in to change notification settings - Fork 110
/
file.go
49 lines (44 loc) · 1.47 KB
/
file.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
package utils
import (
"os"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
"go.viam.com/utils"
)
// ResolveFile returns the path of the given file relative to the root
// of the codebase. For example, if this file currently
// lives in utils/file.go and ./foo/bar/baz is given, then the result
// is foo/bar/baz. This is helpful when you don't want to relatively
// refer to files when you're not sure where the caller actually
// lives in relation to the target file.
func ResolveFile(fn string) string {
//nolint:dogsled
_, thisFilePath, _, _ := runtime.Caller(0)
thisDirPath, err := filepath.Abs(filepath.Dir(thisFilePath))
if err != nil {
panic(err)
}
return filepath.Join(thisDirPath, "..", fn)
}
// RemoveFileNoError will remove the file at the given path if it exists. Any
// errors will be suppressed.
func RemoveFileNoError(path string) {
utils.UncheckedErrorFunc(func() error {
if _, err := os.Stat(path); err == nil {
return os.Remove(path)
}
return nil
})
}
// SafeJoinDir performs a filepath.Join of 'parent' and 'subdir' but returns an error
// if the resulting path points outside of 'parent'.
// See also https://github.com/cyphar/filepath-securejoin.
func SafeJoinDir(parent, subdir string) (string, error) {
res := filepath.Join(parent, subdir)
if !strings.HasPrefix(filepath.Clean(res), filepath.Clean(parent)+string(os.PathSeparator)) {
return res, errors.Errorf("unsafe path join: '%s' with '%s'", parent, subdir)
}
return res, nil
}