Revel命令行工具源码阅读

Rhino(犀牛) · · 2407 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

这一篇来详细看下revel命令行工具。下面是这个包的几个文件,以及对应的功能说明。

文件名 短描述(Short) 完整描述(Long)
clean.go clean a Revel application’s temp files Clean the Revel web application named by the given import path.
For example:
revel clean github.com/robfig/revel/samples/chat
It removes the app/tmp directory.
new.go create a skeleton Revel application New creates a few files to get a new Revel application running quickly.
It puts all of the files in the given import path, taking the final element in
the path to be the app name.
For example:
revel new import/path/helloworld
package.go package a Revel application (e.g. for deployment) Package the Revel web application named by the given import path.
This allows it to be deployed and run on a machine that lacks a Go installation.
For example:
revel package github.com/robfig/revel/samples/chat
run.go run a Revel application Run the Revel web application named by the given import path.
For example, to run the chat room sample application:
revel run github.com/robfig/revel/samples/chat dev
The run mode is used to select which set of app.conf configuration should
apply and may be used to determine logic in the application itself.
Run mode defaults to “dev”.
You can set a port as an optional third parameter. For
revel run github.com/robfig/revel/samples/chat prod 8080
test.go run all tests from the command-line Run all tests for the Revel app named by the given import path.
For example, to run the booking sample application’s tests:
revel test github.com/robfig/revel/samples/booking dev
The run mode is used to select which set of app.conf configuration should
apply and may be used to determine logic in the application itself.
Run mode defaults to “dev”.

以上几个文件分别对应命令行工具的几个命令,而剩下的几个文件及其作用如下:

文件名 作用
package_run.bat.template windows下运行脚本模板
package_run.sh.template linux下运行脚本模板
rev.go 主函数以及命令解析,运行
util.go 工具函数集合

我们需要先了解rev.go文件中以下的代码片段:

// Cribbed from the genius organization of the "go" command.
type Command struct {
Run func(args []string)
UsageLine, Short, Long string
}

func (cmd *Command) Name() string {
name := cmd.UsageLine
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}

var commands = []*Command{
cmdRun,
cmdNew,
cmdClean,
cmdPackage,
cmdTest,
}

  1. Command结构很简单,Run来用存储命令的处理函数,而其他三个参数则用来存储命令的形式,简单描述以及完整描述。例如:
    var cmdNew = &Command{
    UsageLine: "new [path]",
    Short: "create a skeleton Revel application",
    Long:
    New creates a few files to get a new Revel application running quickly.

    It puts all of the files in the given import path, taking the final element in
    the path to be the app name.

    For example:

    revel new import/path/helloworld
    ,
    }

  2. Name函数从UsageLine读取命令名,例如:run,new等。
  3. commands是一个*Command类型数组,存储了所有命令的*Command,例如:cmdNew,它是在new.go文件中定义,注意该结构中Run变量的值则是在init函数获得,例如:

    func init() {
    cmdNew.Run = newApp
    }
    func newApp(args []string) {

    }

当我们使用类似:revel new myapp的命令时,rev包中的main函数将被调用,并解析其中的参数,接着调用对应命令的Run函数,代码片段如下:

for _, cmd := range commands {
if cmd.Name() == args[0] {
cmd.Run(args[1:])
return
}
}

rev.go中剩余一些细节也比较简单,这里就不逐行解释了。
这一篇就到这里。


有疑问加站长微信联系(非本文作者)

本文来自:Rhino 的博客

感谢作者:Rhino(犀牛)

查看原文:Revel命令行工具源码阅读

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2407 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传