mirror of
https://github.com/kevin-DL/revel-cmd.git
synced 2026-01-11 18:54:31 +00:00
revel/revel#389 run cmd become more robust
This commit is contained in:
94
revel/run.go
94
revel/run.go
@@ -6,6 +6,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/revel/cmd/harness"
|
"github.com/revel/cmd/harness"
|
||||||
"github.com/revel/revel"
|
"github.com/revel/revel"
|
||||||
@@ -31,41 +32,90 @@ You can set a port as an optional third parameter. For example:
|
|||||||
revel run github.com/revel/samples/chat prod 8080`,
|
revel run github.com/revel/samples/chat prod 8080`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunArgs holds revel run parameters
|
||||||
|
type RunArgs struct {
|
||||||
|
ImportPath string
|
||||||
|
Mode string
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdRun.Run = runApp
|
cmdRun.Run = runApp
|
||||||
}
|
}
|
||||||
|
|
||||||
func runApp(args []string) {
|
func parseRunArgs(args []string) *RunArgs {
|
||||||
if len(args) == 0 {
|
inputArgs := RunArgs{
|
||||||
errorf("No import path given.\nRun 'revel help run' for usage.\n")
|
ImportPath: importPathFromCurrentDir(),
|
||||||
|
Mode: DefaultRunMode,
|
||||||
|
Port: revel.HTTPPort,
|
||||||
}
|
}
|
||||||
|
switch len(args) {
|
||||||
// Determine the run mode.
|
case 3:
|
||||||
mode := DefaultRunMode
|
// Possibile combinations
|
||||||
if len(args) >= 2 {
|
// revel run [import-path] [run-mode] [port]
|
||||||
mode = args[1]
|
port, err := strconv.Atoi(args[2])
|
||||||
}
|
if err != nil {
|
||||||
|
|
||||||
// Find and parse app.conf
|
|
||||||
revel.Init(mode, args[0], "")
|
|
||||||
revel.LoadMimeConfig()
|
|
||||||
|
|
||||||
// Determine the override port, if any.
|
|
||||||
port := revel.HTTPPort
|
|
||||||
if len(args) == 3 {
|
|
||||||
var err error
|
|
||||||
if port, err = strconv.Atoi(args[2]); err != nil {
|
|
||||||
errorf("Failed to parse port as integer: %s", args[2])
|
errorf("Failed to parse port as integer: %s", args[2])
|
||||||
}
|
}
|
||||||
|
inputArgs.ImportPath = args[0]
|
||||||
|
inputArgs.Mode = args[1]
|
||||||
|
inputArgs.Port = port
|
||||||
|
case 2:
|
||||||
|
// Possibile combinations
|
||||||
|
// 1. revel run [import-path] [run-mode]
|
||||||
|
// 2. revel run [import-path] [port]
|
||||||
|
// 3. revel run [run-mode] [port]
|
||||||
|
if strings.Contains(args[0], "/") {
|
||||||
|
inputArgs.ImportPath = args[0]
|
||||||
|
if port, err := strconv.Atoi(args[1]); err == nil {
|
||||||
|
inputArgs.Port = port
|
||||||
|
} else {
|
||||||
|
inputArgs.Mode = args[1]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
port, err := strconv.Atoi(args[1])
|
||||||
|
if err != nil {
|
||||||
|
errorf("Failed to parse port as integer: %s", args[1])
|
||||||
|
}
|
||||||
|
inputArgs.Mode = args[0]
|
||||||
|
inputArgs.Port = port
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
// Possibile combinations
|
||||||
|
// 1. revel run [import-path]
|
||||||
|
// 2. revel run [port]
|
||||||
|
// 3. revel run [run-mode]
|
||||||
|
if strings.Contains(args[0], "/") {
|
||||||
|
inputArgs.ImportPath = args[0]
|
||||||
|
} else if port, err := strconv.Atoi(args[0]); err == nil {
|
||||||
|
inputArgs.Port = port
|
||||||
|
} else {
|
||||||
|
inputArgs.Mode = args[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
revel.INFO.Printf("Running %s (%s) in %s mode\n", revel.AppName, revel.ImportPath, mode)
|
return &inputArgs
|
||||||
|
}
|
||||||
|
|
||||||
|
func runApp(args []string) {
|
||||||
|
runArgs := parseRunArgs(args)
|
||||||
|
|
||||||
|
// Find and parse app.conf
|
||||||
|
revel.Init(runArgs.Mode, runArgs.ImportPath, "")
|
||||||
|
revel.LoadMimeConfig()
|
||||||
|
|
||||||
|
// fallback to default port
|
||||||
|
if runArgs.Port == 0 {
|
||||||
|
runArgs.Port = revel.HTTPPort
|
||||||
|
}
|
||||||
|
|
||||||
|
revel.INFO.Printf("Running %s (%s) in %s mode\n", revel.AppName, revel.ImportPath, runArgs.Mode)
|
||||||
revel.TRACE.Println("Base path:", revel.BasePath)
|
revel.TRACE.Println("Base path:", revel.BasePath)
|
||||||
|
|
||||||
// If the app is run in "watched" mode, use the harness to run it.
|
// If the app is run in "watched" mode, use the harness to run it.
|
||||||
if revel.Config.BoolDefault("watch", true) && revel.Config.BoolDefault("watch.code", true) {
|
if revel.Config.BoolDefault("watch", true) && revel.Config.BoolDefault("watch.code", true) {
|
||||||
revel.TRACE.Println("Running in watched mode.")
|
revel.TRACE.Println("Running in watched mode.")
|
||||||
revel.HTTPPort = port
|
revel.HTTPPort = runArgs.Port
|
||||||
harness.NewHarness().Run() // Never returns.
|
harness.NewHarness().Run() // Never returns.
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +125,6 @@ func runApp(args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
errorf("Failed to build app: %s", err)
|
errorf("Failed to build app: %s", err)
|
||||||
}
|
}
|
||||||
app.Port = port
|
app.Port = runArgs.Port
|
||||||
app.Cmd().Run()
|
app.Cmd().Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"archive/tar"
|
"archive/tar"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go/build"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -167,3 +168,9 @@ func empty(dirname string) bool {
|
|||||||
results, _ := dir.Readdir(1)
|
results, _ := dir.Readdir(1)
|
||||||
return len(results) == 0
|
return len(results) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func importPathFromCurrentDir() string {
|
||||||
|
pwd, _ := os.Getwd()
|
||||||
|
importPath, _ := filepath.Rel(filepath.Join(build.Default.GOPATH, "src"), pwd)
|
||||||
|
return filepath.ToSlash(importPath)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user