Skip to content

Commit b9fdc2d

Browse files
committed
golink: allow -resolve-from-backup to resolve alias links
It's not uncommon to have multiple links pointing to the same destination, for example to handle different spellings of a word or because different people created them at different times. A common best practice is to select one as the "primary" link and point the others to that link as "aliases". This change updates resolveLink to follow those aliases so that the final destination is returned when using `golink -resolve-from-backup`. Signed-off-by: Will Norris <[email protected]>
1 parent 7fa6e00 commit b9fdc2d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

golink.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,5 +667,11 @@ func resolveLink(link string) (string, error) {
667667
if err != nil {
668668
return "", err
669669
}
670-
return expandLink(l.Long, expandEnv{Now: time.Now().UTC(), Path: remainder})
670+
dst, err := expandLink(l.Long, expandEnv{Now: time.Now().UTC(), Path: remainder})
671+
if err == nil {
672+
if u, uErr := url.Parse(dst); uErr == nil && (u.Hostname() == "" || u.Hostname() == *hostname) {
673+
dst, err = resolveLink(dst)
674+
}
675+
}
676+
return dst, err
671677
}

golink_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ func TestResolveLink(t *testing.T) {
103103
}
104104
db.Save(&Link{Short: "meet", Long: "https://meet.google.com/lookup/"})
105105
db.Save(&Link{Short: "cs", Long: "http://codesearch/{{with .Path}}search?q={{.}}{{end}}"})
106+
db.Save(&Link{Short: "m", Long: "http://go/meet"})
107+
db.Save(&Link{Short: "chat", Long: "/meet"})
106108

107109
tests := []struct {
108110
link string
@@ -137,6 +139,16 @@ func TestResolveLink(t *testing.T) {
137139
link: "cs/term",
138140
want: "http://codesearch/search?q=term",
139141
},
142+
{
143+
// aliased go links with hostname
144+
link: "m/foo",
145+
want: "https://meet.google.com/lookup/foo",
146+
},
147+
{
148+
// aliased go links without hostname
149+
link: "chat/foo",
150+
want: "https://meet.google.com/lookup/foo",
151+
},
140152
}
141153
for _, tt := range tests {
142154
name := "golink " + tt.link

0 commit comments

Comments
 (0)