-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.go
45 lines (41 loc) · 1017 Bytes
/
tools.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
// Package launchr provides common app functionality.
package launchr
import (
"io/fs"
"os"
"path/filepath"
"reflect"
)
// GetFsAbsPath returns absolute path for a FS struct.
func GetFsAbsPath(fs fs.FS) string {
cwd := ""
rval := reflect.ValueOf(fs)
if rval.Kind() == reflect.String {
var err error
cwd = rval.String()
// @todo Rethink absolute path usage overall.
if !filepath.IsAbs(cwd) {
cwd, err = filepath.Abs(cwd)
if err != nil {
panic("can't retrieve absolute path for the path")
}
}
}
return cwd
}
// EnsurePath creates all directories in the path.
func EnsurePath(parts ...string) error {
p := filepath.Clean(filepath.Join(parts...))
if _, err := os.Stat(p); os.IsNotExist(err) {
return os.MkdirAll(p, 0750)
}
return nil
}
// GetTypePkgPathName returns type package path and name for internal usage.
func GetTypePkgPathName(v interface{}) (string, string) {
t := reflect.TypeOf(v)
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
return t.PkgPath(), t.Name()
}