cmd

package
v0.0.0-...-e2602fe Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ApolloIP = "apolloIp"
	Port     = "port"
)

Variables

View Source
var CheckCfgCmd = &gcmd.Command{
	Name:        "checkCfg",
	Usage:       "./main checkCfg",
	Brief:       "check config",
	Description: "检查配置文件是否符合规范",
	Arguments: append(CommonArguments, []gcmd.Argument{
		{
			Name:   "env",
			Short:  "e",
			Brief:  "要检查的环境,可选值: prod, dev。默认dev",
			IsArg:  false,
			Orphan: false,
		},
		{
			Name:   "all",
			Short:  "a",
			Brief:  "强制检查下方所有配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "grpc",
			Short:  "g",
			Brief:  "强制检查grpc服务配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "server",
			Short:  "s",
			Brief:  "强制检查server服务配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "rocketmq",
			Short:  "m",
			Brief:  "强制检查rocketmq配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "xxljob",
			Short:  "j",
			Brief:  "强制检查xxlJob配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "apollo",
			Short:  "c",
			Brief:  "强制检查apollo配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "otlp",
			Short:  "o",
			Brief:  "强制检查otlp链路追踪配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "registry",
			Short:  "y",
			Brief:  "强制检查服务注册发现registry配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "redis",
			Short:  "r",
			Brief:  "强制检查服务注册发现redis配置",
			IsArg:  false,
			Orphan: true,
		},
		{
			Name:   "database",
			Short:  "d",
			Brief:  "强制检查数据库配置",
			IsArg:  false,
			Orphan: true,
		},
	}...,
	),
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		cfg.Check(ctx, parser, GetCommonArguments(ctx, parser, Port).String())
		return
	},
}
View Source
var (
	// CommonArguments 命令行公共参数
	CommonArguments = []gcmd.Argument{
		{
			Name:   Port,
			Short:  "",
			Brief:  "端口,参考格式:`:8080`,默认使用配置文件的`server.address`",
			IsArg:  false,
			Orphan: false,
		},
		{
			Name:   ApolloIP,
			Short:  "",
			Brief:  "apollo连接地址,默认使用配置文件的`apollo.IP`或无",
			IsArg:  false,
			Orphan: false,
		},
	}
)
View Source
var (
	GenHttpForGrpc = &gcmd.Command{
		Name:  "genHttpForGrpc",
		Usage: "./main genHttpForGrpc",
		Brief: "gen http for grpc cmd and controller",
		Arguments: append(CommonArguments, []gcmd.Argument{
			{
				Name:   "sources",
				Short:  "s",
				Brief:  "grpc的控制器文件路径,多个文件逗号分割,可以是基于根目录的相对路径,或绝对路径",
				IsArg:  false,
				Orphan: false,
			},
			{
				Name:   "output",
				Short:  "o",
				Brief:  "控制器文件输出目录,默认为`internal/controller/http/`",
				IsArg:  false,
				Orphan: false,
			},
			{
				Name:   "cmd",
				Short:  "c",
				Brief:  "cmd文件输出目录,默认为`internal/cmd/`",
				IsArg:  false,
				Orphan: false,
			},
		}...),
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			controllerPath := parser.GetOpt("sources", "").String()
			folderPath := parser.GetOpt("output", "internal/controller/http/").String()
			cmdFolderPath := parser.GetOpt("cmd", "internal/cmd/").String()

			if strings.TrimSpace(controllerPath) == "" {
				return fmt.Errorf("output is empty")
			}

			var controllers []*GrpcController
			for _, v := range strings.Split(controllerPath, ",") {

				ret, err1 := GenHttpForGrpcControllers(v, folderPath)
				if err1 != nil {
					err = err1
					return
				}
				controllers = append(controllers, ret...)
			}

			err = GenHttpForGrpcCmd(controllers, cmdFolderPath)
			if err != nil {
				return
			}
			return
		},
	}
)

Functions

func BindHttpHealthApi

func BindHttpHealthApi(group *ghttp.RouterGroup)

BindHttpHealthApi 绑定健康检查api

func GenHttpForGrpcCmd

func GenHttpForGrpcCmd(controllers []*GrpcController, folderPath string) (err error)

func GenMain

func GenMain(mainSrv gcmd.Command, subSrv ...*gcmd.Command) gcmd.Command

GenMain 标准化生成应用主命令

func GetCommonArguments

func GetCommonArguments(ctx context.Context, parser *gcmd.Parser, pattern string, isGrpc ...bool) (ret *gvar.Var)

func GetConsumeCmdFunc

func GetConsumeCmdFunc(name string, middleware MiddlewareForCmd, consumeFunc ConsumeFunc) func(ctx context.Context, parser *gcmd.Parser) error

GetConsumeCmdFunc 标准化消费者服务方法

示例:
gcmd.Command{
	Name:  "demoConsumer",
	Usage: "./main demoConsumer",
	Brief: "批量删除博客的消费者",
	Func: cmd.GetConsumeCmdFunc("demoConsumer", cmd.GetGrpcMiddleware(), demoConsumeFunc),
}

func GetGrpcCmdFunc

func GetGrpcCmdFunc(middleware MiddlewareForCmd, grpcRegFunc GrpcRegFunc) func(ctx context.Context, parser *gcmd.Parser) error

GetGrpcCmdFunc 标准化grpc服务方法

示例:
gcmd.Command{
	Name:  "main",
	Usage: "./main",
	Brief: "主命令",
	Func: cmd.GetGrpcCmdFunc(cmd.GetGrpcMiddleware(), func(server *grpcx.GrpcServer){
		demoController.Register(server)
	}),
}

func GetHttpCmdFunc

func GetHttpCmdFunc(middleware MiddlewareForCmd, useMiddlewares []ghttp.HandlerFunc, serverGroups []ServerGroup, optionApplyFuncs ...HttpCmdOptionApply) func(ctx context.Context, parser *gcmd.Parser) error

GetHttpCmdFunc 标准化http服务方法

示例:
gcmd.Command{
	Name:  "main",
	Usage: "main",
	Brief: "start http server",
	Func: cmd.GetHttpCmdFunc(
		cmd.GetHttpMiddleware(),
		[]ghttp.HandlerFunc{},
		[]cmd.ServerGroup{
			{
				Prefix: "/",
				Middlewares: []ghttp.HandlerFunc{},
				Controllers: []interface{}{
					controller.Demo,
				},
			},
		},
	),
}

func GetXxljobCmdFunc

func GetXxljobCmdFunc(middleware MiddlewareForCmd, taskMap map[string]xxl.TaskFunc) func(ctx context.Context, parser *gcmd.Parser) error

GetXxljobCmdFunc 标准化xxljob服务方法

示例:
gcmd.Command{
	Name:  "xxlJob",
	Usage: "./main xxlJob",
	Brief: "定时任务",
	Func: cmd.GetConsumeCmdFunc(cmd.GetGrpcMiddleware(), map[string]xxl.TaskFunc{
		"task1": func(cxt context.Context, param *RunReq) string{return ""},
		"task2": func(cxt context.Context, param *RunReq) string{return ""}
	}]),
}

func GoFmt

func GoFmt(path string)

func HttpForGrpcFunc

func HttpForGrpcFunc(r *ghttp.Request, req interface{}, f func(ctx context.Context, req interface{}) (res interface{}, err error))

Types

type ConsumeFunc

type ConsumeFunc func(ctx context.Context, parser *gcmd.Parser) (stopFunc func(), err error)

type GrpcController

type GrpcController struct {
	ControllerName string        //原始控制器完整包及名称
	Package        string        //包名
	Name           string        //控制器名称
	OriginName     string        //原始结构体名称
	Methods        []*GrpcMethod //方法列表
}

func GenHttpForGrpcControllers

func GenHttpForGrpcControllers(controllerPath string, folderPath string) (controllers []*GrpcController, err error)

func ParseGrpcControllerFile

func ParseGrpcControllerFile(filePath string) (controllers []*GrpcController, err error)

type GrpcMethod

type GrpcMethod struct {
	Name    string
	ReqName string
}

type GrpcRegFunc

type GrpcRegFunc func(server *grpcx.GrpcServer)

GrpcRegFunc 注册服务接口方法

type HttpCmdOption

type HttpCmdOption struct {
	LogFormatJsonMiddleware func(r *ghttp.Request)
	AccessLogMiddleware     func(r *ghttp.Request)
	ErrorLogMiddleware      func(r *ghttp.Request)
	ResponseMiddleware      func(r *ghttp.Request)
	BindHttpHealthApi       func(group *ghttp.RouterGroup)
}

type HttpCmdOptionApply

type HttpCmdOptionApply func(option *HttpCmdOption)

func WithHttpCmdAccessLogMiddleware

func WithHttpCmdAccessLogMiddleware(v func(r *ghttp.Request)) HttpCmdOptionApply

func WithHttpCmdBindHttpHealthApi

func WithHttpCmdBindHttpHealthApi(v func(group *ghttp.RouterGroup)) HttpCmdOptionApply

func WithHttpCmdErrorLogMiddleware

func WithHttpCmdErrorLogMiddleware(v func(r *ghttp.Request)) HttpCmdOptionApply

func WithHttpCmdLogFormatJsonMiddleware

func WithHttpCmdLogFormatJsonMiddleware(v func(r *ghttp.Request)) HttpCmdOptionApply

func WithHttpCmdResponseMiddleware

func WithHttpCmdResponseMiddleware(v func(r *ghttp.Request)) HttpCmdOptionApply

type MiddlewareForCmd

type MiddlewareForCmd func(ctx context.Context, parser *gcmd.Parser) (stopFunc func())

func GetGrpcMiddleware

func GetGrpcMiddleware() MiddlewareForCmd

GetGrpcMiddleware grpc服务命令全局中间件

func GetHttpMiddleware

func GetHttpMiddleware() MiddlewareForCmd

GetHttpMiddleware http服务命令全局中间件

type ServerGroup

type ServerGroup struct {
	Prefix      string //前缀,默认"/"
	Middlewares []ghttp.HandlerFunc
	Controllers []interface{}
}

Jump to

Keyboard shortcuts

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