func ToValidAppName()

in cmd/setupgh.go [152:189]


func ToValidAppName(name string) (string, error) {
	// replace all underscores with hyphens
	cleanedName := strings.ReplaceAll(name, "_", "-")
	// replace all spaces with hyphens
	cleanedName = strings.ReplaceAll(cleanedName, " ", "-")

	// remove leading non-alphanumeric characters
	for i, r := range cleanedName {
		if unicode.IsLetter(r) || unicode.IsNumber(r) {
			cleanedName = cleanedName[i:]
			break
		}
	}

	// remove trailing non-alphanumeric characters
	for i := len(cleanedName) - 1; i >= 0; i-- {
		r := rune(cleanedName[i])
		if unicode.IsLetter(r) || unicode.IsNumber(r) {
			cleanedName = cleanedName[:i+1]
			break
		}
	}

	// remove all characters except alphanumeric, '-', '.'
	var builder strings.Builder
	for _, r := range cleanedName {
		if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '-' {
			builder.WriteRune(r)
		}
	}

	// lowercase the name
	cleanedName = strings.ToLower(builder.String())
	if err := ValidateAppName(cleanedName); err != nil {
		return "", fmt.Errorf("app name '%s' could not be converted to a valid name: %w", name, err)
	}
	return cleanedName, nil
}