Skip to content

Commit

Permalink
feat: support specify mirror by shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 15, 2022
1 parent d092f34 commit 03b8059
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
20 changes: 19 additions & 1 deletion linux/custom.go → state/custom.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package linux
package state

type buildin_custom_mirror struct {
url string
Expand All @@ -22,3 +22,21 @@ var BUILDIN_CUSTOM_DEBIAN_MIRRORS = []buildin_custom_mirror{
{url: "https://repo.huaweicloud.com/debian/", alias: "cn:huawei"},
{url: "https://mirrors.cloud.tencent.com/debian/", alias: "cn:tencent"},
}

func getUbuntuMirrorByAliases(alias string) string {
for _, mirror := range BUILDIN_CUSTOM_UBUNTU_MIRRORS {
if mirror.alias == alias {
return mirror.url
}
}
return ""
}

func getDebianMirrorByAliases(alias string) string {
for _, mirror := range BUILDIN_CUSTOM_DEBIAN_MIRRORS {
if mirror.alias == alias {
return mirror.url
}
}
return ""
}
20 changes: 18 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ func GetProxyMode() int {
return PROXY_MODE
}

func SetUbuntuMirror(mirror string) {
func SetUbuntuMirror(input string) {
mirror := ""
alias := getUbuntuMirrorByAliases(input)
if alias == "" {
mirror = input
} else {
mirror = alias
}

url, err := url.Parse(mirror)
if err != nil || mirror == "" {
UBUNTU_MIRROR = nil
Expand All @@ -33,7 +41,15 @@ func ResetUbuntuMirror() {
UBUNTU_MIRROR = nil
}

func SetDebianMirror(mirror string) {
func SetDebianMirror(input string) {
mirror := ""
alias := getDebianMirrorByAliases(input)
if alias == "" {
mirror = input
} else {
mirror = alias
}

url, err := url.Parse(mirror)
if err != nil || mirror == "" {
DEBIAN_MIRROR = nil
Expand Down

0 comments on commit 03b8059

Please sign in to comment.