Skip to content

Commit 8f23fd1

Browse files
committed
fix: cover whole _pages_ lifecycle (+delete)
1 parent bfdee4e commit 8f23fd1

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

examples/nobrackets2024/.ctfd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ admin:
3333

3434
pages:
3535
additional:
36-
- title: CTFer.io example index
36+
- title: NoBracketsCTF 2024 - Finale
3737
route: index
3838
format: markdown
3939
content:
40-
from_file: examples/nobrackets2024/index.html
40+
from_file: index.html

pages.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@ package ctfdsetup
22

33
import (
44
"context"
5+
"slices"
56
"strconv"
67

78
"github.com/ctfer-io/go-ctfd/api"
89
)
910

1011
func additionalPages(ctx context.Context, client *api.Client, pages []Page) error {
12+
ctfdPages, err := client.GetPages(&api.GetPagesParams{}, api.WithContext(ctx))
13+
if err != nil {
14+
return err
15+
}
16+
cu := []string{}
17+
1118
for _, page := range pages {
12-
ctfdP, err := client.GetPages(&api.GetPagesParams{
13-
Route: ptr(page.Route),
14-
}, api.WithContext(ctx))
15-
if err != nil {
16-
return err
19+
var ctfdP *api.Page
20+
for _, p := range ctfdPages {
21+
if p.Route == page.Route {
22+
ctfdP = p
23+
break
24+
}
1725
}
1826

19-
exist := len(ctfdP) == 1
20-
if exist {
21-
if _, err := client.PatchPage(strconv.Itoa(ctfdP[0].ID), &api.PatchPageParams{
27+
cu = append(cu, page.Route)
28+
if ctfdP != nil {
29+
// UPDATE
30+
if _, err := client.PatchPage(strconv.Itoa(ctfdP.ID), &api.PatchPageParams{
2231
Title: page.Title,
2332
Route: page.Route,
2433
Format: page.Format,
@@ -30,6 +39,7 @@ func additionalPages(ctx context.Context, client *api.Client, pages []Page) erro
3039
return err
3140
}
3241
} else {
42+
// CREATE
3343
if _, err := client.PostPages(&api.PostPagesParams{
3444
Title: page.Title,
3545
Route: page.Route,
@@ -43,5 +53,14 @@ func additionalPages(ctx context.Context, client *api.Client, pages []Page) erro
4353
}
4454
}
4555
}
56+
57+
// DELETE
58+
for _, ctfdP := range ctfdPages {
59+
if !slices.Contains(cu, ctfdP.Route) {
60+
if err := client.DeletePage(strconv.Itoa(ctfdP.ID)); err != nil {
61+
return err
62+
}
63+
}
64+
}
4665
return nil
4766
}

0 commit comments

Comments
 (0)