base

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package base defines shared basic pieces of the commands, in particular logging and the Command structure.

Index

Constants

This section is empty.

Variables

View Source
var SortLessFunc = func(i, j *Command) bool {
	return i.Name() < j.Name()
}

SortLessFunc used for sort commands list, can be override from outside

Functions

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs error and set exit status to 1, but not exit

func Execute

func Execute()

Execute excute the commands

func Exit

func Exit()

Exit exits with code set with SetExitStatus()

func ExitIfErrors

func ExitIfErrors()

ExitIfErrors exits if current status is not zero

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs error and exit with code 1

func GetExitStatus

func GetExitStatus() int

GetExitStatus get exit status code

func Help

func Help(w io.Writer, args []string)

Help implements the 'help' command.

func PrintUsage

func PrintUsage(w io.Writer, cmd *Command)

PrintUsage prints usage of cmd to w

func RegisterCommand

func RegisterCommand(cmd *Command)

RegisterCommand register a command to RootCommand

func SetExitStatus

func SetExitStatus(n int)

SetExitStatus set exit status code

func Sort

func Sort()

Sort sorts the commands

Types

type Command

type Command struct {
	// Run runs the command.
	// The args are the arguments after the command name.
	Run func(cmd *Command, args []string)

	// UsageLine is the one-line usage message.
	// The words between the first word (the "executable name") and the first flag or argument in the line are taken to be the command name.
	//
	// UsageLine supports go template syntax. It's recommended to use "{{.Exec}}" instead of hardcoding name
	UsageLine string

	// Short is the short description shown in the 'go help' output.
	//
	// Note: Short does not support go template syntax.
	Short string

	// Long is the long message shown in the 'go help <this-command>' output.
	//
	// Long supports go template syntax. It's recommended to use "{{.Exec}}", "{{.LongName}}" instead of hardcoding strings
	Long string

	// Flag is a set of flags specific to this command.
	Flag flag.FlagSet

	// CustomFlags indicates that the command will do its own
	// flag parsing.
	CustomFlags bool

	// Commands lists the available commands and help topics.
	// The order here is the order in which they are printed by 'go help'.
	// Note that subcommands are in general best avoided.
	Commands []*Command
}

A Command is an implementation of a xray command like xray run or xray version.

var RootCommand *Command

RootCommand is the root command of all commands

func (*Command) LongName

func (c *Command) LongName() string

LongName returns the command's long name: all the words in the usage line between first word (e.g. "xray") and a flag or argument,

func (*Command) Name

func (c *Command) Name() string

Name returns the command's short name: the last word in the usage line before a flag or argument.

func (*Command) Runnable

func (c *Command) Runnable() bool

Runnable reports whether the command can be run; otherwise it is a documentation pseudo-command such as importpath.

func (*Command) Usage

func (c *Command) Usage()

Usage prints usage of the Command

type CommandEnvHolder

type CommandEnvHolder struct {
	// Excutable name of current binary
	Exec string
	// commands column width of current command
	CommandsWidth int
}

CommandEnvHolder is a struct holds the environment info of commands

var CommandEnv CommandEnvHolder

CommandEnv holds the environment info of commands

Jump to

Keyboard shortcuts

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