Skip to content

Commit

Permalink
添加扩展命令行管理
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 4, 2023
1 parent 207b4e2 commit 3c7542d
Show file tree
Hide file tree
Showing 16 changed files with 601 additions and 167 deletions.
14 changes: 12 additions & 2 deletions extension/lakego/demo/demo/provider/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ func (this *Demo) Register() {

// 引导
func (this *Demo) Boot() {
// todo
// Boot
}

// 导入扩展
func (this *Demo) loadExtInfo() {
// 加载后
extension.Booting(func() {
logger.New().Error("demo Booting")
})

// 加载前
extension.Booted(func() {
logger.New().Error("demo Booted")
})

slug := "lakego-admin.ext.demo"

extension.Extend(extension.Extension{
Name: "deatil.demo",
Name: "lakego.demo",
Title: "扩展示例",
Description: "扩展示例",
Keywords: []string{
Expand Down
126 changes: 126 additions & 0 deletions pkg/lakego-app/doak-extension/extension/cmd/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cmd

import (
"fmt"
"errors"

"github.com/deatil/lakego-doak/lakego/command"

"github.com/deatil/lakego-doak-extension/extension/service"
)

/**
* 扩展管理
*
* > ./main lakego-admin:extension --action=[action] --name=[name]
* > main.exe lakego-admin:extension --action=[action] --name=[name]
* > go run main.go lakego-admin:extension --action=[action] --name=[name]
*
* > go run main.go lakego-admin:extension --action=local
* > go run main.go lakego-admin:extension --action=inatll --name=lakego.demo
* > go run main.go lakego-admin:extension --action=uninstall --name=lakego.demo
* > go run main.go lakego-admin:extension --action=upgrade --name=lakego.demo
* > go run main.go lakego-admin:extension --action=enable --name=lakego.demo
* > go run main.go lakego-admin:extension --action=disable --name=lakego.demo
* > go run main.go lakego-admin:extension --action=sort --name=lakego.demo --sort=105
*
* @create 2023-7-3
* @author deatil
*/
var ExtensionCmd = &command.Command{
Use: "lakego-admin:extension",
Short: "lakego-admin extension ctl.",
Example: "{execfile} lakego-admin:extension --action=[action] --name=[name]",
SilenceUsage: true,
PreRun: func(cmd *command.Command, args []string) {

},
Run: func(cmd *command.Command, args []string) {
ExtensionCtl()
},
}

var action string
var name string
var sort int

func init() {
pf := ExtensionCmd.Flags()
pf.StringVarP(&action, "action", "a", "", "操作类型")
pf.StringVarP(&name, "name", "n", "", "扩展名称")
pf.IntVarP(&sort, "sort", "s", 100, "扩展排序值")

command.MarkFlagRequired(pf, "action")
}

// 重设权限
func ExtensionCtl() {
if action == "" {
fmt.Println("操作类型不能为空")
return
}

switch name {
case "inatll", "uninstall", "upgrade",
"enable", "disable", "sort":
if name == "" {
fmt.Println("扩展名称不能为空")
return
}
}

err := errors.New("操作类型不存在")

newExtension := service.NewExtension()

switch action {
case "local":
exts := newExtension.Local()

fmt.Println("本地扩展列表:")
for i, ext := range exts {
fmt.Println(fmt.Sprintf("%d: %s(%s)", i+1, ext["name"], ext["version"]))
}

return
case "inatll":
err = newExtension.Inatll(name)
if err == nil {
fmt.Println("安装扩展成功")
return
}
case "uninstall":
err = newExtension.Uninstall(name)
if err == nil {
fmt.Println("卸载扩展成功")
return
}
case "upgrade":
err = newExtension.Upgrade(name)
if err == nil {
fmt.Println("更新扩展成功")
return
}
case "enable":
err = newExtension.Enable(name)
if err == nil {
fmt.Println("启用扩展成功")
return
}
case "disable":
err = newExtension.Disable(name)
if err == nil {
fmt.Println("禁用扩展成功")
return
}
case "sort":
err = newExtension.Listorder(name, sort)
if err == nil {
fmt.Println("更新扩展排序成功")
return
}
}

fmt.Println(err.Error())
}

14 changes: 7 additions & 7 deletions pkg/lakego-app/doak-extension/extension/controller/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (this *Extension) Local(ctx *router.Context) {
// @Produce application/json
// @Param name query string true "扩展名称"
// @Success 200 {string} json "{"success": true, "code": 0, "message": "string", "data": ""}"
// @Router /extension/:name/install [post]
// @Router /extension/{name}/install [post]
// @Security Bearer
// @x-lakego {"slug": "lakego-admin.extension.install"}
func (this *Extension) Inatll(ctx *router.Context) {
Expand Down Expand Up @@ -262,7 +262,7 @@ func (this *Extension) Inatll(ctx *router.Context) {
// @Produce application/json
// @Param name query string true "扩展名称"
// @Success 200 {string} json "{"success": true, "code": 0, "message": "string", "data": ""}"
// @Router /extension/:name/uninstall [delete]
// @Router /extension/{name}/uninstall [delete]
// @Security Bearer
// @x-lakego {"slug": "lakego-admin.extension.uninstall"}
func (this *Extension) Uninstall(ctx *router.Context) {
Expand Down Expand Up @@ -306,7 +306,7 @@ func (this *Extension) Uninstall(ctx *router.Context) {
// @Produce application/json
// @Param name query string true "扩展名称"
// @Success 200 {string} json "{"success": true, "code": 0, "message": "string", "data": ""}"
// @Router /extension/:name/upgrade [put]
// @Router /extension/{name}/upgrade [put]
// @Security Bearer
// @x-lakego {"slug": "lakego-admin.extension.upgrade"}
func (this *Extension) Upgrade(ctx *router.Context) {
Expand Down Expand Up @@ -351,7 +351,7 @@ func (this *Extension) Upgrade(ctx *router.Context) {
// 检测升级版本
err = version.VersionCheck(info.Version, fmt.Sprintf("> %s", installInfo.Version))
if err != nil {
this.Error(ctx, fmt.Sprintf("扩展[%s]升级版本[%s]错误", installInfo.Version, info.Version))
this.Error(ctx, fmt.Sprintf("扩展[%s]升级到版本[%s]错误", installInfo.Version, info.Version))
return
}

Expand Down Expand Up @@ -393,7 +393,7 @@ func (this *Extension) Upgrade(ctx *router.Context) {
// @Param name query string true "扩展名称"
// @Param listorder formData string true "排序值"
// @Success 200 {string} json "{"success": true, "code": 0, "message": "string", "data": ""}"
// @Router /extension/:name/sort [patch]
// @Router /extension/{name}/sort [patch]
// @Security Bearer
// @x-lakego {"slug": "lakego-admin.extension.sort"}
func (this *Extension) Listorder(ctx *router.Context) {
Expand Down Expand Up @@ -448,7 +448,7 @@ func (this *Extension) Listorder(ctx *router.Context) {
// @Produce application/json
// @Param name query string true "扩展名称"
// @Success 200 {string} json "{"success": true, "code": 0, "message": "string", "data": ""}"
// @Router /extension/:name/enable [patch]
// @Router /extension/{name}/enable [patch]
// @Security Bearer
// @x-lakego {"slug": "lakego-admin.extension.enable"}
func (this *Extension) Enable(ctx *router.Context) {
Expand Down Expand Up @@ -496,7 +496,7 @@ func (this *Extension) Enable(ctx *router.Context) {
// @Produce application/json
// @Param name query string true "扩展名称"
// @Success 200 {string} json "{"success": true, "code": 0, "message": "string", "data": ""}"
// @Router /extension/:name/disable [patch]
// @Router /extension/{name}/disable [patch]
// @Security Bearer
// @x-lakego {"slug": "lakego-admin.extension.disable"}
func (this *Extension) Disable(ctx *router.Context) {
Expand Down
17 changes: 0 additions & 17 deletions pkg/lakego-app/doak-extension/extension/middleware/boot.go

This file was deleted.

41 changes: 9 additions & 32 deletions pkg/lakego-app/doak-extension/extension/provider/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ import (
admin_route "github.com/deatil/lakego-doak-admin/admin/support/route"

"github.com/deatil/lakego-doak-extension/extension/extension"
ext_cmd "github.com/deatil/lakego-doak-extension/extension/cmd"
ext_router "github.com/deatil/lakego-doak-extension/extension/route"
ext_middleware "github.com/deatil/lakego-doak-extension/extension/middleware"
)

// 路由中间件
var routeMiddlewares = map[string]router.HandlerFunc{
// 操作日志
"lakego-admin.extension": ext_middleware.NewBoot(),
}

// 中间件分组
var middlewareGroups = map[string][]string{
// 扩展
"lakego-admin": {
"lakego-admin.extension",
},
}

/**
* 服务提供者
*
Expand All @@ -37,34 +23,25 @@ type Extension struct {

// 注册
func (this *Extension) Register() {
// 中间件
this.loadMiddleware()
// Register
}

// 引导
func (this *Extension) Boot() {
// 脚本
this.loadCommand()

// 路由
this.loadRoute()

// 加载扩展
this.loadExtension()
}

// 导入中间件
func (this *Extension) loadMiddleware() {
m := router.InstanceMiddleware()

// 导入中间件
for name, value := range routeMiddlewares {
m.AliasMiddleware(name, value)
}

// 导入中间件分组
for groupName, middlewares := range middlewareGroups {
for _, middleware := range middlewares {
m.PushMiddlewareToGroup(groupName, middleware)
}
}
// 导入脚本
func (this *Extension) loadCommand() {
// 扩展管理
this.AddCommand(ext_cmd.ExtensionCmd)
}

// 导入路由
Expand Down
Loading

0 comments on commit 3c7542d

Please sign in to comment.