Documentation
¶
Overview ¶
Package common provides shared utilities and helper functions for CLI commands. It includes progress bar initialization, error handling, help display, and text formatting utilities used across the WarpDL command-line interface.
Index ¶
- Variables
- func Beaut(s string, n int) (b string)
- func GetVersion(ctx *cli.Context) error
- func Help(ctx *cli.Context) error
- func InitBars(p *mpb.Progress, prefix string, cLength int64) (dbar *mpb.Bar, cbar *mpb.Bar)
- func InitBarsWithProgress(p *mpb.Progress, prefix string, cLength int64, initialProgress int64) (dbar *mpb.Bar, cbar *mpb.Bar)
- func PrintErrWithCmdHelp(ctx *cli.Context, err error) error
- func PrintErrWithHelp(ctx *cli.Context, err error) error
- func PrintRuntimeErr(ctx *cli.Context, cmd, action string, err error)
- func SetShowAppHelpAndExit(fn func(*cli.Context, int)) func(*cli.Context, int)
- func SetShowCommandHelp(fn func(*cli.Context, string) error) func(*cli.Context, string) error
- func UsageErrorCallback(ctx *cli.Context, err error, _ bool) error
Constants ¶
This section is empty.
Variables ¶
var VersionCmdStr string
VersionCmdStr holds the formatted version string displayed by the version command. It is populated at runtime by the Execute function with build-time information including version, platform, build date, and commit hash.
Functions ¶
func Beaut ¶
Beaut centers a string within a field of width n by padding with spaces. If the string length is less than n, spaces are added equally on both sides. If n minus the string length is odd, an extra space is appended at the end. This is useful for creating centered text in fixed-width displays.
func GetVersion ¶
GetVersion prints the version string to stdout and returns nil. The version string includes the application name, version, platform, build date, and commit hash as configured in VersionCmdStr.
func Help ¶
Help displays help information for the application or a specific command. If no argument is provided or the argument is "help", it displays the application-level help and exits. Otherwise, it shows help for the specified command name.
func InitBars ¶
InitBars creates and configures download and compile progress bars. It returns two progress bars: dbar for tracking download progress and cbar for tracking file compilation/assembly progress. The prefix parameter is prepended to bar labels, and cLength specifies the total content length for progress calculation. Both bars use a visual block-style display.
func InitBarsWithProgress ¶ added in v1.3.21
func InitBarsWithProgress(p *mpb.Progress, prefix string, cLength int64, initialProgress int64) (dbar *mpb.Bar, cbar *mpb.Bar)
InitBarsWithProgress creates progress bars with an initial progress value. This is used when resuming downloads where some bytes are already downloaded. The initialProgress parameter sets the starting position of the download bar.
func PrintErrWithCmdHelp ¶
PrintErrWithCmdHelp prints the error message followed by the current command's help text. It is used for errors that occur in the context of a specific subcommand.
func PrintErrWithHelp ¶
PrintErrWithHelp prints the error message followed by the application-level help text and exits with status code 1. It is used for errors that occur at the application level rather than within a specific command.
func PrintRuntimeErr ¶
PrintRuntimeErr formats and prints a runtime error message to stdout. It includes the application name, command name, action identifier, and the error message. If err is nil, it prints a diagnostic message indicating no error was present. The ctx parameter may be nil, in which case the application name is derived from os.Args[0].
func SetShowAppHelpAndExit ¶ added in v1.3.11
SetShowAppHelpAndExit sets the function used to show app help and exit. It returns the previous function, allowing for restoration after testing. This is primarily used for testing to avoid os.Exit calls.
func SetShowCommandHelp ¶ added in v1.3.11
SetShowCommandHelp sets the function used to show command help. It returns the previous function, allowing for restoration after testing. This is primarily used for testing.
func UsageErrorCallback ¶
UsageErrorCallback handles usage errors from the CLI framework. It determines whether the error occurred at the command level or application level and displays the appropriate help text along with the error message. This function is designed to be used as the OnUsageError callback for cli.App and cli.Command.
Types ¶
This section is empty.