-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages.go
66 lines (60 loc) · 1.45 KB
/
pages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 {
var ctfdP *api.Page
for _, p := range ctfdPages {
if p.Route == page.Route {
ctfdP = p
break
}
}
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,
Content: string(page.Content.Content),
Draft: page.Draft,
Hidden: page.Hidden,
AuthRequired: page.AuthRequired,
}, api.WithContext(ctx)); err != nil {
return err
}
} else {
// CREATE
if _, err := client.PostPages(&api.PostPagesParams{
Title: page.Title,
Route: page.Route,
Format: page.Format,
Content: string(page.Content.Content),
Draft: page.Draft,
Hidden: page.Hidden,
AuthRequired: page.AuthRequired,
}, api.WithContext(ctx)); err != nil {
return err
}
}
}
// 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
}