github.com/blend/go-sdk@v1.20220411.3/env/singleton.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package env
     9  
    10  import (
    11  	"os"
    12  	"sync"
    13  )
    14  
    15  var (
    16  	_env     Vars
    17  	_envLock = sync.Mutex{}
    18  )
    19  
    20  // Env returns the current env var set.
    21  func Env() Vars {
    22  	if _env == nil {
    23  		_envLock.Lock()
    24  		defer _envLock.Unlock()
    25  		if _env == nil {
    26  			_env = New(OptEnviron(os.Environ()...))
    27  		}
    28  	}
    29  	return _env
    30  }
    31  
    32  // SetEnv sets the env vars.
    33  func SetEnv(vars Vars) {
    34  	_envLock.Lock()
    35  	_env = vars
    36  	_envLock.Unlock()
    37  }
    38  
    39  // Restore sets .Env() to the current os environment.
    40  func Restore() {
    41  	SetEnv(New(OptEnviron(os.Environ()...)))
    42  }
    43  
    44  // Clear sets .Env() to an empty env var set.
    45  func Clear() {
    46  	SetEnv(New())
    47  }