mirror of https://github.com/tailscale/tailscale/
version: add CmdName func for future use by logpolicy
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: I02a7c907844f71242ef06ed097f2a92ece7ae091pull/78/head
parent
cf1e386cbd
commit
f266e2d1eb
@ -0,0 +1,41 @@
|
||||
// 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.
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"rsc.io/goversion/version"
|
||||
)
|
||||
|
||||
// CmdName returns either the base name of the current binary
|
||||
// using os.Executable. If os.Executable fails (it sholdn't), then
|
||||
// "cmd" is returned.
|
||||
func CmdName() string {
|
||||
e, err := os.Executable()
|
||||
if err != nil {
|
||||
return "cmd"
|
||||
}
|
||||
var ret string
|
||||
v, err := version.ReadExe(e)
|
||||
if err != nil {
|
||||
ret = strings.TrimSuffix(strings.ToLower(e), ".exe")
|
||||
} else {
|
||||
// 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") {
|
||||
ret = path.Base(strings.TrimPrefix(line, "path\t"))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if ret == "" {
|
||||
return "cmd"
|
||||
}
|
||||
return ret
|
||||
}
|
Loading…
Reference in New Issue