Files
summercms/cmd/summer/main.go
Jakub Zych 168d65cd1b feat(conga,dev): worker and scheduler reports, summer dev runs serve
- queue:work and schedule:run print a startup summary and a shutdown
  report on a terminal, log them as records otherwise, and return nil
  after a clean stop
- schedule:run --once ends with 'Ran N due entries (M failed)'
- summer dev runs bin/<app> serve with the forwarded global options,
  reports through Output, stops the old server before starting the new
  one and prints a dev report on Ctrl+C (exit 0)
- lagoon.DatabaseLabel describes a DSN without its secrets
- Long help and examples for queue:work, schedule:run, dev, docs:serve
2026-10-21 13:04:27 +02:00

353 lines
9.6 KiB
Go

package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"git.golem15.com/golem15/summercms/internal/build"
"git.golem15.com/golem15/summercms/internal/dev"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/diary"
"git.golem15.com/golem15/summercms/modules/lagoon"
)
func main() {
args := os.Args[1:]
g := bonfire.PeekGlobals(args)
diary.Bootstrap(os.Stderr, diary.FromGlobals(g))
root, err := bonfire.NewRoot("summer", toolCommands(), os.Stdout,
bonfire.WithVersion(bonfire.BuildVersion()), bonfire.OnGlobals(diary.ApplyGlobals))
if err != nil {
os.Exit(bonfire.Fail(os.Stderr, "summer", err, g))
}
os.Exit(bonfire.Execute(context.Background(), root, args))
}
func toolCommands() []bonfire.Command {
return append([]bonfire.Command{
buildCommand(),
makePluginCommand(),
makeModelCommand(),
makeMigrationCommand(),
makeCommandCommand(),
makeJobCommand(),
makeAdminControllerCommand(),
addPluginCommand(),
newCommand(),
devCommand(),
parityProxyCommand(),
parityRecordCommand(),
parityReplayCommand(),
parityBroadcastsCommand(),
parityUpstreamCommand(),
docsBuildCommand(),
docsSyncCommand(),
docsServeCommand(),
}, delegateCommands()...)
}
func buildCommand() bonfire.Command {
return bonfire.Command{
Name: "build",
Description: "Generate plugin imports and build the app binary",
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
return build.App(ctx, dir, out)
},
}
}
func makePluginCommand() bonfire.Command {
return bonfire.Command{
Name: "make:plugin",
Description: "Scaffold a compiling plugin module from a vendor.plugin id",
Args: []bonfire.Arg{{
Name: "id",
Description: "Plugin ID in vendor.plugin form",
Required: true,
}},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
id, ok := in.Argument("id")
if !ok || id == "" {
return fmt.Errorf("make:plugin requires a vendor.plugin id")
}
dir, err := os.Getwd()
if err != nil {
return err
}
created, err := build.MakePlugin(ctx, dir, id)
if err != nil {
return err
}
out.Printf("created %s\n", created)
return nil
},
}
}
func makeModelCommand() bonfire.Command {
return bonfire.Command{
Name: "make:model",
Description: "Generate a GORM model and create-table migration",
Args: makeArtifactArgs(),
Flags: []bonfire.Flag{{
Name: "no-migration",
Description: "Omit the create-table migration",
Bare: true,
}},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
id, name, err := resolveMakeArgs(in, "make:model")
if err != nil {
return err
}
result, err := build.MakeModel(ctx, dir, id, name, flagTrue(in, "no-migration"))
if err != nil {
return err
}
return printArtifact(out, result)
},
}
}
func makeMigrationCommand() bonfire.Command {
return bonfire.Command{
Name: "make:migration",
Description: "Generate an ordered gormigrate stub",
Args: makeArtifactArgs(),
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
id, name, err := resolveMakeArgs(in, "make:migration")
if err != nil {
return err
}
result, err := build.MakeMigration(ctx, dir, id, name)
if err != nil {
return err
}
return printArtifact(out, result)
},
}
}
func makeCommandCommand() bonfire.Command {
return bonfire.Command{
Name: "make:command",
Description: "Generate a plugin console command",
Args: makeArtifactArgs(),
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
id, name, err := resolveMakeArgs(in, "make:command")
if err != nil {
return err
}
result, err := build.MakeCommand(ctx, dir, id, name)
if err != nil {
return err
}
return printArtifact(out, result)
},
}
}
func makeJobCommand() bonfire.Command {
return bonfire.Command{
Name: "make:job",
Description: "Generate a plugin job without importing River",
Args: makeArtifactArgs(),
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
id, name, err := resolveMakeArgs(in, "make:job")
if err != nil {
return err
}
result, err := build.MakeJob(ctx, dir, id, name)
if err != nil {
return err
}
return printArtifact(out, result)
},
}
}
func makeAdminControllerCommand() bonfire.Command {
return bonfire.Command{
Name: "make:admin-controller",
Description: "Generate an admin controller with fields.yaml and columns.yaml",
Args: makeArtifactArgs(),
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
id, name, err := resolveMakeArgs(in, "make:admin-controller")
if err != nil {
return err
}
result, err := build.MakeAdminController(ctx, dir, id, name)
if err != nil {
return err
}
return printArtifact(out, result)
},
}
}
func makeArtifactArgs() []bonfire.Arg {
return []bonfire.Arg{
{Name: "plugin", Description: "Plugin ID in vendor.plugin form, or the artifact name when already inside the plugin"},
{Name: "name", Description: "Exported Go identifier"},
}
}
func resolveMakeArgs(in bonfire.Input, cmd string) (id, name string, err error) {
args := in.Args()
switch len(args) {
case 2:
return args[0], args[1], nil
case 1:
return "", args[0], nil
default:
return "", "", fmt.Errorf("%s requires <vendor.plugin> <Name>, or <Name> inside a plugin directory", cmd)
}
}
func flagTrue(in bonfire.Input, name string) bool {
v, ok := in.Flag(name)
if !ok {
return false
}
switch strings.ToLower(strings.TrimSpace(v)) {
case "", "true", "1", "yes":
return true
default:
return false
}
}
func printArtifact(out bonfire.Output, result build.ArtifactResult) error {
for _, path := range result.Files {
out.Printf("created %s\n", path)
}
if result.Hint != "" {
out.Printf("%s\n", result.Hint)
}
return nil
}
func devCommand() bonfire.Command {
return bonfire.Command{
Name: "dev",
Description: "Watch sources and rebuild the app binary",
Long: `Builds the application in the current directory, runs bin/<binary> serve and
watches the application's Go, YAML, go.mod, go.work, summer.yaml and .env
files (bin/, tmp/, .git/, main.go and plugins.gen.go excluded). After a change
it rebuilds and restarts serve; a failed build keeps the running server.
The global options summer dev was given (-v, -q, --ansi, --no-ansi, -n,
--env) are passed on to serve. Ctrl+C stops serve, which prints its own
shutdown report, then prints the dev report (rebuilds, failed builds,
restarts, uptime) and exits 0.`,
Examples: []bonfire.Example{
{Command: "dev", Description: "Build, serve and rebuild on every change"},
{Command: "dev -v", Description: "Pass -v on to serve: per-plugin summary lines and info logs of every component"},
},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
dir, err := os.Getwd()
if err != nil {
return err
}
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
return dev.Watch(ctx, dir, out, bonfire.GlobalsOf(out).Args())
},
}
}
func addPluginCommand() bonfire.Command {
return bonfire.Command{
Name: "plugin:add",
Description: "Register a local plugin module in the app manifest and workspace",
Args: []bonfire.Arg{{
Name: "dir",
Description: "Local plugin directory",
Required: true,
}},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
pluginDir, ok := in.Argument("dir")
if !ok || pluginDir == "" {
return fmt.Errorf("plugin:add requires a local plugin directory")
}
dir, err := os.Getwd()
if err != nil {
return err
}
if err := build.AddPlugin(ctx, dir, pluginDir); err != nil {
return err
}
out.Printf("added %s\n", pluginDir)
return nil
},
}
}
func newCommand() bonfire.Command {
return bonfire.Command{
Name: "new",
Description: "Create a SummerCMS application in sm-<name>-app",
Args: []bonfire.Arg{{
Name: "name",
Description: "Application name: lower-case letters and digits, starting with a letter; not a Go keyword, main or internal",
Required: true,
}},
Flags: []bonfire.Flag{
{Name: "framework", Description: "Path to the framework checkout (default: a sibling summercms or summercms.go checkout)"},
{Name: "no-plugin", Description: "Do not create the starter plugin", Bare: true},
{Name: "no-git", Description: "Do not initialise a git repository", Bare: true},
},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
name, ok := in.Argument("name")
if !ok || name == "" {
return fmt.Errorf("new requires an application name")
}
dir, err := os.Getwd()
if err != nil {
return err
}
framework, _ := in.Flag("framework")
key, err := lagoon.GenerateAppKey()
if err != nil {
return fmt.Errorf("new: generate application key: %w", err)
}
_, err = build.NewApp(ctx, build.NewOptions{
Name: name,
Parent: dir,
Framework: framework,
NoPlugin: flagTrue(in, "no-plugin"),
AppKey: key,
NoGit: flagTrue(in, "no-git"),
}, out)
return err
},
}
}