1package utils23import (4 "fmt"5 "path"6 "strings"7 "unicode"89 "github.com/charmbracelet/x/ansi"10)1112// SanitizeRepo returns a sanitized version of the given repository name.13func SanitizeRepo(repo string) string {14 repo = Sanitize(repo)15 // We need to use an absolute path for the path to be cleaned correctly.16 repo = strings.TrimPrefix(repo, "/")17 repo = "/" + repo1819 // We're using path instead of filepath here because this is not OS dependent20 // looking at you Windows21 repo = path.Clean(repo)22 repo = strings.TrimSuffix(repo, ".git")23 return repo[1:]24}2526// Sanitize strips ANSI escape codes from the given string.27func Sanitize(s string) string {28 return ansi.Strip(s)29}3031// ValidateUsername returns an error if any of the given usernames are invalid.32func ValidateUsername(username string) error {33 if username == "" {34 return fmt.Errorf("username cannot be empty")35 }3637 if !unicode.IsLetter(rune(username[0])) {38 return fmt.Errorf("username must start with a letter")39 }4041 for _, r := range username {42 if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' {43 return fmt.Errorf("username can only contain letters, numbers, and hyphens")44 }45 }4647 return nil48}4950// ValidateRepo returns an error if the given repository name is invalid.51func ValidateRepo(repo string) error {52 if repo == "" {53 return fmt.Errorf("repo cannot be empty")54 }5556 for _, r := range repo {57 if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' && r != '.' && r != '/' {58 return fmt.Errorf("repo can only contain letters, numbers, hyphens, underscores, periods, and slashes")59 }60 }6162 return nil63}