Skip to content

Commit 7fd2d35

Browse files
committed
include current user resolving link in expandEnv
Add "User" to the expansion environment for links. The intent here it support personalized go links such as: go/mycal => https://calendar.google.com/calendar/embed?src={{.User}} That's not a terribly interesting example, but there are others I intend to use internally. Signed-off-by: Will Norris <[email protected]>
1 parent 9c46bb3 commit 7fd2d35

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

golink.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
363363
stats.dirty[link.Short]++
364364
stats.mu.Unlock()
365365

366-
target, err := expandLink(link.Long, expandEnv{Now: time.Now().UTC(), Path: remainder})
366+
currentUser, _ := currentUser(r)
367+
368+
target, err := expandLink(link.Long, expandEnv{Now: time.Now().UTC(), Path: remainder, User: currentUser})
367369
if err != nil {
368370
log.Printf("expanding %q: %v", link.Long, err)
369371
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -431,6 +433,10 @@ type expandEnv struct {
431433
// Path is the remaining path after short name. For example, in
432434
// "http://go/who/amelie", Path is "amelie".
433435
Path string
436+
437+
// User is the current user, if any.
438+
// For example, "[email protected]" or "foo@github".
439+
User string
434440
}
435441

436442
var expandFuncMap = texttemplate.FuncMap{

golink_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010

1111
func TestExpandLink(t *testing.T) {
1212
tests := []struct {
13-
name string
14-
long string
15-
now time.Time
16-
remainder string
17-
want string
13+
name string // test name
14+
long string // long URL for golink
15+
now time.Time // current time
16+
user string // current user resolving link
17+
remainder string // remainder of URL path after golink name
18+
want string // expected redirect URL
1819
}{
1920
{
2021
name: "dont-mangle-escapes",
@@ -45,6 +46,12 @@ func TestExpandLink(t *testing.T) {
4546
want: "https://roamresearch.com/#/app/ts-corp/page/06-02-2022",
4647
now: time.Date(2022, 06, 02, 1, 2, 3, 4, time.UTC),
4748
},
49+
{
50+
name: "var-expansions-user",
51+
long: `http://host.com/{{.User}}`,
52+
53+
want: "http://host.com/[email protected]",
54+
},
4855
{
4956
name: "template-no-path",
5057
long: "https://calendar.google.com/{{with .Path}}calendar/embed?mode=week&src={{.}}@tailscale.com{{end}}",
@@ -58,7 +65,7 @@ func TestExpandLink(t *testing.T) {
5865
},
5966
{
6067
name: "template-with-pathescape-func",
61-
long: "http://host.com/{{QueryEscape .Path}}",
68+
long: "http://host.com/{{PathEscape .Path}}",
6269
remainder: "a/b",
6370
want: "http://host.com/a%2Fb",
6471
},
@@ -77,7 +84,7 @@ func TestExpandLink(t *testing.T) {
7784
}
7885
for _, tt := range tests {
7986
t.Run(tt.name, func(t *testing.T) {
80-
got, err := expandLink(tt.long, expandEnv{Now: tt.now, Path: tt.remainder})
87+
got, err := expandLink(tt.long, expandEnv{Now: tt.now, Path: tt.remainder, User: tt.user})
8188
if err != nil {
8289
t.Fatalf("expandLink(%q): %v", tt.long, err)
8390
}

tmpl/help.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ <h2 id="advanced">Advanced destination links</h2>
4343
Templates are provided a data structure with the following fields:
4444

4545
<ul>
46-
<li><code>.Path</code> is the remaining path value after the short name (without a leading slash). For the link <strong>go/who/amelie</strong>,
47-
the value of <code>.Path</code> is <code>amelie</code>.
46+
<li><code>.Path</code> is the remaining path value after the short name (without a leading slash).
47+
For the link <strong>go/who/amelie</strong>, the value of <code>.Path</code> is <code>amelie</code>.
4848
<li><code>.Now</code> is a <a href="https://pkg.go.dev/time#Time">time.Time</a> value representing the current date and time.
49-
Use <a href="https://pkg.go.dev/time#Time.Format">Time.Format</a> to output date or time in desired format.
49+
<li><code>.User</code> is the current user resolving the link.
50+
This is the email address of the user or <code>{username}@github</code> for tailnets that use GitHub authentication.
5051
</ul>
5152

5253
Templates also have access to the following template functions:

0 commit comments

Comments
 (0)