mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 07:12:46 +01:00
feat: demo flags (#1084)
This commit is contained in:
22
app/app.go
22
app/app.go
@@ -1,10 +1,12 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/migrations"
|
||||
"github.com/SecurityBrewery/catalyst/reaction"
|
||||
@@ -26,6 +28,14 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) {
|
||||
|
||||
app.OnBeforeServe().Add(addRoutes())
|
||||
|
||||
app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
|
||||
if HasFlag(e.App, "demo") {
|
||||
bindDemoHooks(e.App)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
// Register additional commands
|
||||
app.RootCmd.AddCommand(fakeDataCmd(app))
|
||||
app.RootCmd.AddCommand(setFeatureFlagsCmd(app))
|
||||
@@ -41,6 +51,18 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) {
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func bindDemoHooks(app core.App) {
|
||||
app.OnRecordBeforeCreateRequest("files", "reactions").Add(func(e *core.RecordCreateEvent) error {
|
||||
return fmt.Errorf("cannot create %s in demo mode", e.Record.Collection().Name)
|
||||
})
|
||||
app.OnRecordBeforeUpdateRequest("files", "reactions").Add(func(e *core.RecordUpdateEvent) error {
|
||||
return fmt.Errorf("cannot update %s in demo mode", e.Record.Collection().Name)
|
||||
})
|
||||
app.OnRecordBeforeDeleteRequest("files", "reactions").Add(func(e *core.RecordDeleteEvent) error {
|
||||
return fmt.Errorf("cannot delete %s in demo mode", e.Record.Collection().Name)
|
||||
})
|
||||
}
|
||||
|
||||
func dev() bool {
|
||||
return strings.HasPrefix(os.Args[0], os.TempDir())
|
||||
}
|
||||
|
||||
18
app/flags.go
18
app/flags.go
@@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -10,6 +11,23 @@ import (
|
||||
"github.com/SecurityBrewery/catalyst/migrations"
|
||||
)
|
||||
|
||||
func HasFlag(app core.App, flag string) bool {
|
||||
records, err := app.Dao().FindRecordsByExpr(migrations.FeatureCollectionName, dbx.HashExp{"name": flag})
|
||||
if err != nil {
|
||||
app.Logger().Error(err.Error())
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
for _, r := range records {
|
||||
if r.GetString("name") == flag {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Flags(app core.App) ([]string, error) {
|
||||
records, err := app.Dao().FindRecordsByExpr(migrations.FeatureCollectionName)
|
||||
if err != nil {
|
||||
|
||||
@@ -10,6 +10,20 @@ import (
|
||||
catalystTesting "github.com/SecurityBrewery/catalyst/testing"
|
||||
)
|
||||
|
||||
func TestHasFlag(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
catalystApp, _, cleanup := catalystTesting.App(t)
|
||||
defer cleanup()
|
||||
|
||||
// stage 1
|
||||
assert.False(t, app.HasFlag(catalystApp, "test"))
|
||||
|
||||
// stage 2
|
||||
require.NoError(t, app.SetFlags(catalystApp, []string{"test"}))
|
||||
assert.True(t, app.HasFlag(catalystApp, "test"))
|
||||
}
|
||||
|
||||
func Test_flags(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user