mainfx

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2025 License: MIT Imports: 14 Imported by: 0

README

Mainfx

This package provides a bootstrap for starting applications using Uber's FX library. It allows registering modules as basic subcommands.

Installation

Import the package into your Go project:

import "path/to/mainfx"

Usage

Register Subcommands

To register a subcommand, use imports with side effects :

cmd/server/main.go
func init() {
    mainfx.RegisterSubcommand("server", "Start the server",
        fx.Provide(NewServer),
        fx.Invoke(StartServer),
    )
}
main.go
import _ "cmd/server"
Running the Program

Call the Main function in your main package:

func main() {
    mainfx.Main()
}

mainfx Module

Some extra types are provided by mainfx.

*slog.Logger

The default slog.Logger when Main is called or a debug logger if debugging is enabled.

*OsArgs

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
}

Help Output

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"
Debug Logging

Enable debug logging by building the program with the -tags debug flag.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main()

Main runs the program. fx output is only printed if `-tags debug` is specified during build.

func RegisterSubcommand

func RegisterSubcommand(name, description string, options ...fx.Option)

RegisterSubcommand registers a subcommand. The name must not contain any whitespace. The description is printed if --help or -h is specified before the command. The name and options are combines to create a module to run.

The following extra values will be provided by fx:

  • *slog.Logger: The default slog logger when running Main
  • *OsArgs: The command line arguments when running the program
  • context.Context: A context that is cancelled when Main returns or when fx shuts down.

To register a subcommand create an init function in a subcommand package:

func init() {
    program.RegisterSubcommand("server", "start the server", <...server module options>)
}

Then import with side effects:

import _ "cmd/server"

Types

type OsArgs

type OsArgs struct {
	ProgramName string   // Provided by os.Executable
	ProgramArgs []string // os.Args[1:]
	Command     string   // os.Args[1]
	CommandArgs []string // os.Args[2:]
	AllArgs     []string // os.Args
}

OsArgs are the args passed to the program.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL