urfave_cli_integration

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: MIT Imports: 11 Imported by: 0

README

ComponeGo Framework + Urfave CLI

Go Report Card Tests Go Reference

This package provides easy integration of the Componego Framework with Urfave CLI V2.

All Urfave CLI features are supported. You can use a convenient solution for processing command line arguments with the Componego Framework together.

Documentation

The application must be launched in a special way for this to work.

main.go
package main

import (
    "github.com/componego/componego"
    "github.com/componego/urfave-cli-integration"

    "github.com/YOUR-USER-OR-ORG-NAME/YOUR-REPO-NAME/internal/application"
)

func main() {
    urfave_cli_integration.RunAndExit(application.New(), componego.ProductionMode)
}

This is necessary because the current integration may launch different componego applications depending on the command line arguments.

Inside the application, you can access the Urfave CLI context. You can also modify any command options. For example, add a couple of required flags for the command line.

application.go
package application

import (
    "github.com/componego/componego"
    "github.com/componego/urfave-cli-integration"
    "github.com/urfave/cli/v2"
)

type Application struct {
}

func New() *Application {
    return &Application{}
}

// ApplicationName belongs to interface componego.Application.
func (a *Application) ApplicationName() string {
    return "Application Name v0.0.1"
}

// ApplicationCLI belongs to interface urfave_cli_integration.ApplicationCLI.
func (a *Application) ApplicationCLI(cmd *cli.Command) {
    cmd.Flags = []cli.Flag{
        &cli.StringFlag{
            Name:     "config",
            Usage:    "config filename",
            Required: true,
        },
    }
    // ...
}

// ApplicationConfigInit belongs to interface componego.ApplicationConfigInit.
func (a *Application) ApplicationConfigInit(_ componego.ApplicationMode, options any) (map[string]any, error) {
    cliCtx := options.(*cli.Context) // <----
    // ...
}

// ApplicationAction belongs to interface componego.Application.
func (a *Application) ApplicationAction(env componego.Environment, options any) (int, error) {
    cliCtx := options.(*cli.Context) // <----
    // ...
}

var (
    _ componego.Application                 = (*Application)(nil)
    _ componego.ApplicationConfigInit       = (*Application)(nil)
    _ urfave_cli_integration.ApplicationCLI = (*Application)(nil)
)

Subcommands can be added like this:

// ApplicationCLI belongs to interface urfave_cli_integration.ApplicationCLI.
func (a *Application) ApplicationCLI(cmd *cli.Command) {
    cmd.Subcommands = []*cli.Command{
        urfave_cli_integration.ToCommand("subcommand", application.New()),
    }
    // ....
}

Examples available here.

Contributing

We are open to improvements and suggestions. Pull requests are welcome.

License

The source code of the repository is licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ToAction      = toAction
	ToApplication = toApplication
	ToCommand     = toCommand
)

noinspection ALL

Functions

func Run

Run runs the application and returns the exit code.

func RunAndExit

func RunAndExit(app componego.Application, appMode componego.ApplicationMode)

RunAndExit runs the application and exits the program after stopping the application.

func RunGracefullyAndExit

func RunGracefullyAndExit(app componego.Application, appMode componego.ApplicationMode)

RunGracefullyAndExit runs the application and stops it gracefully.

func RunWithContext

func RunWithContext(ctx context.Context, app componego.Application, appMode componego.ApplicationMode) int

RunWithContext runs the application with context and returns the exit code.

Types

type ApplicationCLI

type ApplicationCLI interface {
	componego.Application
	ApplicationCLI(cmd *cli.Command)
}

Jump to

Keyboard shortcuts

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