Skip to content

Commit fb932d1

Browse files
Extract RenderDownloadURL
1 parent 9c36de7 commit fb932d1

File tree

3 files changed

+93
-22
lines changed

3 files changed

+93
-22
lines changed

cmd/cli/main.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"net/url"
1313
"os"
1414
"strconv"
15-
"strings"
16-
texttemplate "text/template"
1715
"time"
1816

1917
"github.com/getsentry/sentry-go"
@@ -32,7 +30,6 @@ import (
3230
"github.com/authgear/authgear-once-license-server/pkg/slogging"
3331
"github.com/authgear/authgear-once-license-server/pkg/smtp"
3432
pkgstripe "github.com/authgear/authgear-once-license-server/pkg/stripe"
35-
"github.com/authgear/authgear-once-license-server/pkg/uname"
3633
)
3734

3835
const indexHTML = `<!DOCTYPE html>
@@ -136,23 +133,18 @@ var serveCmd = &cobra.Command{
136133
w.Header().Set("Cache-Control", "no-store")
137134
w.Write([]byte(script))
138135
default:
139-
// Otherwise, we return 303 to download the executable.
140-
uname_s = uname.NormalizeUnameS(uname_s)
141-
uname_m = uname.NormalizeUnameM(uname_m)
142-
data := map[string]any{
143-
"Uname_s": uname_s,
144-
"Uname_m": uname_m,
145-
}
146-
var buf strings.Builder
147-
err = deps.AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE.Execute(&buf, data)
136+
downloadURL, err := installationscript.RenderDownloadURL(deps.AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE, installationscript.RenderDownloadURLOptions{
137+
Uname_s: uname_s,
138+
Uname_m: uname_m,
139+
})
148140
if err != nil {
149141
slogging.Error(ctx, logger, "failed to render download url",
150142
"error", err)
151143
http.Error(w, "failed to render download url", http.StatusInternalServerError)
152144
return
153145
}
154146

155-
http.Redirect(w, r, buf.String(), http.StatusSeeOther)
147+
http.Redirect(w, r, downloadURL, http.StatusSeeOther)
156148
}
157149
})
158150

@@ -398,7 +390,7 @@ type Dependencies struct {
398390
StripeCheckoutSessionPriceID string
399391
StripeWebhookSigningSecret string
400392
AUTHGEAR_ONCE_PUBLIC_URL_SCHEME string
401-
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE *texttemplate.Template
393+
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE string
402394
AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE string
403395
KeygenConfig keygen.KeygenConfig
404396
}
@@ -463,12 +455,6 @@ func main() {
463455
SMTPPassword: os.Getenv("AUTHGEAR_ONCE_SMTP_PASSWORD"),
464456
})
465457

466-
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE, err := texttemplate.New("").Parse(os.Getenv("AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE"))
467-
if err != nil {
468-
panic(err)
469-
}
470-
AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE := os.Getenv("AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE")
471-
472458
dependencies := Dependencies{
473459
HTTPClient: &http.Client{},
474460
StripeClient: stripeClient,
@@ -479,8 +465,8 @@ func main() {
479465
StripeCheckoutSessionPriceID: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_PRICE_ID"),
480466
StripeWebhookSigningSecret: os.Getenv("AUTHGEAR_ONCE_STRIPE_WEBHOOK_SIGNING_SECRET"),
481467
AUTHGEAR_ONCE_PUBLIC_URL_SCHEME: os.Getenv("AUTHGEAR_ONCE_PUBLIC_URL_SCHEME"),
482-
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE: AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE,
483-
AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE: AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE,
468+
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE: os.Getenv("AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE"),
469+
AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE: os.Getenv("AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE"),
484470
KeygenConfig: keygen.KeygenConfig{
485471
Endpoint: os.Getenv("AUTHGEAR_ONCE_KEYGEN_ENDPOINT"),
486472
AdminToken: os.Getenv("AUTHGEAR_ONCE_KEYGEN_ADMIN_TOKEN"),

pkg/installationscript/installationscript.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package installationscript
33
import (
44
"bytes"
55
texttemplate "text/template"
6+
7+
"github.com/authgear/authgear-once-license-server/pkg/uname"
68
)
79

810
var tmpl *texttemplate.Template
@@ -54,3 +56,27 @@ func Render(opts RenderOptions) (out string, err error) {
5456
out = buf.String()
5557
return
5658
}
59+
60+
type RenderDownloadURLOptions struct {
61+
Uname_s string
62+
Uname_m string
63+
}
64+
65+
func RenderDownloadURL(downloadURLGoTemplate string, opts RenderDownloadURLOptions) (out string, err error) {
66+
t, err := texttemplate.New("").Parse(downloadURLGoTemplate)
67+
if err != nil {
68+
return
69+
}
70+
71+
opts.Uname_s = uname.NormalizeUnameS(opts.Uname_s)
72+
opts.Uname_m = uname.NormalizeUnameM(opts.Uname_m)
73+
74+
var buf bytes.Buffer
75+
err = t.Execute(&buf, opts)
76+
if err != nil {
77+
return
78+
}
79+
80+
out = buf.String()
81+
return
82+
}

pkg/installationscript/installationscript_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,62 @@ fi
7878
})
7979
}
8080
}
81+
82+
func TestRenderDownloadURL(t *testing.T) {
83+
tests := []struct {
84+
name string
85+
downloadURLTemplate string
86+
opts RenderDownloadURLOptions
87+
expected string
88+
}{
89+
{
90+
name: "Darwin ARM64",
91+
downloadURLTemplate: "https://example.com/download/authgear-once-{{.Uname_s}}-{{.Uname_m}}",
92+
opts: RenderDownloadURLOptions{
93+
Uname_s: "Darwin",
94+
Uname_m: "arm64",
95+
},
96+
expected: "https://example.com/download/authgear-once-darwin-arm64",
97+
},
98+
{
99+
name: "Linux AMD64",
100+
downloadURLTemplate: "https://example.com/download/authgear-once-{{.Uname_s}}-{{.Uname_m}}",
101+
opts: RenderDownloadURLOptions{
102+
Uname_s: "Linux",
103+
Uname_m: "x86_64",
104+
},
105+
expected: "https://example.com/download/authgear-once-linux-amd64",
106+
},
107+
{
108+
name: "Whitespace and casing normalization",
109+
downloadURLTemplate: "https://example.com/download/authgear-once-{{.Uname_s}}-{{.Uname_m}}",
110+
opts: RenderDownloadURLOptions{
111+
Uname_s: " DARWIN \n",
112+
Uname_m: " AArch64 ",
113+
},
114+
expected: "https://example.com/download/authgear-once-darwin-arm64",
115+
},
116+
{
117+
name: "Template with additional variables",
118+
downloadURLTemplate: "https://example.com/download/authgear-once-{{.Uname_s}}-{{.Uname_m}}?version=1.0.0",
119+
opts: RenderDownloadURLOptions{
120+
Uname_s: "Linux",
121+
Uname_m: "ARM",
122+
},
123+
expected: "https://example.com/download/authgear-once-linux-arm64?version=1.0.0",
124+
},
125+
}
126+
127+
for _, tt := range tests {
128+
t.Run(tt.name, func(t *testing.T) {
129+
result, err := RenderDownloadURL(tt.downloadURLTemplate, tt.opts)
130+
if err != nil {
131+
t.Fatalf("RenderDownloadURL() error = %v", err)
132+
}
133+
134+
if result != tt.expected {
135+
t.Errorf("RenderDownloadURL() output mismatch\nGot: %q\nWant: %q", result, tt.expected)
136+
}
137+
})
138+
}
139+
}

0 commit comments

Comments
 (0)