Newer
Older
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
package util
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
func Now() int64 {
return int64(time.Now().Nanosecond())
}
func GenerateGosdnPath() (string, error) {
var execPath string
var absExecPath string
var err error
if execPath, err = os.Executable(); err != nil {
return "", err
}
if absExecPath, err = filepath.Abs(execPath); err != nil {
return "", err
}
executableDir := filepath.Dir(absExecPath)
projectRoot := filepath.Dir(executableDir)
return projectRoot, nil
}
// Get the path of a file, if path is a path, just return absolute path
func GetAbsPath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("Failed to resolve absolut path of %s: %w", path, err)
}
if strings.HasSuffix(path, "/") {
return absPath, nil
}
info, err := os.Stat(absPath)
if err != nil {
return "", fmt.Errorf("Failed to stat path: %w", err)
}
// If path is not a directory, make it one
if !info.IsDir() {
absPath = filepath.Dir(absPath)
}
return absPath, nil
}