Revel命令列工具源碼閱讀

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

這一篇來詳細看下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中剩餘一些細節也比較簡單,這裡就不逐行解釋了。
這一篇就到這裡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.