Skip to content

Commit

Permalink
fix: cover whole _pages_ lifecycle (+delete)
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Dec 8, 2024
1 parent bfdee4e commit 8f23fd1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/nobrackets2024/.ctfd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ admin:

pages:
additional:
- title: CTFer.io example index
- title: NoBracketsCTF 2024 - Finale
route: index
format: markdown
content:
from_file: examples/nobrackets2024/index.html
from_file: index.html
35 changes: 27 additions & 8 deletions pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ package ctfdsetup

import (
"context"
"slices"
"strconv"

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

func additionalPages(ctx context.Context, client *api.Client, pages []Page) error {
ctfdPages, err := client.GetPages(&api.GetPagesParams{}, api.WithContext(ctx))
if err != nil {
return err
}
cu := []string{}

for _, page := range pages {
ctfdP, err := client.GetPages(&api.GetPagesParams{
Route: ptr(page.Route),
}, api.WithContext(ctx))
if err != nil {
return err
var ctfdP *api.Page
for _, p := range ctfdPages {
if p.Route == page.Route {
ctfdP = p
break
}
}

exist := len(ctfdP) == 1
if exist {
if _, err := client.PatchPage(strconv.Itoa(ctfdP[0].ID), &api.PatchPageParams{
cu = append(cu, page.Route)
if ctfdP != nil {
// UPDATE
if _, err := client.PatchPage(strconv.Itoa(ctfdP.ID), &api.PatchPageParams{
Title: page.Title,
Route: page.Route,
Format: page.Format,
Expand All @@ -30,6 +39,7 @@ func additionalPages(ctx context.Context, client *api.Client, pages []Page) erro
return err
}
} else {
// CREATE
if _, err := client.PostPages(&api.PostPagesParams{
Title: page.Title,
Route: page.Route,
Expand All @@ -43,5 +53,14 @@ func additionalPages(ctx context.Context, client *api.Client, pages []Page) erro
}
}
}

// DELETE
for _, ctfdP := range ctfdPages {
if !slices.Contains(cu, ctfdP.Route) {
if err := client.DeletePage(strconv.Itoa(ctfdP.ID)); err != nil {
return err
}
}
}
return nil
}

0 comments on commit 8f23fd1

Please sign in to comment.