Revel命令行工具源码阅读

这一篇来详细看下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命令行工具源码阅读

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。