func InterpolateStringWithEnvVar()

in wskenv/environment.go [50:117]


func InterpolateStringWithEnvVar(key interface{}) interface{} {
	// Assure the key itself is not nil
	if key == nil {
		return nil
	}

	if reflect.TypeOf(key).String() == "string" {
		keystr := key.(string)
		if isValidEnvironmentVar(keystr) {
			// retrieve the value of the env. var. from the host system.
			var thisValue string
			//split the string with ${}
			//test if the substr is a environment var
			//if it is, replace it with the value
			f := func(c rune) bool {
				return c == '$' || c == '{' || c == '}'
			}
			for _, substr := range strings.FieldsFunc(keystr, f) {
				//if the substr is a $${ENV_VAR}
				// return ${ENV_VAR}
				if strings.Contains(keystr, "$${"+substr+"}") {
					keystr = strings.Replace(keystr, "$${"+substr+"}", "${"+substr+"}", -1)
					//if the substr is a $ENV_VAR
					// return interpolated string using env. variable
				} else if strings.Contains(keystr, "$"+substr) {
					thisValue = os.Getenv(substr)
					// TODO(920) disabling the warning for now, since we want to get rid of it eventually
					// TODO(920) please add/remove this based on what we decide in issue #920

					/** Fixes #797
					if thisValue == "" {
						warningString := wski18n.T(
							wski18n.ID_WARN_MISSING_ENVIRONMENT_VARIABLE,
							map[string]interface{}{
								wski18n.KEY_VALUE: keystr})
						wskprint.PrintOpenWhiskWarning(warningString)
					}
					**/

					keystr = strings.Replace(keystr, "$"+substr, thisValue, -1)
					//if the substr is a ${ENV_VAR}
					// return interpolated string using env. variable
				} else if strings.Contains(keystr, "${"+substr+"}") {
					thisValue = os.Getenv(substr)
					// TODO(920) disabling the warning for now, since we want to get rid of it eventually
					// TODO(920) please add/remove this based on what we decide in issue #920

					/** Fixes #797
					if thisValue == "" {
						warningString := wski18n.T(
							wski18n.ID_WARN_MISSING_ENVIRONMENT_VARIABLE,
							map[string]interface{}{
								wski18n.KEY_VALUE: keystr})
						wskprint.PrintOpenWhiskWarning(warningString)
					}
					**/

					keystr = strings.Replace(keystr, "${"+substr+"}", thisValue, -1)
				}
			}
			return keystr
		}

		// The key was not a valid env. variable, simply return it as the value itself (of type string)
		return keystr
	}
	return key
}