diff --git a/examples/nobrackets2024/.ctfd.yaml b/examples/nobrackets2024/.ctfd.yaml index 0d4936b..927dec5 100644 --- a/examples/nobrackets2024/.ctfd.yaml +++ b/examples/nobrackets2024/.ctfd.yaml @@ -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 diff --git a/pages.go b/pages.go index 3f0c2bb..ced819b 100644 --- a/pages.go +++ b/pages.go @@ -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, @@ -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, @@ -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 }