Creating Commands
This guide covers creating custom commands using the Nimbus CLI, inspired by Laravel Artisan-style command workflows. Commands use github.com/CodeSyncr/nimbus/command โ no direct Cobra dependency.
Creating your first command
Generate a new command using the make:command Ace command. This creates a command in the commands/ directory with the standard structure.
nimbus make:command greet
Use namespaces with colons for related commands:
nimbus make:command make:controller
The generated file uses command.New, command.Register, and command.Ctx.
package commands
import (
"fmt"
"github.com/CodeSyncr/nimbus/command"
)
func init() {
command.Register(GreetCommand())
}
func GreetCommand() *command.Command {
return command.New("greet", "Description of the command").
Long("Longer help text for --help").
RunE(func(ctx *command.Ctx) error {
fmt.Println("Hello from greet")
return nil
})
}
Execute the command after wiring main.go:
go run . greet
Configuring command metadata
Command metadata controls how your command appears in help screens and how it behaves.
Command name and description
command.New(use, short) sets the name and short description. Use colons for namespaces.
return command.New("greet", "Greet a user by name").
Long("The greet command prints a greeting. Use --loud for emphasis.")
Aliases
Provide alternative names with Aliases:
return command.New("greet", "Greet a user").
Aliases("welcome", "sayhi")
Users can run go run . welcome or go run . sayhi.
Registering commands
Commands register themselves via command.Register in init(). Wire the CLI in main.go with command.Run:
package main
import (
"fmt"
"os"
_ "myapp/commands" // load commands (register via init)
"myapp/bin"
"github.com/CodeSyncr/nimbus/command"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "migrate" {
bin.RunMigrations()
return
}
if len(os.Args) > 1 && os.Args[1] == "queue:work" {
bin.RunQueueWorker()
return
}
if len(os.Args) > 1 {
command.RunOrExit(os.Args[1:])
return
}
app := bin.Boot()
_ = app.Run()
}
The blank import _ "myapp/commands" loads the package and runs init(), which registers each command. Use command.RunOrExit to run and exit on error, or command.Run to handle errors yourself.
Built-in commands
The Nimbus CLI provides these generators (run with nimbus from app root):
nimbus new [app-name]โ scaffold a new Nimbus applicationnimbus serveโ start the app with hot reloadnimbus make:command [name]โ create a custom commandnimbus make:model [name]โ generate a modelnimbus make:controller [name]โ generate a controllernimbus make:middleware [name]โ generate middlewarenimbus make:migration [name]โ generate a migrationnimbus make:seeder [name]โ generate a seedernimbus make:job [name]โ generate a queue job