urfave_cli_integration

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 9 Imported by: 0

README

ComponeGo Framework + Urfave CLI

Go Report Card Tests Go Reference

This package enables seamless integration of Componego Framework with Urfave CLI V2.

It supports all features of the Urfave CLI, offering a convenient solution for processing command-line arguments within the Componego Framework.

Documentation

An application must be launched in a specific way for this functionality 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.

You can access the Urfave CLI context and modify any command options within the application. For example, you can 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 as follows:

// 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 are 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 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