Skip to content

Commit

Permalink
feat(cd): improve continuous setup support
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Apr 15, 2024
1 parent 73c110d commit 29184b2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
13 changes: 13 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ctfdsetup

import "github.com/pkg/errors"

type ErrClient struct {
err error
}

var _ error = (*ErrClient)(nil)

func (err ErrClient) Error() string {
return errors.Wrap(err.err, "client error").Error()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ctfer-io/ctfd-setup
go 1.22.2

require (
github.com/ctfer-io/go-ctfd v0.6.0
github.com/ctfer-io/go-ctfd v0.6.2
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.27.1
go.uber.org/multierr v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/ctfer-io/go-ctfd v0.6.0 h1:6sV4pFthZeNoCKJfsPOJ5Hr1drDoiY7KVWILGjRpQAU=
github.com/ctfer-io/go-ctfd v0.6.0/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM=
github.com/ctfer-io/go-ctfd v0.6.2 h1:dRTWiyi9LSimOaUGuShqg3oYXN0Ur3h7bt2lHlB5zok=
github.com/ctfer-io/go-ctfd v0.6.2/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/schema v1.3.0 h1:rbciOzXAx3IB8stEFnfTwO3sYa6EWlQk79XdyustPDA=
Expand Down
51 changes: 45 additions & 6 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ctfdsetup
import (
"context"
"net/http"
"strconv"

"github.com/ctfer-io/go-ctfd/api"
"github.com/pkg/errors"
Expand Down Expand Up @@ -41,7 +42,7 @@ func bare(ctx context.Context, url string) (bool, error) {
}
res, err := client.Do(req)
if err != nil {
return false, err
return false, &ErrClient{err: err}
}
return res.StatusCode == 200, nil // 302 if already setup
}
Expand All @@ -63,7 +64,8 @@ func bareSetup(ctx context.Context, client *api.Client, conf *Config) error {
}

// Flatten configuration and setup it
return client.Setup(&api.SetupParams{
// TODO basic setup only, will be updated in the upcoming API calls
if err := client.Setup(&api.SetupParams{
CTFName: conf.Global.Name,
CTFDescription: conf.Global.Description,
UserMode: conf.Global.Mode,
Expand All @@ -83,12 +85,49 @@ func bareSetup(ctx context.Context, client *api.Client, conf *Config) error {
ThemeColor: conf.Front.ThemeColor,
Start: conf.Global.Start,
End: conf.Global.End,
}, api.WithContext(ctx))
}, api.WithContext(ctx)); err != nil {
return &ErrClient{err: err}
}
return nil
}

func updateSetup(_ context.Context, _ *api.Client, _ *Config) error {
func updateSetup(ctx context.Context, client *api.Client, conf *Config) error {
Log().Info("logging in")

if err := client.Login(&api.LoginParams{
Name: conf.Admin.Name,
Password: conf.Admin.Password,
}, api.WithContext(ctx)); err != nil {
return &ErrClient{err: err}
}

Log().Info("updating existing CTFd instance")

// TODO implement
return errors.New("not implemented yet")
if err := client.PatchConfigs(&api.PatchConfigsParams{
CTFName: &conf.Global.Name,
CTFDescription: &conf.Global.Description,
UserMode: &conf.Global.Mode,
ChallengeVisibility: &conf.Visibilities.Challenge,
AccountVisibility: &conf.Visibilities.Account,
ScoreVisibility: &conf.Visibilities.Score,
RegistrationVisibility: &conf.Visibilities.Registration,
VerifyEmails: &conf.Global.VerifyEmails,
TeamSize: itoa(conf.Global.TeamSize),
// Admin configuration won't be updated
// TODO add support of front group
// TODO add support of other settings
Start: &conf.Global.Start,
End: &conf.Global.End,
}, api.WithContext(ctx)); err != nil {
return &ErrClient{err: err}
}
return nil
}

func itoa(i *int) *string {
if i == nil {
return nil
}
s := strconv.Itoa(*i)
return &s
}

0 comments on commit 29184b2

Please sign in to comment.