...

Source file src/go.opentelemetry.io/otel/internal/internaltest/env.go

Documentation: go.opentelemetry.io/otel/internal/internaltest

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/internaltest/env.go.tmpl
     3  
     4  // Copyright The OpenTelemetry Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package internaltest // import "go.opentelemetry.io/otel/internal/internaltest"
    19  
    20  import (
    21  	"os"
    22  )
    23  
    24  type Env struct {
    25  	Name   string
    26  	Value  string
    27  	Exists bool
    28  }
    29  
    30  // EnvStore stores and recovers environment variables.
    31  type EnvStore interface {
    32  	// Records the environment variable into the store.
    33  	Record(key string)
    34  
    35  	// Restore recovers the environment variables in the store.
    36  	Restore() error
    37  }
    38  
    39  var _ EnvStore = (*envStore)(nil)
    40  
    41  type envStore struct {
    42  	store map[string]Env
    43  }
    44  
    45  func (s *envStore) add(env Env) {
    46  	s.store[env.Name] = env
    47  }
    48  
    49  func (s *envStore) Restore() error {
    50  	var err error
    51  	for _, v := range s.store {
    52  		if v.Exists {
    53  			err = os.Setenv(v.Name, v.Value)
    54  		} else {
    55  			err = os.Unsetenv(v.Name)
    56  		}
    57  		if err != nil {
    58  			return err
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func (s *envStore) setEnv(key, value string) error {
    65  	s.Record(key)
    66  
    67  	err := os.Setenv(key, value)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return nil
    72  }
    73  
    74  func (s *envStore) Record(key string) {
    75  	originValue, exists := os.LookupEnv(key)
    76  	s.add(Env{
    77  		Name:   key,
    78  		Value:  originValue,
    79  		Exists: exists,
    80  	})
    81  }
    82  
    83  func NewEnvStore() EnvStore {
    84  	return newEnvStore()
    85  }
    86  
    87  func newEnvStore() *envStore {
    88  	return &envStore{store: make(map[string]Env)}
    89  }
    90  
    91  func SetEnvVariables(env map[string]string) (EnvStore, error) {
    92  	envStore := newEnvStore()
    93  
    94  	for k, v := range env {
    95  		err := envStore.setEnv(k, v)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  	}
   100  	return envStore, nil
   101  }
   102  

View as plain text