github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/dos/exe.go (about) 1 package dos 2 3 import ( 4 "context" 5 "os" 6 ) 7 8 // Exe is an abstraction of the executable related functions with the same name in the os package. 9 type Exe interface { 10 Executable() (string, error) 11 } 12 13 type exeKey struct{} 14 15 func WithExe(ctx context.Context, exe Exe) context.Context { 16 return context.WithValue(ctx, exeKey{}, exe) 17 } 18 19 // ExeAPI returns the Exe that has been registered with the given context, or 20 // the instance that delegates to the corresponding functions in the os package. 21 func ExeAPI(ctx context.Context) Exe { 22 if e, ok := ctx.Value(exeKey{}).(Exe); ok { 23 return e 24 } 25 return osExe{} 26 } 27 28 func Executable(ctx context.Context) (string, error) { 29 return ExeAPI(ctx).Executable() 30 } 31 32 type osExe struct{} 33 34 func (osExe) Executable() (string, error) { 35 return os.Executable() 36 }