cli

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2021 License: MIT Imports: 9 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupConsole

func SetupConsole(ctx context.Context, factory ConsoleFactory) error

SetupConsole 设置控制台接口

Types

type AsyncClient

type AsyncClient interface {
	GetContext() context.Context
	GetFactory() ClientFactory

	ExecuteTask(task *Task) task.Promise
	ExecuteScript(script string) task.Promise
	Execute(cmd string, args []string) task.Promise
}

AsyncClient 是用于执行命令的异步 API

type Client

type Client interface {
	GetContext() context.Context
	GetFactory() ClientFactory

	ExecuteTask(task *Task) error
	Execute(cmd string, args []string) error

	// execute multi-line commands
	ExecuteScript(script string) error
}

Client 是用于执行命令的同步 API

type ClientFactory

type ClientFactory interface {
	CreateClient(ctx context.Context) Client
	CreateAsyncClient(ctx context.Context) AsyncClient
}

ClientFactory 是命令客户端的工厂

type CommandHelpInfo

type CommandHelpInfo struct {
	Name        string
	Title       string
	Description string
	Content     string
}

CommandHelpInfo 是命令的帮助信息

type CommandHelper

type CommandHelper interface {
	GetHelpInfo() *CommandHelpInfo
}

CommandHelper 是命令的帮助信息提供者, 通常与Handler 实现于同一个结构

type CommandLineBuilder added in v0.0.2

type CommandLineBuilder struct {
	// contains filtered or unexported fields
}

CommandLineBuilder 命令行创建器

func (*CommandLineBuilder) AppendString added in v0.0.2

func (inst *CommandLineBuilder) AppendString(s string)

AppendString 追加一段字符串

func (*CommandLineBuilder) AppendStrings added in v0.0.2

func (inst *CommandLineBuilder) AppendStrings(items []string)

AppendStrings 追加好几段字符串

func (*CommandLineBuilder) Create added in v0.0.2

func (inst *CommandLineBuilder) Create() string

Create 创建命令行

type CommandLineParser added in v0.0.2

type CommandLineParser struct {
}

CommandLineParser 命令行解析器

func (*CommandLineParser) Parse added in v0.0.2

func (inst *CommandLineParser) Parse(line string) ([]string, error)

Parse 解析命令行

type Console

type Console interface {
	Error() io.Writer
	Output() io.Writer
	Input() io.Reader

	GetWD() string
	GetWorkingPath() fs.Path
	GetLastError() error

	SetWD(wd string)
	SetWorkingPath(wd fs.Path)
	SetError(w io.Writer)
	SetOutput(w io.Writer)
	SetInput(r io.Reader)

	WriteString(str string)
	WriteError(msg string, err error)
}

Console 接口表示一个跟上下文绑定的控制台

func GetConsole

func GetConsole(ctx context.Context) (Console, error)

GetConsole 从上下文取控制台接口

type ConsoleFactory

type ConsoleFactory interface {
	Create() Console
}

ConsoleFactory 是创建 Console 的工厂

type Filter

type Filter interface {
	Init(service Service) error
	Handle(ctx *TaskContext, next FilterChain) error
}

Filter 用来过滤请求

type FilterChain

type FilterChain interface {
	Handle(ctx *TaskContext) error
}

FilterChain 代表过滤器链条

type FilterChainBuilder

type FilterChainBuilder struct {
	// contains filtered or unexported fields
}

func (*FilterChainBuilder) Add

func (inst *FilterChainBuilder) Add(priority int, filter Filter)

func (*FilterChainBuilder) Create

func (inst *FilterChainBuilder) Create(reverse bool) FilterChain

func (*FilterChainBuilder) Len

func (inst *FilterChainBuilder) Len() int

func (*FilterChainBuilder) Less

func (inst *FilterChainBuilder) Less(a, b int) bool

func (*FilterChainBuilder) Swap

func (inst *FilterChainBuilder) Swap(a, b int)

type Handler

type Handler interface {
	Init(service Service) error
	Handle(ctx *TaskContext) error
}

Handler 是某条具体命令的处理器

type Service

type Service interface {
	RegisterHandler(name string, h Handler) error

	FindHandler(name string) (Handler, error)

	GetHandlerNames() []string

	// AddFilter 添加一个过滤器到服务中。
	// priority 是过滤器的优先顺序,数值越大,优先级越高,处理顺序越靠前。
	AddFilter(priority int, filter Filter) error

	GetFilterChain() FilterChain

	GetClient(ctx context.Context) Client

	GetClientFactory() ClientFactory
}

Service 是用来处理命令的服务

type Task

type Task struct {
	Context  context.Context
	TaskList []*TaskUnit
	Script   string
	Reporter task.ProgressReporter
}

Task 表示将要执行的任务

type TaskContext

type TaskContext struct {
	Context     context.Context
	CurrentTask *TaskUnit
	Handler     Handler
	Service     Service
	Reporter    task.ProgressReporter
	TaskList    []*TaskUnit
	Console     Console
}

TaskContext 表示正在执行的任务

func (*TaskContext) Clone

func (inst *TaskContext) Clone() *TaskContext

Clone 生成 TaskContext 的副本(浅拷贝)

type TaskListBuilder

type TaskListBuilder struct {
	// contains filtered or unexported fields
}

TaskListBuilder ...

func (*TaskListBuilder) AddLine

func (inst *TaskListBuilder) AddLine(line string, index int, args []string) error

AddLine @line: 完整的行文本; @index: 行号(base=0); @args: 命令以及参数;

func (*TaskListBuilder) Create

func (inst *TaskListBuilder) Create() []*TaskUnit

Create ...

func (*TaskListBuilder) ParseScript

func (inst *TaskListBuilder) ParseScript(script string) error

ParseScript 解析脚本

type TaskUnit

type TaskUnit struct {
	LineNumber  int      // 行号
	CommandName string   // = args[0]
	CommandLine string   // stringify(args)
	Arguments   []string // 包含命令行的所有参数(包括cmd-name本身)
}

TaskUnit 表示一条简单的命令

Jump to

Keyboard shortcuts

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