github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/juju/osenv/vars.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package osenv
     5  
     6  import (
     7  	"runtime"
     8  	"strings"
     9  
    10  	"github.com/juju/utils/featureflag"
    11  )
    12  
    13  const (
    14  	JujuEnvEnvKey           = "JUJU_ENV"
    15  	JujuHomeEnvKey          = "JUJU_HOME"
    16  	JujuRepositoryEnvKey    = "JUJU_REPOSITORY"
    17  	JujuLoggingConfigEnvKey = "JUJU_LOGGING_CONFIG"
    18  	JujuFeatureFlagEnvKey   = "JUJU_DEV_FEATURE_FLAGS"
    19  
    20  	// Registry key containing juju related information
    21  	JujuRegistryKey = `HKLM:\SOFTWARE\juju-core`
    22  
    23  	// TODO(thumper): 2013-09-02 bug 1219630
    24  	// As much as I'd like to remove JujuContainerType now, it is still
    25  	// needed as MAAS still needs it at this stage, and we can't fix
    26  	// everything at once.
    27  	JujuContainerTypeEnvKey = "JUJU_CONTAINER_TYPE"
    28  
    29  	// JujuStatusIsoTimeEnvKey is the env var which if true, will cause status
    30  	// timestamps to be written in RFC3339 format.
    31  	JujuStatusIsoTimeEnvKey = "JUJU_STATUS_ISO_TIME"
    32  
    33  	// JujuCLIVersion is a numeric value (1, 2, 3 etc) representing
    34  	// the oldest CLI version which should be adhered to.
    35  	// This includes args and output.
    36  	// Default is 1.
    37  	JujuCLIVersion = "JUJU_CLI_VERSION"
    38  )
    39  
    40  // FeatureFlags returns a map that can be merged with os.Environ.
    41  func FeatureFlags() map[string]string {
    42  	result := make(map[string]string)
    43  	if envVar := featureflag.AsEnvironmentValue(); envVar != "" {
    44  		result[JujuFeatureFlagEnvKey] = envVar
    45  	}
    46  	return result
    47  }
    48  
    49  // MergeEnvironment will return the current environment updated with
    50  // all the values from newValues.  If current is nil, a new map is
    51  // created.  If current is not nil, it is mutated.
    52  func MergeEnvironment(current, newValues map[string]string) map[string]string {
    53  	if current == nil {
    54  		current = make(map[string]string)
    55  	}
    56  	if runtime.GOOS == "windows" {
    57  		return mergeEnvWin(current, newValues)
    58  	}
    59  	return mergeEnvUnix(current, newValues)
    60  }
    61  
    62  // mergeEnvUnix merges the two evironment variable lists in a case sensitive way.
    63  func mergeEnvUnix(current, newValues map[string]string) map[string]string {
    64  	for key, value := range newValues {
    65  		current[key] = value
    66  	}
    67  	return current
    68  }
    69  
    70  // mergeEnvWin merges the two environment variable lists in a case insensitive,
    71  // but case preserving way.  Thus, if FOO=bar is set, and newValues has foo=baz,
    72  // then the resultant map will contain FOO=baz.
    73  func mergeEnvWin(current, newValues map[string]string) map[string]string {
    74  	uppers := make(map[string]string, len(current))
    75  	news := map[string]string{}
    76  	for k, v := range current {
    77  		uppers[strings.ToUpper(k)] = v
    78  	}
    79  
    80  	for k, v := range newValues {
    81  		up := strings.ToUpper(k)
    82  		if _, ok := uppers[up]; ok {
    83  			uppers[up] = v
    84  		} else {
    85  			news[k] = v
    86  		}
    87  	}
    88  
    89  	for k := range current {
    90  		current[k] = uppers[strings.ToUpper(k)]
    91  	}
    92  	for k, v := range news {
    93  		current[k] = v
    94  	}
    95  	return current
    96  }