2020-02-19 19:22:12 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-12-19 10:16:44 +00:00
|
|
|
// +build go1.16,!ios !go1.16,!darwin !go1.16,!arm64
|
2020-04-07 16:28:09 +00:00
|
|
|
|
2020-02-19 19:22:12 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-07-30 14:59:43 +00:00
|
|
|
"path"
|
2020-07-30 06:59:06 +00:00
|
|
|
"path/filepath"
|
2020-02-19 19:22:12 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"rsc.io/goversion/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdName returns either the base name of the current binary
|
2020-06-08 17:30:16 +00:00
|
|
|
// using os.Executable. If os.Executable fails (it shouldn't), then
|
2020-02-19 19:22:12 +00:00
|
|
|
// "cmd" is returned.
|
|
|
|
func CmdName() string {
|
|
|
|
e, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return "cmd"
|
|
|
|
}
|
2020-07-30 14:59:43 +00:00
|
|
|
|
|
|
|
// fallbackName, the lowercase basename of the executable, is what we return if
|
|
|
|
// we can't find the Go module metadata embedded in the file.
|
|
|
|
fallbackName := filepath.Base(strings.TrimSuffix(strings.ToLower(e), ".exe"))
|
|
|
|
|
2020-02-19 19:22:12 +00:00
|
|
|
var ret string
|
|
|
|
v, err := version.ReadExe(e)
|
|
|
|
if err != nil {
|
2020-07-30 14:59:43 +00:00
|
|
|
return fallbackName
|
|
|
|
}
|
|
|
|
// v is like:
|
|
|
|
// "path\ttailscale.com/cmd/tailscale\nmod\ttailscale.com\t(devel)\t\ndep\tgithub.com/apenwarr/fixconsole\tv0.0.0-20191012055117-5a9f6489cc29\th1:muXWUcay7DDy1/hEQWrYlBy+g0EuwT70sBHg65SeUc4=\ndep\tgithub....
|
|
|
|
for _, line := range strings.Split(v.ModuleInfo, "\n") {
|
|
|
|
if strings.HasPrefix(line, "path\t") {
|
|
|
|
goPkg := strings.TrimPrefix(line, "path\t") // like "tailscale.com/cmd/tailscale"
|
|
|
|
ret = path.Base(goPkg) // goPkg is always forward slashes; use path, not filepath
|
|
|
|
break
|
2020-02-19 19:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ret == "" {
|
2020-07-30 14:59:43 +00:00
|
|
|
return fallbackName
|
2020-02-19 19:22:12 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|