This package provides a bootstrap for starting applications using Uber's FX library. It allows registering modules as basic subcommands.
Import the package into your Go project:
import "path/to/mainfx"
To register a subcommand, use imports with side effects :
func init() {
mainfx.RegisterSubcommand("server", "Start the server",
fx.Provide(NewServer),
fx.Invoke(StartServer),
)
}
import _ "cmd/server"
Call the Main
function in your main
package:
func main() {
mainfx.Main()
}
Some extra types are provided by mainfx.
The default slog.Logger when Main is called or a debug logger if debugging is enabled.
OsArgs
provides access to command-line arguments:
type OsArgs struct {
ProgramName string // The executable name
ProgramArgs []string // Arguments passed to the program
Command string // The first argument (subcommand)
CommandArgs []string // Remaining arguments for the subcommand
AllArgs []string // All arguments
}
When no subcommand is provided or the -h
or --help
flag is used, the program prints a list of available subcommands:
Available subcommands:
server: "Start the server"
Enable debug logging by building the program with the -tags debug
flag.