cmd

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Main = &gcmd.Command{
	Name:  "vgo",
	Brief: "vgo 命令行工具",
}

Main 主命令,包含所有子命令 在程序启动时会自动注册所有模块的命令

View Source
var Root = &gcmd.Command{
	Name:  "vgo",
	Brief: "vgo 命令行工具",
}

Root 根命令定义 该变量定义了vgo命令行工具的根命令,所有子命令都会注册到这个根命令下

View Source
var Utils = &gcmd.Command{
	Name:  "utils",
	Usage: "utils COMMAND [ARGUMENT]",
	Brief: "通用工具命令集合",
	Description: `
提供一系列通用工具命令,用于简化开发和运维工作。
包括文件操作、环境检查、资源打包等功能。
`,
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		g.Log().Info(ctx, "请指定要执行的子命令,使用 -h 查看帮助")
		return nil
	},
}

定义utils命令

View Source
var UtilsClean = &gcmd.Command{
	Name:  "clean",
	Usage: "utils clean [TYPE]",
	Brief: "清理临时文件",
	Description: `
清理项目中的临时文件和缓存。
支持的类型:
  all    - 清理所有临时文件和缓存
  temp   - 只清理临时文件
  cache  - 只清理缓存文件
  build  - 清理构建产物
`,
	Arguments: []gcmd.Argument{
		{
			Name:  "type",
			Short: "t",
			Brief: "清理类型",
		},
	},
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		cleanType := parser.GetArg(1).String()
		if cleanType == "" {
			cleanType = "all"
		}

		g.Log().Infof(ctx, "开始清理 %s 类型的文件", cleanType)

		rootDir, err := os.Getwd()
		if err != nil {
			g.Log().Error(ctx, "获取当前目录失败:", err)
			return err
		}

		// 定义需要清理的目录
		var dirsToClean []string

		switch strings.ToLower(cleanType) {
		case "all":
			dirsToClean = []string{
				filepath.Join(rootDir, "temp"),
				filepath.Join(rootDir, "data", "cache"),
				filepath.Join(rootDir, "build"),
			}
		case "temp":
			dirsToClean = []string{
				filepath.Join(rootDir, "temp"),
			}
		case "cache":
			dirsToClean = []string{
				filepath.Join(rootDir, "data", "cache"),
			}
		case "build":
			dirsToClean = []string{
				filepath.Join(rootDir, "build"),
			}
		default:
			g.Log().Error(ctx, "不支持的清理类型:", cleanType)
			return fmt.Errorf("不支持的清理类型: %s", cleanType)
		}

		for _, dir := range dirsToClean {
			if gfile.Exists(dir) {
				g.Log().Infof(ctx, "正在清理目录: %s", dir)
				if err := gfile.Remove(dir); err != nil {
					g.Log().Warningf(ctx, "清理目录 %s 失败: %v", dir, err)
				}
			}
		}

		g.Log().Info(ctx, "清理完成")
		return nil
	},
}

定义clean子命令

View Source
var UtilsPack = &gcmd.Command{
	Name:  "pack",
	Usage: "utils pack SOURCE TARGET",
	Brief: "打包资源文件",
	Description: `
将指定的源目录或文件打包到目标Go文件中。
示例:
  vgo utils pack ./public ./internal/packed/public.go
  vgo utils pack ./modules/base/resource/migrates ./modules/base/packed/migrates.go
`,
	Arguments: []gcmd.Argument{
		{
			Name:  "source",
			Short: "s",
			Brief: "源目录或文件路径",
		},
		{
			Name:  "target",
			Short: "t",
			Brief: "目标Go文件路径",
		},
	},
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		source := parser.GetArg(1).String()
		target := parser.GetArg(2).String()

		if source == "" || target == "" {
			g.Log().Error(ctx, "源路径和目标路径不能为空")
			return fmt.Errorf("源路径和目标路径不能为空")
		}

		if !gfile.Exists(source) {
			g.Log().Errorf(ctx, "源路径 %s 不存在", source)
			return fmt.Errorf("源路径 %s 不存在", source)
		}

		targetDir := filepath.Dir(target)
		if !gfile.Exists(targetDir) {
			if err := gfile.Mkdir(targetDir); err != nil {
				g.Log().Errorf(ctx, "创建目标目录失败: %v", err)
				return err
			}
		}

		g.Log().Infof(ctx, "正在打包 %s 到 %s", source, target)

		packCmd := fmt.Sprintf("gf pack %s %s -y", source, target)
		result, err := gproc.ShellExec(ctx, packCmd)
		if err != nil {
			g.Log().Errorf(ctx, "打包失败: %v", err)
			return err
		}

		g.Log().Info(ctx, result)
		g.Log().Info(ctx, "打包完成")
		return nil
	},
}

定义pack子命令

View Source
var UtilsScan = &gcmd.Command{
	Name:  "scan",
	Usage: "utils scan [PATH]",
	Brief: "扫描项目结构",
	Description: `
扫描并显示项目结构,可用于检查模块组织和依赖关系。
示例:
  vgo utils scan            # 扫描整个项目
  vgo utils scan ./modules  # 只扫描模块目录
`,
	Arguments: []gcmd.Argument{
		{
			Name:  "path",
			Short: "p",
			Brief: "要扫描的路径",
		},
	},
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		scanPath := parser.GetArg(1).String()
		if scanPath == "" {
			scanPath = "."
		}

		g.Log().Infof(ctx, "开始扫描路径: %s", scanPath)

		absPath, err := filepath.Abs(scanPath)
		if err != nil {
			g.Log().Error(ctx, "获取绝对路径失败:", err)
			return err
		}

		if !gfile.Exists(absPath) {
			g.Log().Errorf(ctx, "路径不存在: %s", absPath)
			return fmt.Errorf("路径不存在: %s", absPath)
		}

		result := ""
		err = filepath.Walk(absPath, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}

			if strings.HasPrefix(filepath.Base(path), ".") {
				if info.IsDir() {
					return filepath.SkipDir
				}
				return nil
			}

			relPath, err := filepath.Rel(absPath, path)
			if err != nil {
				return err
			}

			if relPath == "." {
				return nil
			}

			indent := strings.Repeat("  ", len(strings.Split(relPath, string(filepath.Separator)))-1)

			if info.IsDir() {
				result += fmt.Sprintf("%s📁 %s\n", indent, filepath.Base(path))
			} else {
				result += fmt.Sprintf("%s📄 %s\n", indent, filepath.Base(path))
			}

			return nil
		})

		if err != nil {
			g.Log().Error(ctx, "扫描失败:", err)
			return err
		}

		fmt.Println(result)
		g.Log().Info(ctx, "扫描完成")
		return nil
	},
}

定义scan子命令

Functions

func MigrateFunc added in v1.1.0

func MigrateFunc(ctx context.Context, parser *gcmd.Parser) error

MigrateFunc 数据库迁移命令处理函数 参数: ctx - 上下文对象, parser - 命令行解析器 返回值: error - 错误信息

Types

type CommandProvider added in v1.1.0

type CommandProvider interface {
	// GetCommands 获取模块提供的命令列表
	// 返回: 命令列表
	GetCommands() []*gcmd.Command

	// GetModuleName 获取模块名称
	// 返回: 模块名称
	GetModuleName() string
}

CommandProvider 命令提供者接口 模块需要实现此接口来提供命令

type CommandRegistry added in v1.1.0

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

CommandRegistry 命令注册中心 负责管理和注册所有模块的命令

func GetRegistry added in v1.1.0

func GetRegistry() *CommandRegistry

GetRegistry 获取全局命令注册中心实例 使用单例模式确保全局唯一 返回: 命令注册中心实例

func (*CommandRegistry) AutoDiscoverModules added in v1.1.0

func (r *CommandRegistry) AutoDiscoverModules(modulesPath string)

AutoDiscoverModules 自动发现模块命令 扫描modules目录下的所有模块,尝试自动注册命令 modulesPath: 模块目录路径

func (*CommandRegistry) GetProviderByName added in v1.1.0

func (r *CommandRegistry) GetProviderByName(name string) CommandProvider

GetProviderByName 根据名称获取命令提供者 name: 提供者名称 返回: 命令提供者实例,如果未找到则返回nil

func (*CommandRegistry) GetRegisteredCommands added in v1.1.0

func (r *CommandRegistry) GetRegisteredCommands() map[string]*gcmd.Command

GetRegisteredCommands 获取已注册的命令列表 返回: 命令名称到命令实例的映射

func (*CommandRegistry) ListProviders added in v1.1.0

func (r *CommandRegistry) ListProviders() []CommandProvider

ListProviders 列出所有已注册的命令提供者 返回: 提供者列表

func (*CommandRegistry) PrintRegistryStatus added in v1.1.0

func (r *CommandRegistry) PrintRegistryStatus()

PrintRegistryStatus 打印注册中心状态信息 用于调试和监控命令注册情况

func (*CommandRegistry) RegisterCommands added in v1.1.0

func (r *CommandRegistry) RegisterCommands()

RegisterCommands 注册所有提供者的命令到根命令 遍历所有已注册的命令提供者,将其命令注册到根命令中

func (*CommandRegistry) RegisterProvider added in v1.1.0

func (r *CommandRegistry) RegisterProvider(provider CommandProvider)

RegisterProvider 注册命令提供者 provider: 命令提供者实例

Jump to

Keyboard shortcuts

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