-
Notifications
You must be signed in to change notification settings - Fork 509
/
version.go
62 lines (53 loc) · 1.61 KB
/
version.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
62
package client
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/blang/semver"
"github.com/telepresenceio/telepresence/v2/pkg/version"
)
// Version returns the version of this executable.
func Version() string {
return version.Version
}
func Semver() semver.Version {
return version.Structured()
}
func Executable() (string, error) {
return version.GetExecutable()
}
// GetInstallMechanism returns how the executable was installed on the machine.
func GetInstallMechanism() (string, error) {
execPath, err := os.Executable()
mechanism := "undetermined"
if err != nil {
wrapErr := fmt.Errorf("Unable to get exec path: %w", err)
return mechanism, wrapErr
}
return GetMechanismFromPath(execPath)
}
// GetMechanismFromPath is a helper function that contains most of the logic
// required for GetInstallMechanism, but enables us to test it since we can
// control the path passed in.
func GetMechanismFromPath(execPath string) (string, error) {
// Some package managers, like brew, symlink binaries into /usr/local/bin.
// We want to use the actual location of the executable when reporting metrics
// so we follow the symlink to get the actual binary path
mechanism := "undetermined"
binaryPath, err := filepath.EvalSymlinks(execPath)
if err != nil {
wrapErr := fmt.Errorf("error following executable symlink %s: %w", execPath, err)
return mechanism, wrapErr
}
switch {
case runtime.GOOS == "darwin" && strings.Contains(binaryPath, "Cellar"):
mechanism = "brew"
case strings.Contains(binaryPath, "docker"):
mechanism = "docker"
default:
mechanism = "website"
}
return mechanism, nil
}