11package core
22
33import (
4+ "fmt"
45 "regexp"
56 "strings"
67)
@@ -27,3 +28,33 @@ func GetRouteFromUrl(url string) (string, bool) {
2728
2829 return "" , false
2930}
31+
32+ func ParseRoute (route string , repoOnly bool ) (string , string , string , error ) {
33+ elements := strings .FieldsFunc (route , func (char rune ) bool { return char == '/' })
34+ validElementPattern := regexp .MustCompile (`^[\w\.-]+$` )
35+ for _ , e := range elements {
36+ if ! validElementPattern .MatchString (e ) {
37+ return "" , "" , "" ,
38+ fmt .Errorf ("invalid element '%s'; route may only contain alphanumeric characters, '.', '_', and/or '-'" , e )
39+ }
40+ if e == "." || e == ".." {
41+ return "" , "" , "" , fmt .Errorf ("invalid route element '%s'" , e )
42+ }
43+ }
44+
45+ switch len (elements ) {
46+ case 0 :
47+ return "" , "" , "" , fmt .Errorf ("empty route" )
48+ case 1 :
49+ return "" , "" , "" , fmt .Errorf ("route has owner, but no repo" )
50+ case 2 :
51+ return elements [0 ], elements [1 ], "" , nil
52+ case 3 :
53+ if repoOnly {
54+ return "" , "" , "" , fmt .Errorf ("route is too deep" )
55+ }
56+ return elements [0 ], elements [1 ], elements [2 ], nil
57+ default :
58+ return "" , "" , "" , fmt .Errorf ("route is too deep" )
59+ }
60+ }
0 commit comments