github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	// If you are adding variables here that could be defined
    15  	// in a system and therefore changing the behavior on test
    16  	// suites please take a moment to add them to JujuOSEnvSuite
    17  	// setup so they are cleared before running the suites using
    18  	// it.
    19  
    20  	JujuModelEnvKey         = "JUJU_MODEL"
    21  	JujuXDGDataHomeEnvKey   = "JUJU_DATA"
    22  	JujuLoggingConfigEnvKey = "JUJU_LOGGING_CONFIG"
    23  	JujuFeatureFlagEnvKey   = "JUJU_DEV_FEATURE_FLAGS"
    24  
    25  	// JujuStartupLoggingConfigEnvKey if set is used to configure the initial
    26  	// logging before the command objects are even created to allow debugging
    27  	// of the command creation and initialisation process.
    28  	JujuStartupLoggingConfigEnvKey = "JUJU_STARTUP_LOGGING_CONFIG"
    29  
    30  	// Registry key containing juju related information
    31  	JujuRegistryKey = `HKLM:\SOFTWARE\juju-core`
    32  
    33  	// Registry value where the jujud password resides
    34  	JujuRegistryPasswordKey = `jujud-password`
    35  
    36  	// TODO(thumper): 2013-09-02 bug 1219630
    37  	// As much as I'd like to remove JujuContainerType now, it is still
    38  	// needed as MAAS still needs it at this stage, and we can't fix
    39  	// everything at once.
    40  	JujuContainerTypeEnvKey = "JUJU_CONTAINER_TYPE"
    41  
    42  	// JujuStatusIsoTimeEnvKey is the env var which if true, will cause status
    43  	// timestamps to be written in RFC3339 format.
    44  	JujuStatusIsoTimeEnvKey = "JUJU_STATUS_ISO_TIME"
    45  
    46  	// XDGDataHome is a path where data for the running user
    47  	// should be stored according to the xdg standard.
    48  	XDGDataHome = "XDG_DATA_HOME"
    49  )
    50  
    51  // FeatureFlags returns a map that can be merged with os.Environ.
    52  func FeatureFlags() map[string]string {
    53  	result := make(map[string]string)
    54  	if envVar := featureflag.AsEnvironmentValue(); envVar != "" {
    55  		result[JujuFeatureFlagEnvKey] = envVar
    56  	}
    57  	return result
    58  }
    59  
    60  // MergeEnvironment will return the current environment updated with
    61  // all the values from newValues.  If current is nil, a new map is
    62  // created.  If current is not nil, it is mutated.
    63  func MergeEnvironment(current, newValues map[string]string) map[string]string {
    64  	if current == nil {
    65  		current = make(map[string]string)
    66  	}
    67  	if runtime.GOOS == "windows" {
    68  		return mergeEnvWin(current, newValues)
    69  	}
    70  	return mergeEnvUnix(current, newValues)
    71  }
    72  
    73  // mergeEnvUnix merges the two evironment variable lists in a case sensitive way.
    74  func mergeEnvUnix(current, newValues map[string]string) map[string]string {
    75  	for key, value := range newValues {
    76  		current[key] = value
    77  	}
    78  	return current
    79  }
    80  
    81  // mergeEnvWin merges the two environment variable lists in a case insensitive,
    82  // but case preserving way.  Thus, if FOO=bar is set, and newValues has foo=baz,
    83  // then the resultant map will contain FOO=baz.
    84  func mergeEnvWin(current, newValues map[string]string) map[string]string {
    85  	uppers := make(map[string]string, len(current))
    86  	news := map[string]string{}
    87  	for k, v := range current {
    88  		uppers[strings.ToUpper(k)] = v
    89  	}
    90  
    91  	for k, v := range newValues {
    92  		up := strings.ToUpper(k)
    93  		if _, ok := uppers[up]; ok {
    94  			uppers[up] = v
    95  		} else {
    96  			news[k] = v
    97  		}
    98  	}
    99  
   100  	for k := range current {
   101  		current[k] = uppers[strings.ToUpper(k)]
   102  	}
   103  	for k, v := range news {
   104  		current[k] = v
   105  	}
   106  	return current
   107  }