Documentation
¶
Index ¶
- Variables
- func AutoRegister(f any) any
- func Commander(lc fx.Lifecycle, shutdowner fx.Shutdowner, cmd *cobra.Command)
- func ConfigCommand[T any](log *slog.Logger, configProvider configfx.Provider[T]) *cobra.Command
- func ConfigFile[T any](configName string) func(log *slog.Logger) configfx.Provider[T]
- func ContainerEntrypoint(tools ...string) func()
- func Shutdown(ctx context.Context, exitCode int) error
- func Unprivileged() error
- func UnprivilegedWarn(log *slog.Logger)
- func VersionCommand(version string) func(log *slog.Logger) *cobra.Command
Constants ¶
This section is empty.
Variables ¶
var AppVersion = "unknown"
AppVersion is the version given to VersionCommand
var AutoCommand = fx.Annotate( newRootCommand, fx.ParamTags(`group:"commands"`), )
AutoCommand is an annotated version of NewRootCommand which passes anything previously called with AutoRegister to an annotated version of NewRootCommand. Usage example:
fx.Provide( stdfx.AutoRegister(firstCommandConstructor), stdfx.AutoRegister(secondCommandConstructor), stdfx.AutoCommand, ), fx.Invoke(stdfx.Commander),
var ContainerEntrypointDefaultTools = []string{"sh", "/bin/sh", "bash", "/bin/bash"}
ContainerEntrypointDefaultTools are the default tools for ContainerEntrypoint
var ErrContextMissingShutdowner = errors.New("context is missing shutdowner")
ErrContextMissingShutdowner can be returned by Shutdown
var ( // ErrRunningAsRoot can be returned by [Unprivileged] ErrRunningAsRoot = errors.New("running as root is dangerous and prohibited") )
Functions ¶
func AutoRegister ¶
AutoRegister annotates a *cobra.Command constructor f to be automatically registered as a sub command in NewRootCommand. Usage example:
fx.Provide( stdfx.AutoRegister(firstCommandConstructor), stdfx.AutoRegister(secondCommandConstructor), stdfx.AutoCommand, ), fx.Invoke(stdfx.Commander),
func Commander ¶
Commander can be used as a *cobra.Command invoker for fx. It will start cmd with Context.Background() in a goroutine. It is typically used as last Invoke option in an fx.App to actually start the application using a previously provided root *cobra.Command. The started *cobra.Command shall use cmd.Context() to watch for Done(). The ctx of cmd.Context() will be cancelled when it is time to shutdown. Failure to track cmd.Context() will kill your application after fx.DefaultTimeout - 15 seconds. fx.Lifecycle and fx.Shutdowner are injected into cmd.Context() and can be retrieved by calling [ExtractFromContext].
func ConfigCommand ¶
ConfigCommand is a *cobra.Command constructor to print, modify and validate config.
func ConfigFile ¶
ConfigFile provides your fx.App with a ConfigProvider[T] constructor. The provider - when being constructed - can be used to search for, read and unmarshal your config file to a struct of type *T or error. [configName] shall be the name of a config file without extension. Internally this curries both functions [config.NewSourceFile] and [config.NewProvider] for syntactic sugar. Usage example:
fx.Provide(stdfx.Config[mypkg.ConfStruct]("configname")),
After providing as described above, you will be able to request this provider to fetch the actual config *struct like this:
func buildCommand(
provider stdfx.ConfigProvider[mypkg.ConfStruct],
) *cobra.Command {
// fetch the config of type *mypkg.ConfStruct
cfg, err := provider.Config()
if err != nil {
// ...
}
// do something using cfg (construct a *cobra.Command for example)
}
func ContainerEntrypoint ¶ added in v0.1.8
func ContainerEntrypoint(tools ...string) func()
ContainerEntrypoint might be used with fx.Invoke and tooling when the calling go program is packaged into a container where it is used as the entrypoint. This will execute any value of `tools` when given as the first argument and if found in $PATH. If tools is empty it will use a default list: ContainerEntrypointDefaultTools. A special value of '*' allows for any tool. There is extra handling when the first argument is the binary name itself: For such cases that argument is silbently shifted out and execution continues.
Example usage:
- fx.Invoke(stdfx.ContainerEntrypoint())
- fx.Invoke(stdfx.ContainerEntrypoint("sh", "bash", "whoami"))
- fx.Invoke(stdfx.ContainerEntrypoint("*"))
Resulting container image invocations:
- docker run --rm -it ghcr.io/choopm/myproject:latest sh -c 'echo hello world'
- docker run --rm -it ghcr.io/choopm/myproject:latest bash -i
- docker run --rm -it ghcr.io/choopm/myproject:latest whoami
- docker run --rm -it ghcr.io/choopm/myproject:latest myproject -c ...
func Shutdown ¶
Shutdown uses fx.Shutdowner from ctx to shutdown a fx.App using exitCode. This works when Commander was used to start it. You can use this to shutdown an application unaware of fx during runtime. It might return ErrContextMissingShutdowner in which case it is up to you to directly call os.Exit or panic.
func Unprivileged ¶
func Unprivileged() error
Unprivileged returns an error if being run as root. This takes effect whenever the real or effective user id of the current user process is 0.
func UnprivilegedWarn ¶
UnprivilegedWarn warns if being run as root. This takes effect whenever the real or effective user id of the current user process is 0.
Types ¶
This section is empty.