Firmeve
1.0.0
1.0.0
  • Firmeve
  • [核心组件]
    • 应用
    • 容器
    • 配置
    • 提供者
    • 事件
    • 日志
    • 命令
  • [基础组件]
    • Http服务
    • 数据验证
    • 数据库
    • 缓存
    • JWT
    • Redis
    • 辅助函数
    • 队列
    • 计划任务
Powered by GitBook
On this page
  • 简介
  • 基础示例
  • 创建命令
  • 命令注册
  • 内置命令
  • 更多用法

Was this helpful?

  1. [核心组件]

命令

简介

Firmeve命令基于cobra,在其基础之上提供了一系内置可用命令,以及灵活的命令扩展。命令是Firmeve运行的最初入口,Firmeve是通过多命令运行。

基础示例

创建命令

命令的创建必须实现contract.Command接口

Command interface {
  // 返回一个新的 cobra command
  CobraCmd() *cobra.Command

  // 业务主入口
  Run(root BaseCommand, cmd *cobra.Command, args []string)
}

以下是一个基础示例:

import (
    "fmt"
    "github.com/firmeve/firmeve"
    "github.com/firmeve/firmeve/kernel/contract"
    "github.com/spf13/cobra"
)

type TestCommand struct {
}

func (t TestCommand) CobraCmd() *cobra.Command {
    cmd := &cobra.Command{}
    cmd.Use = "testing"
    cmd.Short = "Testing a cmd"
    return cmd
}

func (t TestCommand) Run(root contract.BaseCommand, cmd *cobra.Command, args []string) {
    fmt.Println("run")
}

命令注册

所有的命令都是在main初始化中注册,本质上来说,是所有的子命令会在main中挂载到系统root command中

func main() {
    firmeve.RunDefault(firmeve.WithCommands(
        []contract.Command{
            new(TestCommand),
        },
    ))
}

内置命令

Firmeve提供了一系列内置运行命令,如:http:serve migrate等,具体可执行go run main.go -c config.yaml查看

更多用法

Previous日志Next[基础组件]

Last updated 4 years ago

Was this helpful?

更多用法,请参见示例

关于更多命令开发方法请参考

example/command
cobra