-
Notifications
You must be signed in to change notification settings - Fork 26
/
factory.go
61 lines (51 loc) 路 1.27 KB
/
factory.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cmdutil
import (
"os"
"path/filepath"
"strings"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/algolia/cli/api/crawler"
"github.com/algolia/cli/pkg/config"
"github.com/algolia/cli/pkg/iostreams"
)
type Factory struct {
IOStreams *iostreams.IOStreams
Config config.IConfig
SearchClient func() (*search.Client, error)
CrawlerClient func() (*crawler.Client, error)
ExecutableName string
}
// Executable is the path to the currently invoked binary
func (f *Factory) Executable() string {
if !strings.ContainsRune(f.ExecutableName, os.PathSeparator) {
f.ExecutableName = executable(f.ExecutableName)
}
return f.ExecutableName
}
// based on https://github.com/cli/cli/blob/master/pkg/cmdutil/factory.go
func executable(fallbackName string) string {
exe, err := os.Executable()
if err != nil {
return fallbackName
}
base := filepath.Base(exe)
path := os.Getenv("PATH")
for _, dir := range filepath.SplitList(path) {
p, err := filepath.Abs(filepath.Join(dir, base))
if err != nil {
continue
}
f, err := os.Lstat(p)
if err != nil {
continue
}
if p == exe {
return p
} else if f.Mode()&os.ModeSymlink != 0 {
if t, err := os.Readlink(p); err == nil && t == exe {
return p
}
}
}
return exe
}