move the Env key out of internal packages

pull/1439/head
Antoine Toulme 2 years ago
parent 38b94668ea
commit 5b9212e240

@ -0,0 +1,11 @@
package common
type envKey string
// Env is a context key that can be used to set programmatically the environment
// gopsutil relies on to perform calls against the OS.
// Example of use:
//
// ctx := context.WithValue(context.Background(), Env, map[string]string{"HOST_PROC": "/myproc"})
// avg, err := load.AvgWithContext(ctx)
var Env = envKey("env")

@ -25,6 +25,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/shirou/gopsutil/v3/common"
) )
var ( var (
@ -32,10 +34,6 @@ var (
ErrTimeout = errors.New("command timed out") ErrTimeout = errors.New("command timed out")
) )
type envKey string
var Env = envKey("string")
type Invoker interface { type Invoker interface {
Command(string, ...string) ([]byte, error) Command(string, ...string) ([]byte, error)
CommandWithContext(context.Context, string, ...string) ([]byte, error) CommandWithContext(context.Context, string, ...string) ([]byte, error)
@ -329,7 +327,7 @@ func PathExistsWithContents(filename string) bool {
// The context may optionally contain a map superseding os.Env. // The context may optionally contain a map superseding os.Env.
func GetEnvWithContext(ctx context.Context, key string, dfault string, combineWith ...string) string { func GetEnvWithContext(ctx context.Context, key string, dfault string, combineWith ...string) string {
var value string var value string
if env, ok := ctx.Value(Env).(map[string]string); ok { if env, ok := ctx.Value(common.Env).(map[string]string); ok {
value = env[key] value = env[key]
} }
if value == "" { if value == "" {

Loading…
Cancel
Save