github.com/lyraproj/hiera@v1.0.0-rc4/provider/environment.go (about)

     1  package provider
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/lyraproj/dgo/dgo"
     8  	"github.com/lyraproj/dgo/vf"
     9  	"github.com/lyraproj/hierasdk/hiera"
    10  )
    11  
    12  // Environment is a LookupKey function that performs a lookup in the current environment. The key can either be just
    13  // "env" in which case all current environment variables will be returned as an OrderedMap, or
    14  // prefixed with "env::" in which case the rest of the key is interpreted as the environment variable
    15  // to look for.
    16  func Environment(_ hiera.ProviderContext, key string) dgo.Value {
    17  	if key == `env` {
    18  		env := os.Environ()
    19  		em := vf.MapWithCapacity(len(env))
    20  		for _, ev := range env {
    21  			if ei := strings.IndexRune(ev, '='); ei > 0 {
    22  				em.Put(ev[:ei], ev[ei+1:])
    23  			}
    24  		}
    25  		return em
    26  	}
    27  	if strings.HasPrefix(key, `env::`) {
    28  		// Rest of key is name of environment
    29  		if v, ok := os.LookupEnv(key[5:]); ok {
    30  			return vf.String(v)
    31  		}
    32  	}
    33  	return nil
    34  }