summaryrefslogtreecommitdiffstats
path: root/src/go/pkg/executable/executable.go
blob: 3f4e9e0de2073ac9c496846fa56e93a5a50958cc (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package executable

import (
	"os"
	"path/filepath"
	"strings"
)

var (
	Name      string
	Directory string
)

func init() {
	path, err := os.Executable()
	if err != nil || path == "" {
		return
	}

	_, Name = filepath.Split(path)
	Name = strings.TrimSuffix(Name, ".plugin")

	if strings.HasSuffix(Name, ".test") {
		Name = "test"
	}

	fi, err := os.Lstat(path)
	if err != nil {
		return
	}

	if fi.Mode()&os.ModeSymlink != 0 {
		realPath, err := filepath.EvalSymlinks(path)
		if err != nil {
			return
		}
		Directory = filepath.Dir(realPath)
	} else {
		Directory = filepath.Dir(path)
	}
}