Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: subcommands #1111

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ dev:
@echo "Running..."
rm -rf catalyst_data
go run . admin create [email protected] 1234567890
go run . set-feature-flags dev
go run . fake-data
go run . serve
go run . serve --app-url http://localhost:8090 --flags dev

.PHONY: dev-10000
dev-10000:
@echo "Running..."
rm -rf catalyst_data
go run . admin create [email protected] 1234567890
go run . set-feature-flags dev
go run . fake-data --users 100 --tickets 10000
go run . serve
go run . serve --app-url http://localhost:8090 --flags dev

.PHONY: serve-ui
serve-ui:
Expand Down
44 changes: 30 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,47 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) {
DefaultDataDir: dir,
})

var appURL string

app.RootCmd.PersistentFlags().StringVar(&appURL, "app-url", "", "the app's URL")

var flags []string

app.RootCmd.PersistentFlags().StringSliceVar(&flags, "flags", nil, "feature flags")

_ = app.RootCmd.ParseFlags(os.Args[1:])

app.RootCmd.AddCommand(fakeDataCmd(app))

webhook.BindHooks(app)
reaction.BindHooks(app, test)

app.OnBeforeServe().Add(addRoutes())

app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
if err := MigrateDBs(e.App); err != nil {
return err
}

if err := SetFlags(e.App, flags); err != nil {
return err
}

if HasFlag(e.App, "demo") {
bindDemoHooks(e.App)
}

return nil
})
if appURL != "" {
s := e.App.Settings()
s.Meta.AppUrl = appURL

// Register additional commands
app.RootCmd.AddCommand(fakeDataCmd(app))
app.RootCmd.AddCommand(setFeatureFlagsCmd(app))
app.RootCmd.AddCommand(setAppURL(app))
if err := e.App.Dao().SaveSettings(s); err != nil {
return err
}
}

if err := app.Bootstrap(); err != nil {
return nil, err
}
return e.App.RefreshSettings()
})

if err := MigrateDBs(app); err != nil {
return nil, err
}
app.OnBeforeServe().Add(addRoutes())

return app, nil
}
Expand Down
6 changes: 2 additions & 4 deletions app/fakedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ func fakeDataCmd(app core.App) *cobra.Command {

cmd := &cobra.Command{
Use: "fake-data",
Run: func(_ *cobra.Command, _ []string) {
if err := fakedata.Generate(app, userCount, ticketCount); err != nil {
app.Logger().Error(err.Error())
}
RunE: func(_ *cobra.Command, _ []string) error {
return fakedata.Generate(app, userCount, ticketCount)
},
}

Expand Down
33 changes: 0 additions & 33 deletions app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/spf13/cobra"

"github.com/SecurityBrewery/catalyst/migrations"
)
Expand Down Expand Up @@ -85,35 +84,3 @@ func SetFlags(app core.App, args []string) error {

return nil
}

func setFeatureFlagsCmd(app core.App) *cobra.Command {
return &cobra.Command{
Use: "set-feature-flags",
Run: func(_ *cobra.Command, args []string) {
if err := SetFlags(app, args); err != nil {
app.Logger().Error(err.Error())
}
},
}
}

func setAppURL(app core.App) *cobra.Command {
return &cobra.Command{
Use: "set-app-url",
Run: func(_ *cobra.Command, args []string) {
if len(args) != 1 {
app.Logger().Error("missing app url")

return
}

settings := app.Settings()

settings.Meta.AppUrl = args[0]

if err := app.Dao().SaveSettings(settings); err != nil {
app.Logger().Error(err.Error())
}
},
}
}
4 changes: 4 additions & 0 deletions testing/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func App(t *testing.T) (*pocketbase.PocketBase, *Counter, func()) {
t.Fatal(err)
}

if err := baseApp.Bootstrap(); err != nil {
t.Fatal(fmt.Errorf("failed to bootstrap: %w", err))
}

baseApp.Settings().Logs.MaxDays = 0

defaultTestData(t, baseApp)
Expand Down
Loading