subproc: lazy lookup nomad binary in self call (#20231)

This commit is contained in:
Seth Hoenig
2024-03-26 12:33:06 -05:00
committed by GitHub
parent 2fde4a0c93
commit bd2a809135
2 changed files with 32 additions and 16 deletions

View File

@@ -8,14 +8,18 @@ import (
"os"
"os/exec"
"strings"
"sync"
)
var (
// executable is the executable of this process
executable string
once sync.Once
)
func init() {
// Self returns the path to the executable of this process.
func Self() string {
once.Do(func() {
s, err := os.Executable()
if err != nil {
panic(fmt.Sprintf("failed to detect executable: %v", err))
@@ -29,9 +33,6 @@ func init() {
}
}
executable = s
}
// Self returns the path to the executable of this process.
func Self() string {
})
return executable
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package subproc
import (
"testing"
"github.com/shoenig/test/must"
)
func TestSelf(t *testing.T) {
value := Self()
must.NotEq(t, "", value)
}