github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	// Registry value where the jujud password resides
    24  	JujuRegistryPasswordKey = `jujud-password`
    25  
    26  	// TODO(thumper): 2013-09-02 bug 1219630
    27  	// As much as I'd like to remove JujuContainerType now, it is still
    28  	// needed as MAAS still needs it at this stage, and we can't fix
    29  	// everything at once.
    30  	JujuContainerTypeEnvKey = "JUJU_CONTAINER_TYPE"
    31  
    32  	// JujuStatusIsoTimeEnvKey is the env var which if true, will cause status
    33  	// timestamps to be written in RFC3339 format.
    34  	JujuStatusIsoTimeEnvKey = "JUJU_STATUS_ISO_TIME"
    35  
    36  	// JujuCLIVersion is a numeric value (1, 2, 3 etc) representing
    37  	// the oldest CLI version which should be adhered to.
    38  	// This includes args and output.
    39  	// Default is 1.
    40  	JujuCLIVersion = "JUJU_CLI_VERSION"
    41  )
    42  
    43  // FeatureFlags returns a map that can be merged with os.Environ.
    44  func FeatureFlags() map[string]string {
    45  	result := make(map[string]string)
    46  	if envVar := featureflag.AsEnvironmentValue(); envVar != "" {
    47  		result[JujuFeatureFlagEnvKey] = envVar
    48  	}
    49  	return result
    50  }
    51  
    52  // MergeEnvironment will return the current environment updated with
    53  // all the values from newValues.  If current is nil, a new map is
    54  // created.  If current is not nil, it is mutated.
    55  func MergeEnvironment(current, newValues map[string]string) map[string]string {
    56  	if current == nil {
    57  		current = make(map[string]string)
    58  	}
    59  	if runtime.GOOS == "windows" {
    60  		return mergeEnvWin(current, newValues)
    61  	}
    62  	return mergeEnvUnix(current, newValues)
    63  }
    64  
    65  // mergeEnvUnix merges the two evironment variable lists in a case sensitive way.
    66  func mergeEnvUnix(current, newValues map[string]string) map[string]string {
    67  	for key, value := range newValues {
    68  		current[key] = value
    69  	}
    70  	return current
    71  }
    72  
    73  // mergeEnvWin merges the two environment variable lists in a case insensitive,
    74  // but case preserving way.  Thus, if FOO=bar is set, and newValues has foo=baz,
    75  // then the resultant map will contain FOO=baz.
    76  func mergeEnvWin(current, newValues map[string]string) map[string]string {
    77  	uppers := make(map[string]string, len(current))
    78  	news := map[string]string{}
    79  	for k, v := range current {
    80  		uppers[strings.ToUpper(k)] = v
    81  	}
    82  
    83  	for k, v := range newValues {
    84  		up := strings.ToUpper(k)
    85  		if _, ok := uppers[up]; ok {
    86  			uppers[up] = v
    87  		} else {
    88  			news[k] = v
    89  		}
    90  	}
    91  
    92  	for k := range current {
    93  		current[k] = uppers[strings.ToUpper(k)]
    94  	}
    95  	for k, v := range news {
    96  		current[k] = v
    97  	}
    98  	return current
    99  }