Example #1
0
func envVarOrEmptyMap(yamlMap generic.Map, errs *[]error) *map[string]interface{} {
	key := "env"
	switch envVars := yamlMap.Get(key).(type) {
	case nil:
		aMap := make(map[string]interface{}, 0)
		return &aMap
	case map[string]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case map[interface{}]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case generic.Map:
		merrs := validateEnvVars(envVars)
		if merrs != nil {
			*errs = append(*errs, merrs...)
			return nil
		}

		result := make(map[string]interface{}, envVars.Count())
		generic.Each(envVars, func(key, value interface{}) {
			result[key.(string)] = value
		})

		return &result
	default:
		*errs = append(*errs, fmt.Errorf(T("Expected {{.Name}} to be a set of key => value, but it was a {{.Type}}.",
			map[string]interface{}{"Name": key, "Type": envVars})))
		return nil
	}
}
Example #2
0
func validateEnvVars(input generic.Map) (errs []error) {
	generic.Each(input, func(key, value interface{}) {
		if value == nil {
			errs = append(errs, errors.New(fmt.Sprintf("env var '%s' should not be null", key)))
		}
	})
	return
}
Example #3
0
func validateEnvVars(input generic.Map) (errs []error) {
	generic.Each(input, func(key, value interface{}) {
		if value == nil {
			errs = append(errs, fmt.Errorf(T("env var '{{.PropertyName}}' should not be null",
				map[string]interface{}{"PropertyName": key})))
		}
	})
	return
}
Example #4
0
func checkForNulls(yamlMap generic.Map) (errs []error) {
	generic.Each(yamlMap, func(key interface{}, value interface{}) {
		if key == "command" || key == "buildpack" {
			return
		}
		if value == nil {
			errs = append(errs, errors.NewWithFmt(T("{{.PropertyName}} should not be null", map[string]interface{}{"PropertyName": key})))
		}
	})

	return
}
Example #5
0
func checkForNulls(yamlMap generic.Map) (errs []error) {
	generic.Each(yamlMap, func(key interface{}, value interface{}) {
		if key == "command" || key == "buildpack" {
			return
		}
		if value == nil {
			errs = append(errs, errors.NewWithFmt("%s should not be null", key))
		}
	})

	return
}
Example #6
0
func expandProperties(input interface{}, babbler words.WordGenerator) (output interface{}, errs []error) {
	switch input := input.(type) {
	case string:
		match := propertyRegex.FindStringSubmatch(input)
		if match != nil {
			if match[0] == "${random-word}" {
				output = strings.Replace(input, "${random-word}", strings.ToLower(babbler.Babble()), -1)
			} else {
				err := errors.NewWithFmt(T("Property '{{.PropertyName}}' found in manifest. This feature is no longer supported. Please remove it and try again.",
					map[string]interface{}{"PropertyName": match[0]}))
				errs = append(errs, err)
			}
		} else {
			output = input
		}
	case []interface{}:
		outputSlice := make([]interface{}, len(input))
		for index, item := range input {
			itemOutput, itemErrs := expandProperties(item, babbler)
			outputSlice[index] = itemOutput
			errs = append(errs, itemErrs...)
		}
		output = outputSlice
	case map[interface{}]interface{}:
		outputMap := make(map[interface{}]interface{})
		for key, value := range input {
			itemOutput, itemErrs := expandProperties(value, babbler)
			outputMap[key] = itemOutput
			errs = append(errs, itemErrs...)
		}
		output = outputMap
	case generic.Map:
		outputMap := generic.NewMap()
		generic.Each(input, func(key, value interface{}) {
			itemOutput, itemErrs := expandProperties(value, babbler)
			outputMap.Set(key, itemOutput)
			errs = append(errs, itemErrs...)
		})
		output = outputMap
	default:
		output = input
	}

	return
}
Example #7
0
func envVarOrEmptyMap(yamlMap generic.Map, errs *[]error) *map[string]string {
	key := "env"
	switch envVars := yamlMap.Get(key).(type) {
	case nil:
		aMap := make(map[string]string, 0)
		return &aMap
	case map[string]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case map[interface{}]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case generic.Map:
		merrs := validateEnvVars(envVars)
		if merrs != nil {
			*errs = append(*errs, merrs...)
			return nil
		}

		result := make(map[string]string, envVars.Count())
		generic.Each(envVars, func(key, value interface{}) {

			switch value.(type) {
			case string:
				result[key.(string)] = value.(string)
			case int64, int, int32:
				result[key.(string)] = fmt.Sprintf("%d", value)
			case float32, float64:
				result[key.(string)] = fmt.Sprintf("%f", value)
			default:
				*errs = append(*errs, errors.NewWithFmt(T("Expected environment variable {{.PropertyName}} to have a string value, but it was a {{.PropertyType}}.",
					map[string]interface{}{"PropertyName": key, "PropertyType": value})))
			}

		})
		return &result
	default:
		*errs = append(*errs, errors.NewWithFmt(T("Expected {{.Name}} to be a set of key => value, but it was a {{.Type}}.",
			map[string]interface{}{"Name": key, "Type": envVars})))
		return nil
	}
}
Example #8
0
func checkForNulls(yamlMap generic.Map) error {
	var errs []error
	generic.Each(yamlMap, func(key interface{}, value interface{}) {
		if key == "command" || key == "buildpack" {
			return
		}
		if value == nil {
			errs = append(errs, fmt.Errorf(T("{{.PropertyName}} should not be null", map[string]interface{}{"PropertyName": key})))
		}
	})

	if len(errs) > 0 {
		message := ""
		for i := range errs {
			message = message + fmt.Sprintf("%s\n", errs[i].Error())
		}
		return errors.New(message)
	}

	return nil
}
Example #9
0
func expandProperties(input interface{}, babbler generator.WordGenerator) (interface{}, error) {
	var errs []error
	var output interface{}

	switch input := input.(type) {
	case string:
		match := propertyRegex.FindStringSubmatch(input)
		if match != nil {
			if match[0] == "${random-word}" {
				output = strings.Replace(input, "${random-word}", strings.ToLower(babbler.Babble()), -1)
			} else {
				err := fmt.Errorf(T("Property '{{.PropertyName}}' found in manifest. This feature is no longer supported. Please remove it and try again.",
					map[string]interface{}{"PropertyName": match[0]}))
				errs = append(errs, err)
			}
		} else {
			output = input
		}
	case []interface{}:
		outputSlice := make([]interface{}, len(input))
		for index, item := range input {
			itemOutput, itemErr := expandProperties(item, babbler)
			if itemErr != nil {
				errs = append(errs, itemErr)
				break
			}
			outputSlice[index] = itemOutput
		}
		output = outputSlice
	case map[interface{}]interface{}:
		outputMap := make(map[interface{}]interface{})
		for key, value := range input {
			itemOutput, itemErr := expandProperties(value, babbler)
			if itemErr != nil {
				errs = append(errs, itemErr)
				break
			}
			outputMap[key] = itemOutput
		}
		output = outputMap
	case generic.Map:
		outputMap := generic.NewMap()
		generic.Each(input, func(key, value interface{}) {
			itemOutput, itemErr := expandProperties(value, babbler)
			if itemErr != nil {
				errs = append(errs, itemErr)
				return
			}
			outputMap.Set(key, itemOutput)
		})
		output = outputMap
	default:
		output = input
	}

	if len(errs) > 0 {
		message := ""
		for _, err := range errs {
			message = message + fmt.Sprintf("%s\n", err.Error())
		}
		return nil, errors.New(message)
	}

	return output, nil
}