all: use strs.CutPrefix and strs.CutSuffix more

Updates places where we use HasPrefix + TrimPrefix to use the combined
function.

Updates #5309

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2022-11-18 10:13:14 -08:00
committed by Mihai Parparita
parent 41e1d336cc
commit 33520920c3
14 changed files with 46 additions and 34 deletions

View File

@@ -15,6 +15,8 @@ import (
"path"
"path/filepath"
"strings"
"tailscale.com/util/strs"
)
// CmdName returns either the base name of the current binary
@@ -41,9 +43,8 @@ func cmdName(exe string) string {
// 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(info, "\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
if goPkg, ok := strs.CutPrefix(line, "path\t"); ok { // like "tailscale.com/cmd/tailscale"
ret = path.Base(goPkg) // goPkg is always forward slashes; use path, not filepath
break
}
}