utils.go

 1package utils
 2
 3import (
 4	"fmt"
 5	"path/filepath"
 6	"strings"
 7	"unicode"
 8)
 9
10// SanitizeRepo returns a sanitized version of the given repository name.
11func SanitizeRepo(repo string) string {
12	repo = strings.TrimPrefix(repo, "/")
13	repo = filepath.Clean(repo)
14	repo = strings.TrimSuffix(repo, ".git")
15	return repo
16}
17
18// ValidateUsername returns an error if any of the given usernames are invalid.
19func ValidateUsername(username string) error {
20	if username == "" {
21		return fmt.Errorf("username cannot be empty")
22	}
23
24	if !unicode.IsLetter(rune(username[0])) {
25		return fmt.Errorf("username must start with a letter")
26	}
27
28	for _, r := range username {
29		if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' {
30			return fmt.Errorf("username can only contain letters, numbers, and hyphens")
31		}
32	}
33
34	return nil
35}
36
37// ValidateRepo returns an error if the given repository name is invalid.
38func ValidateRepo(repo string) error {
39	if repo == "" {
40		return fmt.Errorf("repo cannot be empty")
41	}
42
43	fmt.Println("ACTUAL REPO NAME IS", repo)
44
45	for _, r := range repo {
46		if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' && r != '.' && r != '/' {
47			fmt.Println("INVALID CHAR", r, string(r))
48			return fmt.Errorf("repo can only contain letters, numbers, hyphens, underscores, periods, and slashes")
49		}
50	}
51
52	return nil
53}