Skip to content

Commit 50de11d

Browse files
committed
Fix bugs related to templates in Description
Challenges with {{url}} or {{lookup}} template tags in the Description field will no longer fail validation. These templates will also be properly rendered when using `cmgr playtest`.
1 parent 2eefb47 commit 50de11d

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

cmd/cmgr/playtest.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,21 @@ func launchPortal(mgr *cmgr.Manager, iface string, port int, cid cmgr.ChallengeI
9898

9999
w.Write([]byte(fmt.Sprintf(`<h1>%s</h1>`, cMeta.Name)))
100100

101-
w.Write([]byte(fmt.Sprintf(`<h2>Description</h2><p>%s</p>`, cMeta.Description)))
101+
artifactUrl := fmt.Sprintf("http://%s:%d/artifact/$1", iface, port)
102+
103+
description := cMeta.Description
104+
description = urlRe.ReplaceAllString(description, artifactUrl)
105+
for lookupRe.MatchString(description) {
106+
match := lookupRe.FindStringSubmatch(description)
107+
description = strings.ReplaceAll(
108+
description,
109+
match[0],
110+
fmt.Sprintf("%s", bMeta.LookupData[match[1]]))
111+
}
112+
113+
w.Write([]byte(fmt.Sprintf(`<h2>Description</h2><p>%s</p>`, description)))
102114

103115
details := cMeta.Details
104-
artifactUrl := fmt.Sprintf("http://%s:%d/artifact/$1", iface, port)
105116
details = urlRe.ReplaceAllString(details, artifactUrl)
106117
details = serverRe.ReplaceAllString(details, iface)
107118
details = httpBaseRe.ReplaceAllString(details, fmt.Sprintf("http://%s", iface))

cmgr/loader.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,13 @@ func (m *Manager) validateMetadata(md *ChallengeMetadata) error {
304304
return s, err
305305
}
306306

307-
// Validate (& lift) Details
308-
res, err := normalizeAndCheckTemplated(md.Details)
307+
res, err := normalizeAndCheckTemplated(md.Description)
308+
if err != nil {
309+
lastErr = err
310+
}
311+
md.Description = res
312+
313+
res, err = normalizeAndCheckTemplated(md.Details)
309314
if err != nil {
310315
lastErr = err
311316
}
@@ -447,8 +452,12 @@ func (m *Manager) validateBuild(cMeta *ChallengeMetadata, md *BuildMetadata, fil
447452
return err
448453
}
449454

450-
// Validate (& lift) Details
451-
err := checkTemplated(cMeta.Details)
455+
// Validate templated fields
456+
err := checkTemplated(cMeta.Description)
457+
detailsErr := checkTemplated(cMeta.Details)
458+
if detailsErr != nil {
459+
err = detailsErr
460+
}
452461

453462
for _, hint := range cMeta.Hints {
454463
tmpErr := checkTemplated(hint)

0 commit comments

Comments
 (0)