Skip to content

Commit

Permalink
refactor: expose different commands from standalone / embeded CLI
Browse files Browse the repository at this point in the history
The bootstrapped CLI which will be compiled along with the rest
of the user migration files does not need the Init command.

On the other hand, the standalone bundb command will provide that
and exclude all other commands to avoid confusion between them.
  • Loading branch information
bevzzz committed Nov 27, 2024
1 parent 7852c10 commit ece68d4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 1 addition & 2 deletions cmd/bundb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
20 changes: 19 additions & 1 deletion extra/buncli/buncli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion extra/buncli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion extra/buncli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ece68d4

Please sign in to comment.