Skip to content

Commit 8c202c5

Browse files
zhsjanthonyfok
authored andcommitted
Refactor shortHostName
Move knownHosts to a map to simplify the code, and fix the publicsuffix guess, input should be fqdn.
1 parent 88748fe commit 8c202c5

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

make.go

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -537,48 +537,35 @@ func normalizeDebianProgramName(str string) string {
537537
}
538538

539539
func shortHostName(gopkg string, allowUnknownHoster bool) (host string, err error) {
540+
541+
knownHosts := map[string]string{
542+
"github.com": "github",
543+
"code.google.com": "googlecode",
544+
"cloud.google.com": "googlecloud",
545+
"gopkg.in": "gopkg",
546+
"golang.org": "golang",
547+
"google.golang.org": "google",
548+
"gitlab.com": "gitlab",
549+
"bitbucket.org": "bitbucket",
550+
"bazil.org": "bazil",
551+
"blitiri.com.ar": "blitiri",
552+
"pault.ag": "pault",
553+
"howett.net": "howett",
554+
"go4.org": "go4",
555+
"salsa.debian.org": "debian",
556+
}
540557
parts := strings.Split(gopkg, "/")
541558
fqdn := parts[0]
542-
543-
switch fqdn {
544-
case "github.com":
545-
host = "github"
546-
case "code.google.com":
547-
host = "googlecode"
548-
case "cloud.google.com":
549-
host = "googlecloud"
550-
case "gopkg.in":
551-
host = "gopkg"
552-
case "golang.org":
553-
host = "golang"
554-
case "google.golang.org":
555-
host = "google"
556-
case "gitlab.com":
557-
host = "gitlab"
558-
case "bitbucket.org":
559-
host = "bitbucket"
560-
case "bazil.org":
561-
host = "bazil"
562-
case "blitiri.com.ar":
563-
host = "blitiri"
564-
case "pault.ag":
565-
host = "pault"
566-
case "howett.net":
567-
host = "howett"
568-
case "go4.org":
569-
host = "go4"
570-
case "salsa.debian.org":
571-
host = "debian"
572-
default:
573-
if allowUnknownHoster {
574-
suffix, _ := publicsuffix.PublicSuffix(host)
575-
host = fqdn[:len(fqdn)-len(suffix)-len(".")]
576-
log.Printf("WARNING: Using %q as canonical hostname for %q. If that is not okay, please file a bug against %s.\n", host, fqdn, os.Args[0])
577-
} else {
578-
err = fmt.Errorf("unknown hoster %q", fqdn)
579-
}
559+
if host, ok := knownHosts[fqdn]; ok {
560+
return host, nil
561+
}
562+
if !allowUnknownHoster {
563+
return "", fmt.Errorf("unknown hoster %q", fqdn)
580564
}
581-
return host, err
565+
suffix, _ := publicsuffix.PublicSuffix(fqdn)
566+
host = fqdn[:len(fqdn)-len(suffix)-len(".")]
567+
log.Printf("WARNING: Using %q as canonical hostname for %q. If that is not okay, please file a bug against %s.\n", host, fqdn, os.Args[0])
568+
return host, nil
582569
}
583570

584571
// debianNameFromGopkg maps a Go package repo path to a Debian package name,

0 commit comments

Comments
 (0)