| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package executable |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | var ( |
| 12 | Name string |
| 13 | Directory string |
| 14 | ) |
| 15 | |
| 16 | func init() { |
| 17 | path, err := os.Executable() |
| 18 | if err != nil || path == "" { |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | _, Name = filepath.Split(path) |
| 23 | Name = strings.TrimSuffix(Name, ".exe") |
| 24 | Name = strings.TrimSuffix(Name, ".plugin") |
| 25 | |
| 26 | if strings.HasSuffix(Name, ".test") { |
| 27 | Name = "test" |
| 28 | } |
| 29 | if Name == "godplugin" { |
| 30 | Name = "go.d" |
| 31 | } |
| 32 | |
| 33 | fi, err := os.Lstat(path) |
| 34 | if err != nil { |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | if fi.Mode()&os.ModeSymlink != 0 { |
| 39 | realPath, err := filepath.EvalSymlinks(path) |
| 40 | if err != nil { |
| 41 | return |
| 42 | } |
| 43 | Directory = filepath.Dir(realPath) |
| 44 | } else { |
| 45 | Directory = filepath.Dir(path) |
| 46 | } |
| 47 | } |