goktrl

package module
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 22 Imported by: 3

README

Introduction to goktrl


Zh_CN

goktrl is a powerful interactive shell designed to probe into your multi-processing go projects by using unix domain sockets.

Characteristics


  • interactive shell
  • nice hints for args and options
  • use struct tags for configuration of options
  • automatically parse and verify args and options
  • automatically render table for terminal if enabled
  • automatically handle and print data if enabled(including table data)
  • connect to your process using unix domain sockets
  • dispatching requests easily
  • very intuitive and flexible

Usage


go get -u "github.com/moqsien/goktrl@v1.3.6"
package main

import (
	"fmt"

	"github.com/moqsien/goktrl"
)

/*
  A very simple example by implementing goktrl.
  Even a hook for the shell is not required.
*/

func Handler(c *goktrl.Context) {
	fmt.Printf("$$ server: args = %v\n", c.Args) // args are automatically parsed and stored in c.Args
	Result := map[string]string{
		"hello": "info",
	}
	c.Send(Result)
}

func ShowInfo() *goktrl.Ktrl {
	kt := goktrl.NewKtrl()
	kt.AddKtrlCommand(&goktrl.KCommand{
		Name:            "info",          // name of your shell command
		Help:            "show info",     // help info for your shell command
		KtrlHandler:     Handler,         // view controller for server side
		Auto:            true,            // automatically show results or not
	})
	return kt
}

func main() {
	kt := ShowInfo()
	if len(os.Args) > 1 {
		kt.RunShell()
	} else {
		kt.RunCtrl()
	}
}
  • Some Exibitions shell-0 shell-1 shell-2

License

MIT

Thanks To


dmicro

goframe

gin

ishell

table

Documentation

Index

Constants

View Source
const (
	ContextClient ContextType = 1 // 客户端
	ContextServer ContextType = 2 // 服务端
	ArgsFormatStr string      = "args%sargs"
)
View Source
const (
	Alias       = "alias"
	NeedParse   = "needparse"
	Required    = "required"
	Description = "descr"
)
View Source
const (
	HeadersInJson = "headers"
	RowsInJson    = "rows"
)
View Source
const (
	GoKtrlSockDirEnv string = "GOKTRL_SOCK_DIR"
)
View Source
const (
	TableFieldOrderFlag = "order"
)

Variables

This section is empty.

Functions

func GetSockFilePath added in v1.3.7

func GetSockFilePath(sockName string) (p string)

func ParseOptionsProperties

func ParseOptionsProperties(valType reflect.Type, k *KCommand)

func ParseShellOptions

func ParseShellOptions(o KtrlOpt, k *KCommand) (KtrlOpt, *ParserPlus)

func SetStructValue

func SetStructValue(field reflect.Value, fValue string, found bool)

func ShowHelpStr

func ShowHelpStr(o KtrlOpt) (help string)

Types

type Context

type Context struct {
	*gin.Context
	ShellContext  *ishell.Context
	Type          ContextType
	Options       KtrlOpt
	Args          []string
	Parser        *ParserPlus
	Table         *KtrlTable
	KtrlPath      string
	Client        *KtrlClient
	DefaultSocket string
	ShellCmdName  string
	Result        []byte
}

func (*Context) GetResult

func (that *Context) GetResult(sockName ...string) ([]byte, error)

func (*Context) Send

func (that *Context) Send(content interface{}, code ...int)

type ContextType

type ContextType int

type KCommand

type KCommand struct {
	Name            string           // shell 命令名称
	Help            string           // shell 命令解释
	Func            func(c *Context) // shell 命令钩子函数
	Opts            KtrlOpt          // shell 命令可选参数配置
	KtrlHandler     func(c *Context) // Ktrl服务端视图函数
	SocketName      string           // 默认Unix套接字名称
	ArgsDescription string           // 位置参数说明
	ArgsRequired    bool             // 位置参数是否至少要传一个
	Auto            bool             // 是否自动发送请求并处理结果
	TableObject     interface{}      // 空的表格对象
	ShowTable       bool             // 结果是否在命令行中以表格显示

	ArgsHook func([]string) []string // 在shell中处理Args, 然后向Server发送处理之后的Args
	// contains filtered or unexported fields
}

func (*KCommand) GetKtrlPath

func (that *KCommand) GetKtrlPath() string

type Ktrl

type Ktrl struct {
	CtrlServer *KtrlServer // 服务端
	CtrlShell  *KtrlShell  // 客户端交互式shell
	Multiple   bool        // 是否客户端和服务端在不同进程中执行
}

func NewKtrl

func NewKtrl(ismulti ...bool) *Ktrl

func (*Ktrl) AddKtrlCommand

func (that *Ktrl) AddKtrlCommand(kcmd *KCommand)

func (*Ktrl) RunCtrl

func (that *Ktrl) RunCtrl(sockName ...string)

RunCtrl 运行Ktrl服务端

func (*Ktrl) RunShell

func (that *Ktrl) RunShell(sockName ...string)

RunShell 运行Ktrl交互式shell

type KtrlClient

type KtrlClient struct {
	UnixSocket
	Client *http.Client
	// contains filtered or unexported fields
}

func NewKtrlClient

func NewKtrlClient() *KtrlClient

func (*KtrlClient) GetResult

func (that *KtrlClient) GetResult(urlPath string, params map[string]string, sockName ...string) ([]byte, error)

func (*KtrlClient) ParseParams

func (that *KtrlClient) ParseParams(params map[string]string)

func (*KtrlClient) SetConnection

func (that *KtrlClient) SetConnection()

SetConnition 设置连接

func (*KtrlClient) SetUnixSocket

func (that *KtrlClient) SetUnixSocket(sockName string)

type KtrlOpt

type KtrlOpt interface{}

func ParseServerOptions

func ParseServerOptions(o KtrlOpt, c *gin.Context) KtrlOpt

type KtrlServer

type KtrlServer struct {
	UnixSocket
	Router *gin.Engine // UnixSockHttp 服务端
}

func NewKtrlServer

func NewKtrlServer() *KtrlServer

func (*KtrlServer) AddHandler

func (that *KtrlServer) AddHandler(kcmd *KCommand)

AddHandler 为KtrlServer添加视图函数

func (*KtrlServer) CheckUnixSocket

func (that *KtrlServer) CheckUnixSocket()

func (*KtrlServer) SetUnixSocket

func (that *KtrlServer) SetUnixSocket(sockName string)

func (*KtrlServer) Start

func (that *KtrlServer) Start(sockName ...string) error

type KtrlShell

type KtrlShell struct {
	*ishell.Shell
}

为shell增加参数解析功能

func NewShell

func NewShell() *KtrlShell

func (*KtrlShell) AddCmd

func (that *KtrlShell) AddCmd(kcmd *KCommand)

type KtrlTable

type KtrlTable struct {
	*table.Table
	Headers  []string
	RowCache []string
}

func NewKtrlTable

func NewKtrlTable() *KtrlTable

func (*KtrlTable) AddRowsByJsonString

func (that *KtrlTable) AddRowsByJsonString(jsonString string)

AddRowsByJsonString 为表格添加数据行; jsonString 格式: {headers: ["", "", ""], rows: [["", "", ""], ["", "", ""], ["", "", ""]]}

func (*KtrlTable) AddRowsByListObject

func (that *KtrlTable) AddRowsByListObject(dataList interface{})
  AddRowsByListObject 为表格添加数据行;
  dataList 数据类型格式: []Data 或 []*Data;
  Data为struct, 结构示例如下:
    type Data struct {
		FieldOne string `order:"1"`
		FieldTwo string `order:"2"`
	}

func (*KtrlTable) ParseHeadersFromObject

func (that *KtrlTable) ParseHeadersFromObject(vType reflect.Type)

func (*KtrlTable) ParseHeadersFromString

func (that *KtrlTable) ParseHeadersFromString(jsonStr string)

func (*KtrlTable) RestTable

func (that *KtrlTable) RestTable()

RestTable 重置KtrlTable

type Options

type Options struct {
	OptList  g.MapStrBool // 是否解析参数的值
	Required g.MapStrBool // 是否必传
}

type ParserPlus

type ParserPlus struct {
	*gcmd.Parser
	Params map[string]string
}

type UnixSocket

type UnixSocket struct {
	UnixSocketName string // 套接字名称
	UnixSocketPath string // 套接字存放完整路径
}

Unix套接字

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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