github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/version.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  
    10  	"github.com/blang/semver/v4"
    11  
    12  	"github.com/telepresenceio/telepresence/v2/pkg/version"
    13  )
    14  
    15  var DisplayName = "Telepresence" //nolint:gochecknoglobals // extension point
    16  
    17  // Version returns the version of this executable.
    18  func Version() string {
    19  	return version.Version
    20  }
    21  
    22  func Semver() semver.Version {
    23  	return version.Structured
    24  }
    25  
    26  func Executable() (string, error) {
    27  	return version.GetExecutable()
    28  }
    29  
    30  // GetInstallMechanism returns how the executable was installed on the machine.
    31  func GetInstallMechanism() (string, error) {
    32  	execPath, err := os.Executable()
    33  	mechanism := "undetermined"
    34  	if err != nil {
    35  		wrapErr := fmt.Errorf("unable to get exec path: %w", err)
    36  		return mechanism, wrapErr
    37  	}
    38  
    39  	return GetMechanismFromPath(execPath)
    40  }
    41  
    42  // GetMechanismFromPath is a helper function that contains most of the logic
    43  // required for GetInstallMechanism, but enables us to test it since we can
    44  // control the path passed in.
    45  func GetMechanismFromPath(execPath string) (string, error) {
    46  	// Some package managers, like brew, symlink binaries into /usr/local/bin.
    47  	// We want to use the actual location of the executable when reporting metrics
    48  	// so we follow the symlink to get the actual binary path
    49  	mechanism := "undetermined"
    50  	binaryPath, err := filepath.EvalSymlinks(execPath)
    51  	if err != nil {
    52  		wrapErr := fmt.Errorf("error following executable symlink %s: %w", execPath, err)
    53  		return mechanism, wrapErr
    54  	}
    55  	switch {
    56  	case runtime.GOOS == "darwin" && strings.Contains(binaryPath, "Cellar"):
    57  		mechanism = "brew"
    58  	case strings.Contains(binaryPath, "docker"):
    59  		mechanism = "docker"
    60  	default:
    61  		mechanism = "website"
    62  	}
    63  	return mechanism, nil
    64  }