- the generated CLI publishes no database or uploads bucket for plugin commands, so the command stopped with database is not configured - without a published database it opens database.dsn and publishes it, and without a published bucket it opens storage.uploads.bucket_url, closing what it opened
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package console
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/lagoon"
|
|
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
|
|
"gocloud.dev/blob"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// lookupDB returns the app's database. The generated CLI publishes none for
|
|
// plugin commands (only serve and the framework's own commands open one),
|
|
// so without a published database it opens database.dsn and publishes it,
|
|
// as lagoon's migrate does. done closes a pool this call opened and is a
|
|
// no-op otherwise.
|
|
func lookupDB(ctx context.Context, app *backpack.App) (*gorm.DB, func(), error) {
|
|
if app == nil {
|
|
return nil, nil, errors.New("journal: app is nil")
|
|
}
|
|
if db := mustDB(app); db != nil {
|
|
return db, func() {}, nil
|
|
}
|
|
if lagoon.DSN(app.Config) == "" {
|
|
return nil, nil, errors.New("journal: database is not configured (set SUMMER_DATABASE__DSN)")
|
|
}
|
|
sqlDB, gdb, err := lagoon.OpenFromApp(ctx, app)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := lagoon.Publish(app, sqlDB, gdb); err != nil {
|
|
_ = sqlDB.Close()
|
|
return nil, nil, err
|
|
}
|
|
return gdb, func() { _ = sqlDB.Close() }, nil
|
|
}
|
|
|
|
// lookupBucket returns the published uploads bucket, or opens
|
|
// storage.uploads.bucket_url when the process has none (the generated
|
|
// CLI). done closes a bucket this call opened and is a no-op otherwise.
|
|
func lookupBucket(ctx context.Context, app *backpack.App) (*blob.Bucket, func(), error) {
|
|
if app == nil {
|
|
return nil, nil, errors.New("journal: app is nil")
|
|
}
|
|
if bucket, ok := app.Lookup[*blob.Bucket](); ok && bucket != nil {
|
|
return bucket, func() {}, nil
|
|
}
|
|
bucket, err := attach.OpenBucket(ctx, app.Config)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return bucket, func() { _ = bucket.Close() }, nil
|
|
}
|