Skip to content

Commit

Permalink
refine region parser
Browse files Browse the repository at this point in the history
  • Loading branch information
crimson-gao committed Feb 23, 2024
1 parent 18143f8 commit 0946ef5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions util/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import (

const ENDPOINT_REGEX_PATTERN = `^(?:http[s]?:\/\/)?([a-z-0-9]+)\.(?:sls|log)\.aliyuncs\.com$`

var regionSuffixs = []string{"-intranet", "-share", "-vpc"}

func ParseRegion(endpoint string) (string, error) {
var re = regexp.MustCompile(ENDPOINT_REGEX_PATTERN)
groups := re.FindStringSubmatch(endpoint)
if groups == nil {
return "", fmt.Errorf("invalid endpoint format: %s", endpoint)
}
region := groups[1]
region = strings.TrimSuffix(region, "-intranet")
region = strings.TrimSuffix(region, "-share")
for _, suffix := range regionSuffixs {
if strings.HasSuffix(region, suffix) {
return region[:len(region)-len(suffix)], nil
}
}
return region, nil
}

0 comments on commit 0946ef5

Please sign in to comment.