This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
/
run.go
167 lines (147 loc) · 4.54 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"strconv"
"encoding/json"
"fmt"
"github.com/revel/cmd/harness"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
"os"
)
var cmdRun = &Command{
UsageLine: "run [-m [run mode] -p [port]] [import path] ",
Short: "run a Revel application",
Long: `
Run the Revel web application named by the given import path.
For example, to run the chat room sample application:
revel run -m dev github.com/revel/examples/chat
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 well. For example:
revel run -m prod -p 8080 github.com/revel/examples/chat `,
}
// RunArgs holds revel run parameters
type RunArgs struct {
ImportPath string
Mode string
Port int
}
func init() {
cmdRun.RunWith = runApp
cmdRun.UpdateConfig = updateRunConfig
}
func updateRunConfig(c *model.CommandConfig, args []string) bool {
convertPort := func(value string) int {
if value != "" {
port, err := strconv.Atoi(value)
if err != nil {
utils.Logger.Fatalf("Failed to parse port as integer: %s", c.Run.Port)
}
return port
}
return 0
}
switch len(args) {
case 3:
// Possible combinations
// revel run [import-path] [run-mode] [port]
c.Run.ImportPath = args[0]
c.Run.Mode = args[1]
c.Run.Port = convertPort(args[2])
case 2:
// Possible combinations
// 1. revel run [import-path] [run-mode]
// 2. revel run [import-path] [port]
// 3. revel run [run-mode] [port]
// Check to see if the import path evaluates out to something that may be on a gopath
if runIsImportPath(args[0]) {
// 1st arg is the import path
c.Run.ImportPath = args[0]
if _, err := strconv.Atoi(args[1]); err == nil {
// 2nd arg is the port number
c.Run.Port = convertPort(args[1])
} else {
// 2nd arg is the run mode
c.Run.Mode = args[1]
}
} else {
// 1st arg is the run mode
c.Run.Mode = args[0]
c.Run.Port = convertPort(args[1])
}
case 1:
// Possible combinations
// 1. revel run [import-path]
// 2. revel run [port]
// 3. revel run [run-mode]
if runIsImportPath(args[0]) {
// 1st arg is the import path
c.Run.ImportPath = args[0]
} else if _, err := strconv.Atoi(args[0]); err == nil {
// 1st arg is the port number
c.Run.Port = convertPort(args[0])
} else {
// 1st arg is the run mode
c.Run.Mode = args[0]
}
case 0:
// Attempt to set the import path to the current working director.
if c.Run.ImportPath == "" {
c.Run.ImportPath, _ = os.Getwd()
}
}
c.Index = model.RUN
return true
}
// Returns true if this is an absolute path or a relative gopath
func runIsImportPath(pathToCheck string) bool {
return utils.DirExists(pathToCheck)
}
// Called to run the app
func runApp(c *model.CommandConfig) (err error) {
if c.Run.Mode == "" {
c.Run.Mode = "dev"
}
revel_path, err := model.NewRevelPaths(c.Run.Mode, c.ImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
if err != nil {
return utils.NewBuildIfError(err, "Revel paths")
}
if c.Run.Port > -1 {
revel_path.HTTPPort = c.Run.Port
} else {
c.Run.Port = revel_path.HTTPPort
}
utils.Logger.Infof("Running %s (%s) in %s mode\n", revel_path.AppName, revel_path.ImportPath, revel_path.RunMode)
utils.Logger.Debug("Base path:", "path", revel_path.BasePath)
// If the app is run in "watched" mode, use the harness to run it.
if revel_path.Config.BoolDefault("watch", true) && revel_path.Config.BoolDefault("watch.code", true) {
utils.Logger.Info("Running in watched mode.")
runMode := fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v}`, revel_path.RunMode, c.Verbose)
if c.HistoricMode {
runMode = revel_path.RunMode
}
// **** Never returns.
harness.NewHarness(c, revel_path, runMode, c.Run.NoProxy).Run()
}
// Else, just build and run the app.
utils.Logger.Debug("Running in live build mode.")
app, err := harness.Build(c, revel_path)
if err != nil {
utils.Logger.Errorf("Failed to build app: %s", err)
}
app.Port = revel_path.HTTPPort
var paths []byte
if len(app.PackagePathMap) > 0 {
paths, _ = json.Marshal(app.PackagePathMap)
}
runMode := fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v,"packagePathMap":%s}`, app.Paths.RunMode, c.Verbose, string(paths))
if c.HistoricMode {
runMode = revel_path.RunMode
}
app.Cmd(runMode).Run(c)
return
}