mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
subproc: lazy lookup nomad binary in self call (#20231)
This commit is contained in:
@@ -8,30 +8,31 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// executable is the executable of this process
|
||||
executable string
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func init() {
|
||||
s, err := os.Executable()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to detect executable: %v", err))
|
||||
}
|
||||
|
||||
// when running tests, we need to use the real nomad binary,
|
||||
// and make sure you recompile between changes!
|
||||
if strings.HasSuffix(s, ".test") {
|
||||
if s, err = exec.LookPath("nomad"); err != nil {
|
||||
panic(fmt.Sprintf("failed to find nomad binary: %v", err))
|
||||
}
|
||||
}
|
||||
executable = s
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
// when running tests, we need to use the real nomad binary,
|
||||
// and make sure you recompile between changes!
|
||||
if strings.HasSuffix(s, ".test") {
|
||||
if s, err = exec.LookPath("nomad"); err != nil {
|
||||
panic(fmt.Sprintf("failed to find nomad binary: %v", err))
|
||||
}
|
||||
}
|
||||
executable = s
|
||||
})
|
||||
return executable
|
||||
}
|
||||
|
||||
15
helper/subproc/self_test.go
Normal file
15
helper/subproc/self_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user