github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/integration_test/itest/env.go (about)

     1  package itest
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"sigs.k8s.io/yaml"
    10  
    11  	"github.com/telepresenceio/telepresence/v2/pkg/dos"
    12  	"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
    13  	"github.com/telepresenceio/telepresence/v2/pkg/maps"
    14  )
    15  
    16  type itestConfig struct {
    17  	Env map[string]string `json:"env,omitempty"`
    18  }
    19  
    20  func LoadEnv(ctx context.Context) context.Context {
    21  	cf := filepath.Join(filelocation.AppUserConfigDir(ctx), "itest.yml")
    22  	data, err := os.ReadFile(cf)
    23  	if err != nil {
    24  		if !os.IsNotExist(err) {
    25  			getT(ctx).Fatal(cf, err)
    26  		}
    27  		return ctx
    28  	}
    29  
    30  	var ic itestConfig
    31  	if err := yaml.Unmarshal(data, &ic); err != nil {
    32  		getT(ctx).Fatal(cf, err)
    33  		return ctx
    34  	}
    35  
    36  	env := os.Environ()
    37  	dosEnv := make(dos.MapEnv, len(env))
    38  	for _, ep := range env {
    39  		if ix := strings.IndexByte(ep, '='); ix > 0 {
    40  			dosEnv[ep[:ix]] = ep[ix+1:]
    41  		}
    42  	}
    43  	maps.Merge(dosEnv, ic.Env)
    44  	return dos.WithEnv(ctx, dosEnv)
    45  }