diff --git a/cmd/bundb/main.go b/cmd/bundb/main.go index 0e424d42..227c3f37 100644 --- a/cmd/bundb/main.go +++ b/cmd/bundb/main.go @@ -33,8 +33,7 @@ import ( func main() { log.SetPrefix("bundb: ") - // TODO: use buncli.New(buncli.FromPlugin()) to read config from plugin - if err := buncli.Run(os.Args, nil); err != nil { + if err := buncli.NewStandalone("bundb").Run(os.Args); err != nil { log.Fatal(err) } } diff --git a/extra/buncli/buncli.go b/extra/buncli/buncli.go index bd67ee61..6d148516 100644 --- a/extra/buncli/buncli.go +++ b/extra/buncli/buncli.go @@ -24,8 +24,10 @@ var bunApp = &cli.App{ // New creates a new CLI application for managing bun migrations. func New(c *Config) *App { + if c.RootName != "" { + bunApp.Name = c.RootName + } bunApp.Commands = cli.Commands{ - CmdInit(), CmdMigrate(c), CmdRollback(c), CmdCreate(c), @@ -37,7 +39,23 @@ func New(c *Config) *App { } } +// NewStandalone create a new CLI application to be distributed as a standalone binary. +// It's intended to be used in the cmb/bund and does not require any prior setup from the user: +// the app only includes the Init command and reads all its configuration from command line. +// +// Prefer using New(*Config) in your custom entrypoint. +func NewStandalone(name string) *App { + bunApp.Name = name + bunApp.Commands = cli.Commands{ + CmdInit(), + } + return &App{ + App: bunApp, + } +} + type Config struct { + RootName string DB *bun.DB AutoMigrator *migrate.AutoMigrator Migrations *migrate.Migrations diff --git a/extra/buncli/go.mod b/extra/buncli/go.mod index 0cc2ef1a..900ae1e8 100644 --- a/extra/buncli/go.mod +++ b/extra/buncli/go.mod @@ -12,6 +12,7 @@ require ( github.com/uptrace/bun/driver/pgdriver v1.2.7-0.20241126124946-928d0779110e github.com/uptrace/bun/driver/sqliteshim v1.2.7-0.20241126124946-928d0779110e github.com/urfave/cli/v2 v2.27.5 + golang.org/x/mod v0.22.0 ) require ( @@ -32,7 +33,6 @@ require ( github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect golang.org/x/crypto v0.29.0 // indirect golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect - golang.org/x/mod v0.22.0 // indirect golang.org/x/sys v0.27.0 // indirect mellium.im/sasl v0.3.2 // indirect modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852 // indirect diff --git a/extra/buncli/init.go b/extra/buncli/init.go index 07964763..d3c36bfc 100644 --- a/extra/buncli/init.go +++ b/extra/buncli/init.go @@ -127,6 +127,7 @@ func main() { var _ /* auto */ migrate.AutoMigrator if err := buncli.Run(os.Args, &buncli.Config{ + RootName: %q, // DB: db, // AutoMigrator: auto, Migrations: migrations.Migrations, @@ -160,7 +161,7 @@ func initCmd(binDir string, migrationsDir string) error { pkgMigrations := path.Join(modPath, strings.TrimLeft(migrationsDir, ".")) log.Print("pkgMigrations: ", pkgMigrations) - if _, err := fmt.Fprintf(f, entrypointTemplate, pkgMigrations); err != nil { + if _, err := fmt.Fprintf(f, entrypointTemplate, pkgMigrations, defaultBin); err != nil { log.Print("here!") return err } @@ -259,6 +260,7 @@ func newDB(ctx *cli.Context) (*bun.DB, error) { return bun.NewDB(sqlDB, dialect), nil } +// getModPath parses the ./go.mod file in the current directory and returns the declared module path. func getModPath() (string, error) { f, err := os.ReadFile("go.mod") if err != nil {