-
Notifications
You must be signed in to change notification settings - Fork 110
/
file.go
36 lines (32 loc) · 976 Bytes
/
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
package utils
import (
"os"
"path/filepath"
"runtime"
"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
})
}