@@ -2,23 +2,32 @@ package ctfdsetup
22
33import (
44 "context"
5+ "slices"
56 "strconv"
67
78 "github.com/ctfer-io/go-ctfd/api"
89)
910
1011func 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