Compare commits

..

110 Commits

Author SHA1 Message Date
Steve
e2480171e5 Merge pull request #214 from revel/master
v1.1.1 hotfix sync
2022-04-12 15:20:35 -07:00
Brenden Soares
d83d2d5891 Merge pull request #213 from revel/hotfix/v1.1.0
Fixed run mode for verbose
2022-04-12 15:19:24 -07:00
notzippy@gmail.com
55b736e588 Fixed run mode for verbose 2022-04-12 15:08:06 -07:00
Brenden Soares
8162ef3ed6 develop v1.2.0-dev 2022-04-12 13:37:12 -07:00
Brenden Soares
0ec9e69d97 Merge pull request #212 from revel/build_process_update
Updated verbose flag
2022-04-12 08:40:27 -07:00
notzippy@gmail.com
1cdd318d9c Updated verbose flag 2022-04-12 08:30:29 -07:00
Brenden Soares
da2a81d035 release v1.1.0 2022-04-11 23:57:40 -07:00
Brenden Soares
86b4670a2f Update README.md 2022-04-11 20:31:59 -07:00
Brenden Soares
bc376fbee0 Merge pull request #211 from lujiacn/develop
updated golang.org/x/tools, to avoid internal error: package xxx with…
2022-04-11 20:30:13 -07:00
Brenden Soares
5c8ac53937 Merge branch 'develop' into develop 2022-04-11 20:30:05 -07:00
Brenden Soares
c67408482e Merge pull request #201 from ptman/lint
More linting
2022-04-11 20:19:06 -07:00
Brenden Soares
cfe1d9718f Merge branch 'develop' into lint 2022-04-11 20:18:54 -07:00
Brenden Soares
7f9f658392 Merge pull request #209 from glaslos/patch-1
Fixing typo
2022-04-11 20:05:38 -07:00
Brenden Soares
126d20c873 Merge pull request #210 from revel/build_process_update
Updated Launch code
2022-04-11 20:03:26 -07:00
Jia Lu
111264cfa0 updated go.mod 2022-03-25 14:46:52 +08:00
Jia Lu
4087c49b9e updated golang.org/x/tools, to avoid internal error: package xxx without types was imported from ... 2022-03-25 10:34:03 +08:00
Steve
3602eb4ea7 Merge branch 'develop' into patch-1 2022-03-06 09:01:17 -08:00
Steve
192fc6669a Merge pull request #200 from julidau/develop
harness: interrupt process on windows as well
2022-03-06 08:58:38 -08:00
Steve
5689f8679b Merge pull request #204 from shinypb/master
Get rid of redundant space in the output of `revel new -a`
2022-03-06 08:55:23 -08:00
notzippy@gmail.com
6dba0c3d2d Fix bad error syntax
An wrapped error message in the cmd module was referencing the wrong parameter value to be built
closes revel/revel#1532
2022-03-06 07:45:11 -08:00
notzippy@gmail.com
bb926f396a Added additional pattern to test against
Another different missing pacakge error thrown that can be detected and added
This error occurs because a package may have been stripped down when originally loaded
2022-03-05 09:10:51 -08:00
notzippy@gmail.com
3cd5ebbde2 Updated launch scripts 2022-03-04 17:21:29 -08:00
notzippy@gmail.com
25dc05b31e Updated Launch code
Added output to error stack, so terminal errors are displayed
Ficed c.Verbose, it was changed to an array which causes issues launching
Removed . notation from doing anything special. This was already replaced with the -p CLI option
Added documentaiton on adding the package name
Started watcher with force refresh.
2022-02-28 20:01:01 -08:00
Brenden Soares
0a40a2048e Merge pull request #208 from notzippy/build_process_update
Fixed building
2022-02-23 09:44:18 -08:00
Lukas Rist
fcc1319245 Fixing type 2022-02-23 14:55:14 +01:00
notzippy@gmail.com
ea5acb720f Updated shared build environments
Updated check for errors.
Updated go.mod
Added .vscode launch
2022-02-20 17:16:12 -08:00
Mark Christian
25d6352bc5 Get rid of redundant space in the output of revel new -a 2021-08-15 14:29:07 -07:00
Paul Tötterman
ddec572d5d More linting 2021-02-10 16:34:20 +02:00
Julian Daube
7a91d0ca0b interrupt process on windows as well
On Windows, the killing of running services relies on the
60 second kill timeout, making it faster to restart the harness
to force rebuilding.

Since os.Interrupt works for me, remove the runtime os check in
harness/app.go completely.
2021-02-07 00:30:16 +01:00
Steve
b562bd2dc5 Merge pull request #199 from ptman/lint
Lint fixes
2020-10-20 06:29:15 -07:00
Steve
bf17a71166 Merge branch 'develop' into lint 2020-10-19 20:34:02 -07:00
Paul Tötterman
3d924a016b Lint fixes 2020-10-19 13:40:52 +03:00
Steve
3cec19ee62 Merge pull request #187 from Laur1nMartins/master
Fixed parsing of user defined linker flags
2020-07-21 09:12:17 -07:00
Steve
236499f9e5 Merge pull request #192 from revel/hotfix-1
Added local import map to getControllerFunc lookup
2020-07-14 22:10:25 -07:00
notzippy@gmail.com
42e0e3bf2b Added local import map to getControllerFunc lookup 2020-07-14 21:46:37 -07:00
Steve
b0484c9279 Merge pull request #188 from revel/hotfix-1
Fixed issues with test cases and launching app using the run mode
2020-07-14 13:16:12 -07:00
notzippy@gmail.com
c7f4307a5d Changed the local code walker to include all go files in the base path, and not just the app path 2020-07-14 12:45:10 -07:00
notzippy@gmail.com
ebc9c73ba0 Modified run command so launching command would be properly treated 2020-07-13 22:35:22 -07:00
Steve
2e2f22ad7d remove go 1.12 test 2020-07-13 09:40:56 -07:00
Steve
357c382d96 Version compatible Check
Increase version compatibility to 1.0 - 1.9
2020-07-13 09:39:57 -07:00
Laurin
6d4ae81af9 Fixed parsing of user defined linker flags 2020-07-12 11:09:57 +02:00
notzippy@gmail.com
d64c7f164f develop v1.1.0-dev 2020-07-11 22:57:37 -07:00
notzippy@gmail.com
6ecc0a7c0a release v1.0.0 2020-07-11 22:57:36 -07:00
Steve
d8117a33d3 Merge pull request #186 from notzippy/go-mod
Removed version update from Revel
2020-06-06 07:50:58 -07:00
notzippy@gmail.com
6371373eb5 Removed version update
Version control is maintained through go.mod file
Modified harness to only kill the application if not responded after 60 seconds in windows
2020-06-06 07:49:10 -07:00
Steve
28ac65f1c1 Merge pull request #185 from notzippy/go-mod
Go mod updates
2020-05-19 02:42:57 -07:00
notzippy@gmail.com
5070fb8be0 Fixed issue with new and run flag
Updated tests to run final test in non gopath, with new name
2020-05-19 02:23:18 -07:00
notzippy@gmail.com
904cfa2995 Added some informational messages while download 2020-05-18 12:43:00 -07:00
notzippy@gmail.com
223bd3b7c0 Added manual scan on packages in app folder
This allows for source code generation. Packages in <application>/app folder are scanned manually as opposed to the `packages.Load` scan which will fast fail on compile error, and leave you with go files with no syntax.
2020-05-18 11:47:01 -07:00
notzippy@gmail.com
4987ee8319 Added verbose logging to building / testing a no-vendor app
Removed section which raises an error when examining packages, we dont need to check for errors on foreign packages since we are importing only a slice of the data
2020-05-17 05:58:28 -07:00
notzippy@gmail.com
4bab4409b9 Updated Revel command
Added a check to see if harness had already started, saves a recompile on load
Added check to source info for local import renames
Removed the go/build check for path and just check existence of the path
Formatting updates
2020-05-13 22:26:05 -07:00
notzippy@gmail.com
741f49236a Updated scanner
Removed scanning all the import statements, this is not needed
Added recursive scan for revel import path to pick up testunits
2020-05-08 15:41:20 -07:00
Steve
60b88a42c9 Merge pull request #180 from notzippy/go-mod
Initial commit to go mod
2020-05-03 21:47:09 -07:00
notzippy@gmail.com
49eef29bb5 Build and Historic build updates
Modified GOPATH to not modify build with go.mod
Updated go.mod to version 1.12
Updated harness to setup listener before killing process
Updated notvendored flag to --no-vendor
Updated command_config to ensure no-vendor can be build
Added additional checks in source path lookup
2020-05-03 13:39:48 -07:00
notzippy@gmail.com
9d3a554bec Updates
Updated NotVendored flag
Updated travis matrix
Updated build log
2020-04-29 22:01:28 -07:00
notzippy@gmail.com
36bd6b944a Corrected flags 2020-04-29 21:48:11 -07:00
notzippy@gmail.com
1d9df256a0 Moved test cases to run last 2020-04-29 21:39:19 -07:00
notzippy@gmail.com
ad694c0fb0 Debug travis 2020-04-29 21:32:12 -07:00
notzippy@gmail.com
fb4b56513a Debug travis
Added verbose flag so we can see what is occurring,
Removed checkout for revel, not needed anymore
2020-04-29 21:13:00 -07:00
notzippy@gmail.com
20d5766eb6 Added gomod-flags
Added a gomod-flags parameter which allows you to run go mod commands on the go.mod file before the build is performed. This allows for development environments.
2020-04-29 17:05:39 -07:00
notzippy@gmail.com
0920905a0c Updated to build go 1.12 and up
Modified to use fsnotify directlyUpdated travis to not use go deps
2020-04-28 12:56:23 -07:00
notzippy@gmail.com
31cb64e496 Check-in of command_test,
remaps the go mod command to use the develop branch.
2020-04-27 09:07:04 -07:00
notzippy@gmail.com
33abc47c7a Fixed remaining test 2020-04-26 23:00:51 -07:00
notzippy@gmail.com
86736d6e43 Updated formating
Ran through testing individually for vendored Revel applications
2020-04-26 22:29:16 -07:00
notzippy@gmail.com
07d67846c1 Restructured command config
Removed go/build reference in clean
2020-04-26 22:28:46 -07:00
notzippy@gmail.com
c1aee24445 Corrected version detection, so that equal versions match 2020-04-26 22:28:46 -07:00
notzippy@gmail.com
f2b54f5a69 Updated sourceinfo
Added packagepathmap to the SourceInfo, this in turn allows the RevelCLI app command to pass the source paths directly to Revel directly
Added default to build to be "target" of the current folder
Renamed source processor
2020-04-26 22:28:46 -07:00
notzippy@gmail.com
3f54665d4e Added processor to read the functions in the imported files, and populate the SourceInfo object the same as before 2020-04-26 22:28:46 -07:00
notzippy@gmail.com
548cbc1764 Upatede Error type to SourceError
Added processor object to code
Verified compile errors appearing
Signed-off-by: notzippy@gmail.com
2020-04-26 22:28:46 -07:00
notzippy@gmail.com
9a9511d28f Updated so revel new works and revel run starts parsing the source. 2020-04-26 22:28:46 -07:00
notzippy@gmail.com
acb8fb631b Initial commit to go mod 2020-04-26 22:28:46 -07:00
Steve
d2014633af Merge pull request #176 from xXLokerXx/fix_windows_path
acept slash and inverted slash in src path validation
2020-04-13 07:32:55 -07:00
Steve
773f6889b4 Merge branch 'develop' into fix_windows_path 2020-04-13 07:32:03 -07:00
Steve
ca4cfa567e Merge pull request #165 from kumakichi/fixed_import_C
fixed import "C"
2020-04-13 07:30:30 -07:00
Steve
436869049c Merge pull request #179 from Laur1nMartins/Laur1nMartins/fix-linkerFlags
Fix linker flags inclusion in build command
2020-04-13 07:29:34 -07:00
Steve
cf2e617618 Merge branch 'develop' into Laur1nMartins/fix-linkerFlags 2020-04-13 07:29:15 -07:00
Laurin
424474a035 Fix linker flags inclusion in build comamnd 2020-02-10 10:35:43 +01:00
Steve
531aa1e209 Merge pull request #178 from helgix/package-bugfix
eliminates package bug on silent build problems
2019-09-30 16:54:18 -07:00
Олег Вакарев
83dfdb8ad2 this fixes error when revel package creates tar without indicating that build had errors due to unhandled error 2019-09-26 13:14:33 +03:00
xXlokerXx
6d8fcd90c1 Fix sintax error 2019-07-08 19:48:09 -05:00
xXlokerXx
aa459c1b66 Fix sintaxis error 2019-07-08 19:19:33 -05:00
xXlokerXx
0b23b3e494 Fix complexity 2019-07-08 18:38:10 -05:00
xXlokerXx
3f65e1ef41 acept slash and inverted slash in src path validation 2019-07-08 18:23:58 -05:00
san
7dce3d8967 fixed import "C" 2018-11-03 09:12:25 +08:00
Steve
149f9f992b Merge pull request #161 from notzippy/master
Patch for windows
2018-11-01 20:31:14 -07:00
NotZippy
dfb08d9bd2 Amended importPathFromPath to detect vendor folder from basePath, not just the word vendor in the path 2018-11-01 09:06:31 -07:00
NotZippy
98e771cd01 Patch for windows
Made interrupt call os.Kill for windows
Added check for process still running before killing it
2018-10-31 12:52:43 -07:00
NotZippy
5c8d5bca7f develop v1.0.0-dev 2018-10-30 06:23:53 -07:00
NotZippy
5f558aca4e release v0.21.0 2018-10-30 06:23:52 -07:00
Steve
facfe0ecaf Merge pull request #159 from notzippy/develop
Patchset for 21
2018-10-29 16:29:03 -07:00
NotZippy
ee53d2f399 Patchset for 21
Added Version -u to update the checked out libaraies
Enhanced new skeleton toto support http https and git schemas when cloning the skeleton repo.
Enhanced version command to fetch server version from master branch
Enhanced version command to update local repository if a new version exists on the server
2018-10-29 15:26:59 -07:00
Steve
3c48e1f83e Merge pull request #158 from hwsoderlund/fix_compile_error
Fix compilation error visibility
2018-10-29 10:19:17 -07:00
Henrik Söderlund
19fb7d6776 Fix compilation error visibility 2018-10-29 16:55:59 +01:00
Steve
1e7b5322f5 Merge pull request #157 from HaraldNordgren/go_versions
Bump Go versions and use '.x' to always get latest patch versions
2018-10-28 11:08:56 -07:00
Harald Nordgren
205c652f07 Bump Go versions and use '.x' to always get latest patch versions 2018-10-28 18:42:35 +01:00
Steve
3de8b8c03c Merge pull request #156 from notzippy/develop
Modified run command to translate symlinks to absolute paths
2018-10-25 09:19:43 -07:00
NotZippy
4a877b2a8a Modified run command to translate symlinks to absolute paths 2018-10-24 21:29:07 -07:00
Steve
a0bafdc2a7 Merge pull request #155 from notzippy/develop
Added ability to readback output from command line
2018-10-24 10:26:16 -07:00
NotZippy
cdef0b75a8 Added ability to readback output from command line
Updated readme
2018-10-24 10:23:09 -07:00
Steve
e0d3f83ca8 Merge pull request #153 from notzippy/develop
Updated tool to give more meaningful messages
2018-10-19 09:12:48 -07:00
NotZippy
87c9e56322 Tool updates
Updated tool to give more meaningful errors
Added file system as an option to fetch the skeleton from
2018-10-19 06:45:01 -07:00
Steve
554e62574d Merge pull request #152 from notzippy/develop
Allow windows to fail on travis since to address a bug on current master
2018-10-18 08:05:51 -07:00
NotZippy
32a3f08dde Allow windows to fail on travis since to address a bug on current master 2018-10-18 08:05:15 -07:00
Steve
e6e1cad795 Merge pull request #148 from notzippy/develop
Shutdown / logger update
2018-10-12 21:20:05 -07:00
NotZippy
5e36cb1025 Updated travis to use checkout matching branch of Revel Framework for build. 2018-10-12 20:50:42 -07:00
NotZippy
8c21a56302 Revel tool enhancements
* run Command will choose CWD if no additional arguments are supplied
* Added Revel version check, compatible lists are in model/version
2018-10-12 20:40:48 -07:00
NotZippy
031fde6009 Update to logger 2018-10-12 20:40:48 -07:00
Steve
09ca80add8 Merge pull request #151 from lujiacn/master
Update build.go
2018-10-12 15:48:22 -07:00
Jia Lu
2d6c2eefa4 Update build.go
add missing c.Build.ImportPath, which is required to generate run.sh and run.bat
2018-10-12 22:53:01 +08:00
Steve
644d6e12bd Added check for copy dir
CopyDir should not fail if the source folder does not exist.
2018-10-10 11:40:29 -07:00
77 changed files with 4112 additions and 2507 deletions

View File

@@ -1,11 +1,13 @@
{ {
"GOLANG": { "GOLANG": {
"ABC":[25, 35, 50, 70], "ABC":[33, 38, 50, 70],
"BLOCK_NESTING":[5, 6, 7, 8], "ARITY":[5,6,7,8],
"CYCLO":[20, 30, 45, 60], "BLOCK_NESTING":[9, 10, 12, 13],
"TOO_MANY_IVARS": [15, 18, 20, 25], "CYCLO":[30, 35, 45, 60],
"TOO_MANY_IVARS": [28, 30, 40, 45],
"TOO_MANY_FUNCTIONS": [20, 30, 40, 50], "TOO_MANY_FUNCTIONS": [20, 30, 40, 50],
"TOTAL_COMPLEXITY": [150, 250, 400, 500], "TOTAL_COMPLEXITY": [150, 250, 400, 500],
"LOC": [50, 75, 90, 120] "LOC": [100, 175, 250, 320],
"TOTAL_LOC": [300, 400, 500, 600]
} }
} }

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
.idea/ .idea/
*.iml *.iml
.temp/

View File

@@ -1,10 +1,8 @@
language: go language: go
go: go:
- "1.8.7" - "1.13.x"
- "1.9" - "1.14.x"
- "1.10"
- "1.11"
- "tip" - "tip"
os: os:
@@ -19,48 +17,56 @@ branches:
- master - master
- develop - develop
env:
# Setting environments variables
- GO111MODULE=on
install: install:
# Setting environments variables
- export PATH=$PATH:$HOME/gopath/bin - export PATH=$PATH:$HOME/gopath/bin
- export REVEL_BRANCH="develop" - export REVEL_BRANCH="develop"
- 'if [[ "$TRAVIS_BRANCH" == "master" ]]; then export REVEL_BRANCH="master"; fi' - 'if [[ "$TRAVIS_BRANCH" == "master" ]]; then export REVEL_BRANCH="master"; fi'
- 'echo "Travis branch: $TRAVIS_BRANCH, Revel dependency branch: $REVEL_BRANCH"' - 'echo "Travis branch: $TRAVIS_BRANCH, Revel dependency branch: $REVEL_BRANCH"'
- go get -t -v github.com/revel/cmd/revel # Since travis already checks out go build the commandline tool (revel)
- go get -u github.com/golang/dep/cmd/dep - mkdir $HOME/GOPATH_PROTECTED
- echo $GOPATH - export GOPATH=$HOME/GOPATH_PROTECTED
- echo $PATH - go build -o $HOME/gopath/bin/revel github.com/revel/cmd/revel
- pwd - pwd
- env
script: script:
- go test -v github.com/revel/cmd/revel/... - go test -v github.com/revel/cmd/revel/...
# Ensure the new-app flow works (plus the other commands). # Ensure the new-app flow works (plus the other commands).
- revel version #- revel version
- revel new my/testapp #- revel new my/testapp
- revel test my/testapp #- revel test my/testapp
- revel clean my/testapp #- revel clean my/testapp
- revel build my/testapp build/testapp #- revel build my/testapp build/testapp
- revel build my/testapp build/testapp prod #- revel build my/testapp build/testapp prod
- revel package my/testapp #- revel package my/testapp
- revel package my/testapp prod #- revel package my/testapp prod
# Ensure the new-app flow works (plus the other commands). # Ensure the new-app flow works (plus the other commands).
- revel new -a my/testapp2 - revel new --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 --package revelframework.com -v
- revel test -a my/testapp2 - revel test --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v
- revel clean -a my/testapp2 - revel clean --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v
- revel build -a my/testapp2 -t build/testapp2 - revel build --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v -t build/testapp2
- revel build -a my/testapp2 -t build/testapp2 -m prod - revel build --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v -t build/testapp2 -m prod
- revel package -a my/testapp2 - revel package --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v
- revel package -a my/testapp2 -m prod - revel package --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v -m prod
- revel new -v -a my/testapp3 -V - export INITIALWD=$PWD
- revel test -v -a my/testapp3 # Check build works with no-vendor flag
- revel clean -v -a my/testapp3 - cd $GOPATH
- revel build -a my/testapp3 -t build/testapp3 - export GO111MODULE=auto
- revel build -a my/testapp3 -t build/testapp3 -m prod - revel new -a my/testapp2 --no-vendor -v
- revel package -a my/testapp3 - revel test -a my/testapp2 -v
- revel package -a my/testapp3 -m prod
# Check non verbose build, outside of GO path
- cd $INITIALWD
- revel new --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp3 --package revelframework.com
- revel test --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp3
matrix: matrix:
allow_failures: allow_failures:
- go: tip - go: tip
- os: windows

50
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,50 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Help",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceRoot}/revel",
"args": []
},
{
"name": "Create new",
"type": "go",
"request": "launch",
"preLaunchTask": "Clean-Test-Project",
"mode": "auto",
"program": "${workspaceRoot}/revel",
"args": ["new", "-v","-a", "${workspaceRoot}/.temp/revel/reveltest", "-p","revel.com/testproject"],
"env": {
"GOPATH": "${workspaceRoot}/.temp/revel/GOPATH"
},
},
{
"name": "Run program",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceRoot}/revel",
"args": ["run","-v", "-v","-a", "${workspaceRoot}/.temp/revel/reveltest"],
"env": {
"GOPATH": "${workspaceRoot}/.temp/revel/GOPATH"
},
},
{
"name": "Run program Directly",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceRoot}/.temp/revel/reveltest/app/tmp/main.go",
"args": ["-port=9000","-importPath=revel.com/testproject/reveltest", "-runMode={\"mode\":\"dev\", \"specialUseFlag\":true,\"packagePathMap\":{\"github.com/revel/modules/static\":\"/home/notzippy/go/pkg/mod/github.com/revel/modules@v1.0.0/static\",\"github.com/revel/modules/testrunner\":\"/home/notzippy/go/pkg/mod/github.com/revel/modules@v1.0.0/testrunner\",\"github.com/revel/revel\":\"/home/notzippy/go/pkg/mod/github.com/revel/revel@v1.0.0\",\"revel.com/testproject/reveltest\":\"/mnt/DevSystem/Work/Workareas/revel/revel3/cmd/.temp/revel/reveltest\"}}"],
"env": {
"GOPATH": "${workspaceRoot}/.temp/revel/GOPATH"
},
}
]
}

1
.vscode/load.sh vendored Executable file
View File

@@ -0,0 +1 @@
http_load -rate 5 -seconds 10 load.web

2
.vscode/load.web vendored Normal file
View File

@@ -0,0 +1,2 @@
http://localhost:9000/
http://localhost:9000/miss

20
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,20 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Clean-Test-Project",
"type": "shell",
"command": "rm -rf ${workspaceRoot}/.temp/revel/testproject"
},
{
"label": "Update Go Mod",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/.temp/revel/testproject"
},
"command": "go mod tidy && go mod edit -replace github.com/revel/revel => ../../../revel"
},
]
}

View File

@@ -11,7 +11,7 @@ Provides the `revel` command, used to create and run Revel apps.
Install Install
------------ ------------
```bash ```bash
go get -u github.com/revel/cmd/revel go install github.com/revel/cmd/revel@latest
``` ```
New Application New Application
@@ -19,5 +19,33 @@ New Application
Create a new application Create a new application
```commandline ```commandline
revel new my/app revel new -a my/app
``` ```
## Community
* [Gitter](https://gitter.im/revel/community)
* [StackOverflow](http://stackoverflow.com/questions/tagged/revel)
## Learn More
* [Manual, Samples, Godocs, etc](http://revel.github.io)
* [Apps using Revel](https://github.com/revel/revel/wiki/Apps-in-the-Wild)
* [Articles Featuring Revel](https://github.com/revel/revel/wiki/Articles)
## Contributing
* [Contributing Code Guidelines](https://github.com/revel/revel/blob/master/CONTRIBUTING.md)
* [Revel Contributors](https://github.com/revel/revel/graphs/contributors)
## Contributors
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/0)](https://sourcerer.io/fame/notzippy/revel/cmd/links/0)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/1)](https://sourcerer.io/fame/notzippy/revel/cmd/links/1)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/2)](https://sourcerer.io/fame/notzippy/revel/cmd/links/2)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/3)](https://sourcerer.io/fame/notzippy/revel/cmd/links/3)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/4)](https://sourcerer.io/fame/notzippy/revel/cmd/links/4)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/5)](https://sourcerer.io/fame/notzippy/revel/cmd/links/5)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/6)](https://sourcerer.io/fame/notzippy/revel/cmd/links/6)
[![](https://sourcerer.io/fame/notzippy/revel/cmd/images/7)](https://sourcerer.io/fame/notzippy/revel/cmd/links/7)

35
go.mod Normal file
View File

@@ -0,0 +1,35 @@
module github.com/revel/cmd
go 1.17
require (
github.com/BurntSushi/toml v1.0.0 // indirect
github.com/agtorre/gocolorize v1.0.0
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/go-stack/stack v1.8.0 // indirect
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac // indirect
github.com/jessevdk/go-flags v1.4.0
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/revel/config v1.0.0
github.com/revel/log15 v2.11.20+incompatible
github.com/revel/pathtree v0.0.0-20140121041023-41257a1839e9 // indirect
github.com/revel/revel v1.0.0
github.com/stretchr/testify v1.7.0
github.com/twinj/uuid v1.0.0 // indirect
github.com/xeonx/timeago v1.0.0-rc4 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/tools v0.1.10
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/stack.v0 v0.0.0-20141108040640-9b43fcefddd0
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

86
go.sum Normal file
View File

@@ -0,0 +1,86 @@
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/agtorre/gocolorize v1.0.0 h1:TvGQd+fAqWQlDjQxSKe//Y6RaxK+RHpEU9X/zPmHW50=
github.com/agtorre/gocolorize v1.0.0/go.mod h1:cH6imfTkHVBRJhSOeSeEZhB4zqEYSq0sXuIyehgZMIY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac h1:n1DqxAo4oWPMvH1+v+DLYlMCecgumhhgnxAPdqDIFHI=
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/myesui/uuid v1.0.0 h1:xCBmH4l5KuvLYc5L7AS7SZg9/jKdIFubM7OVoLqaQUI=
github.com/myesui/uuid v1.0.0/go.mod h1:2CDfNgU0LR8mIdO8vdWd8i9gWWxLlcoIGGpSNgafq84=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/revel/config v1.0.0 h1:UAzLPQ+x9nJeP6a+H93G+AKEosg3OO2oVLBXK9oSN2U=
github.com/revel/config v1.0.0/go.mod h1:GT4a9px5kDGRqLizcw/md0QFErrhen76toz4qS3oIoI=
github.com/revel/log15 v2.11.20+incompatible h1:JkA4tbwIo/UGEMumY50zndKq816RQW3LQ0wIpRc+32U=
github.com/revel/log15 v2.11.20+incompatible/go.mod h1:l0WmLRs+IM1hBl4noJiBc2tZQiOgZyXzS1mdmFt+5Gc=
github.com/revel/pathtree v0.0.0-20140121041023-41257a1839e9 h1:/d6kfjzjyx19ieWqMOXHSTLFuRxLOH15ZubtcAXExKw=
github.com/revel/pathtree v0.0.0-20140121041023-41257a1839e9/go.mod h1:TmlwoRLDvgRjoTe6rbsxIaka/CulzYrgfef7iNJcEWY=
github.com/revel/revel v1.0.0 h1:BsPFnKuuzXEkPtrjdjZHiDcvDmbBiBQvh7Z5c6kLb/Y=
github.com/revel/revel v1.0.0/go.mod h1:VZWJnHjpDEtuGUuZJ2NO42XryitrtwsdVaJxfDeo5yc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/twinj/uuid v1.0.0 h1:fzz7COZnDrXGTAOHGuUGYd6sG+JMq+AoE7+Jlu0przk=
github.com/twinj/uuid v1.0.0/go.mod h1:mMgcE1RHFUFqe5AfiwlINXisXfDGro23fWdPUfOMjRY=
github.com/xeonx/timeago v1.0.0-rc4 h1:9rRzv48GlJC0vm+iBpLcWAr8YbETyN9Vij+7h2ammz4=
github.com/xeonx/timeago v1.0.0-rc4/go.mod h1:qDLrYEFynLO7y5Ho7w3GwgtYgpy5UfhcXIIQvMKVDkA=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/stack.v0 v0.0.0-20141108040640-9b43fcefddd0 h1:lMH45EKqD8Nf6LwoF+43YOKjOAEEHQRVgDyG8RCV4MU=
gopkg.in/stack.v0 v0.0.0-20141108040640-9b43fcefddd0/go.mod h1:kl/bNzW/jgTgUOCGDj3XPn9/Hbfhw6pjfBRUnaTioFQ=
gopkg.in/stretchr/testify.v1 v1.2.2 h1:yhQC6Uy5CqibAIlk1wlusa/MJ3iAN49/BsR/dCCKz3M=
gopkg.in/stretchr/testify.v1 v1.2.2/go.mod h1:QI5V/q6UbPmuhtm10CaFZxED9NreB8PnFYN9JcR6TxU=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -6,30 +6,41 @@ package harness
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"sync"
"sync/atomic"
"time" "time"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"log"
) )
// Error is used for constant errors.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
const ErrTimedOut Error = "app timed out"
// App contains the configuration for running a Revel app. (Not for the app itself) // App contains the configuration for running a Revel app. (Not for the app itself)
// Its only purpose is constructing the command to execute. // Its only purpose is constructing the command to execute.
type App struct { type App struct {
BinaryPath string // Path to the app executable BinaryPath string // Path to the app executable
Port int // Port to pass as a command line argument. Port int // Port to pass as a command line argument.
cmd AppCmd // The last cmd returned. cmd AppCmd // The last cmd returned.
Paths *model.RevelContainer PackagePathMap map[string]string // Package to directory path map
Paths *model.RevelContainer
} }
// NewApp returns app instance with binary path in it // NewApp returns app instance with binary path in it.
func NewApp(binPath string, paths *model.RevelContainer) *App { func NewApp(binPath string, paths *model.RevelContainer, packagePathMap map[string]string) *App {
return &App{BinaryPath: binPath, Paths: paths, Port: paths.HTTPPort} return &App{BinaryPath: binPath, Paths: paths, Port: paths.HTTPPort, PackagePathMap: packagePathMap}
} }
// Cmd returns a command to run the app server using the current configuration. // Cmd returns a command to run the app server using the current configuration.
@@ -49,7 +60,7 @@ type AppCmd struct {
*exec.Cmd *exec.Cmd
} }
// NewAppCmd returns the AppCmd with parameters initialized for running app // NewAppCmd returns the AppCmd with parameters initialized for running app.
func NewAppCmd(binPath string, port int, runMode string, paths *model.RevelContainer) AppCmd { func NewAppCmd(binPath string, port int, runMode string, paths *model.RevelContainer) AppCmd {
cmd := exec.Command(binPath, cmd := exec.Command(binPath,
fmt.Sprintf("-port=%d", port), fmt.Sprintf("-port=%d", port),
@@ -61,35 +72,41 @@ func NewAppCmd(binPath string, port int, runMode string, paths *model.RevelConta
// Start the app server, and wait until it is ready to serve requests. // Start the app server, and wait until it is ready to serve requests.
func (cmd AppCmd) Start(c *model.CommandConfig) error { func (cmd AppCmd) Start(c *model.CommandConfig) error {
listeningWriter := &startupListeningWriter{os.Stdout, make(chan bool), c} listeningWriter := &startupListeningWriter{os.Stdout, make(chan bool), c, &bytes.Buffer{}}
cmd.Stdout = listeningWriter cmd.Stdout = listeningWriter
cmd.Stderr = listeningWriter
utils.CmdInit(cmd.Cmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec app:", "path", cmd.Path, "args", cmd.Args, "dir", cmd.Dir, "env", cmd.Env) utils.Logger.Info("Exec app:", "path", cmd.Path, "args", cmd.Args, "dir", cmd.Dir, "env", cmd.Env)
utils.CmdInit(cmd.Cmd, c.AppPath)
if err := cmd.Cmd.Start(); err != nil { if err := cmd.Cmd.Start(); err != nil {
utils.Logger.Fatal("Error running:", "error", err) utils.Logger.Fatal("Error running:", "error", err)
} }
select { select {
case exitState := <-cmd.waitChan(): case exitState := <-cmd.waitChan():
println("Revel proxy is listening, point your browser to :", c.Run.Port) fmt.Println("Startup failure view previous messages, \n Proxy is listening :", c.Run.Port)
return errors.New("revel/harness: app died reason: " + exitState) err := utils.NewError("", "Revel Run Error", "starting your application there was an exception. See terminal output, "+exitState, "")
atomic.SwapInt32(&startupError, 1)
// TODO pretiffy command line output
err.Stack = listeningWriter.buffer.String()
return err
case <-time.After(60 * time.Second): case <-time.After(60 * time.Second):
println("Revel proxy is listening, point your browser to :", c.Run.Port) println("Revel proxy is listening, point your browser to :", c.Run.Port)
utils.Logger.Error("Killing revel server process did not respond after wait timeout.", "processid", cmd.Process.Pid) utils.Logger.Error("Killing revel server process did not respond after wait timeout.", "processid", cmd.Process.Pid)
cmd.Kill() cmd.Kill()
return errors.New("revel/harness: app timed out")
return fmt.Errorf("revel/harness: %w", ErrTimedOut)
case <-listeningWriter.notifyReady: case <-listeningWriter.notifyReady:
println("Revel proxy is listening, point your browser to :", c.Run.Port) println("Revel proxy is listening, point your browser to :", c.Run.Port)
return nil return nil
} }
} }
// Run the app server inline. Never returns. // Run the app server inline. Never returns.
func (cmd AppCmd) Run() { func (cmd AppCmd) Run(c *model.CommandConfig) {
log.Println("Exec app:", "path", cmd.Path, "args", cmd.Args) utils.CmdInit(cmd.Cmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec app:", "path", cmd.Path, "args", cmd.Args)
if err := cmd.Cmd.Run(); err != nil { if err := cmd.Cmd.Run(); err != nil {
utils.Logger.Fatal("Error running:", "error", err) utils.Logger.Fatal("Error running:", "error", err)
} }
@@ -98,11 +115,61 @@ func (cmd AppCmd) Run() {
// Kill terminates the app server if it's running. // Kill terminates the app server if it's running.
func (cmd AppCmd) Kill() { func (cmd AppCmd) Kill() {
if cmd.Cmd != nil && (cmd.ProcessState == nil || !cmd.ProcessState.Exited()) { if cmd.Cmd != nil && (cmd.ProcessState == nil || !cmd.ProcessState.Exited()) {
utils.Logger.Info("Killing revel server pid", "pid", cmd.Process.Pid) // Windows appears to send the kill to all threads, shutting down the
err := cmd.Process.Kill() // server before this can, this check will ensure the process is still running
if err != nil { if _, err := os.FindProcess(cmd.Process.Pid); err != nil {
utils.Logger.Fatal("Failed to kill revel server:", "error", err) // Server has already exited
utils.Logger.Info("Server not running revel server pid", "pid", cmd.Process.Pid)
return
} }
// Wait for the shutdown channel
waitMutex := &sync.WaitGroup{}
waitMutex.Add(1)
ch := make(chan bool, 1)
go func() {
waitMutex.Done()
s, err := cmd.Process.Wait()
defer func() {
ch <- true
}()
if err != nil {
utils.Logger.Info("Wait failed for process ", "error", err)
}
if s != nil {
utils.Logger.Info("Revel App exited", "state", s.String())
}
}()
// Wait for the channel to begin waiting
waitMutex.Wait()
// Send an interrupt signal to allow for a graceful shutdown
utils.Logger.Info("Killing revel server pid", "pid", cmd.Process.Pid)
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
utils.Logger.Info(
"Revel app already exited.",
"processid", cmd.Process.Pid, "error", err,
"killerror", cmd.Process.Kill())
return
}
// Use a timer to ensure that the process exits
utils.Logger.Info("Waiting to exit")
select {
case <-ch:
return
case <-time.After(60 * time.Second):
// Kill the process
utils.Logger.Error(
"Revel app failed to exit in 60 seconds - killing.",
"processid", cmd.Process.Pid,
"killerror", cmd.Process.Kill())
}
utils.Logger.Info("Done Waiting to exit")
} }
} }
@@ -129,8 +196,10 @@ type startupListeningWriter struct {
dest io.Writer dest io.Writer
notifyReady chan bool notifyReady chan bool
c *model.CommandConfig c *model.CommandConfig
buffer *bytes.Buffer
} }
// Writes to this output stream.
func (w *startupListeningWriter) Write(p []byte) (int, error) { func (w *startupListeningWriter) Write(p []byte) (int, error) {
if w.notifyReady != nil && bytes.Contains(p, []byte("Revel engine is listening on")) { if w.notifyReady != nil && bytes.Contains(p, []byte("Revel engine is listening on")) {
w.notifyReady <- true w.notifyReady <- true
@@ -142,5 +211,8 @@ func (w *startupListeningWriter) Write(p []byte) (int, error) {
w.notifyReady = nil w.notifyReady = nil
} }
} }
if w.notifyReady != nil {
w.buffer.Write(p)
}
return w.dest.Write(p) return w.dest.Write(p)
} }

View File

@@ -20,16 +20,27 @@ import (
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/parser" "github.com/revel/cmd/parser"
"github.com/revel/cmd/parser2"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
) )
var importErrorPattern = regexp.MustCompile("cannot find package \"([^\"]+)\"") var importErrorPattern = regexp.MustCompile("cannot find package \"([^\"]+)\"")
var importErrorPattern2 = regexp.MustCompile("no required module provides package ([^;]+)+")
var addPackagePattern = regexp.MustCompile(`to add:\n\tgo get (.*)\n`)
type ByString []*model.TypeInfo type ByString []*model.TypeInfo
func (c ByString) Len() int { return len(c) } func (c ByString) Len() int {
func (c ByString) Swap(i, j int) { c[i], c[j] = c[j], c[i] } return len(c)
func (c ByString) Less(i, j int) bool { return c[i].String() < c[j].String() } }
func (c ByString) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c ByString) Less(i, j int) bool {
return c[i].String() < c[j].String()
}
// Build the app: // Build the app:
// 1. Generate the the main.go file. // 1. Generate the the main.go file.
@@ -40,7 +51,13 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
// First, clear the generated files (to avoid them messing with ProcessSource). // First, clear the generated files (to avoid them messing with ProcessSource).
cleanSource(paths, "tmp", "routes") cleanSource(paths, "tmp", "routes")
sourceInfo, err := parser.ProcessSource(paths) var sourceInfo *model.SourceInfo
if c.HistoricBuildMode {
sourceInfo, err = parser.ProcessSource(paths)
} else {
sourceInfo, err = parser2.ProcessSource(paths)
}
if err != nil { if err != nil {
return return
} }
@@ -88,33 +105,8 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
utils.Logger.Fatal("Go executable not found in PATH.") utils.Logger.Fatal("Go executable not found in PATH.")
} }
// Detect if deps tool should be used (is there a vendor folder ?) // Binary path is a combination of target/app directory, app's import path and its name.
useVendor := utils.DirExists(filepath.Join(paths.BasePath, "vendor")) binName := filepath.Join("target", "app", paths.ImportPath, filepath.Base(paths.BasePath))
basePath := paths.BasePath
for !useVendor {
basePath = filepath.Dir(basePath)
found := false
// Check to see if we are still in the GOPATH
for _, gopath := range filepath.SplitList(build.Default.GOPATH) {
if strings.HasPrefix(basePath, gopath) {
found = true
break
}
}
if !found {
break
} else {
useVendor = utils.DirExists(filepath.Join(basePath, "vendor"))
}
}
pkg, err := build.Default.Import(paths.ImportPath, "", build.FindOnly)
if err != nil {
return
}
// Binary path is a combination of $GOBIN/revel.d directory, app's import path and its name.
binName := filepath.Join(pkg.BinDir, "revel.d", paths.ImportPath, filepath.Base(paths.BasePath))
// Change binary path for Windows build // Change binary path for Windows build
goos := runtime.GOOS goos := runtime.GOOS
@@ -135,11 +127,28 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
return false return false
} }
if len(c.GoModFlags) > 0 {
for _, gomod := range c.GoModFlags {
goModCmd := exec.Command(goPath, append([]string{"mod"}, strings.Split(gomod, " ")...)...)
utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
output, err := goModCmd.CombinedOutput()
utils.Logger.Info("Gomod applied ", "output", string(output))
// If the build succeeded, we're done.
if err != nil {
utils.Logger.Error("Gomod Failed continuing ", "error", err, "output", string(output))
}
}
}
for { for {
appVersion := getAppVersion(paths) appVersion := getAppVersion(paths)
if appVersion == "" {
appVersion = "noVersionProvided"
}
buildTime := time.Now().UTC().Format(time.RFC3339) buildTime := time.Now().UTC().Format(time.RFC3339)
versionLinkerFlags := fmt.Sprintf("-X %s/app.AppVersion=%s -X %s/app.BuildTime=%s", versionLinkerFlags := fmt.Sprintf("-X '%s/app.AppVersion=%s' -X '%s/app.BuildTime=%s'",
paths.ImportPath, appVersion, paths.ImportPath, buildTime) paths.ImportPath, appVersion, paths.ImportPath, buildTime)
// Append any build flags specified, they will override existing flags // Append any build flags specified, they will override existing flags
@@ -147,19 +156,23 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
if len(c.BuildFlags) == 0 { if len(c.BuildFlags) == 0 {
flags = []string{ flags = []string{
"build", "build",
"-i",
"-ldflags", versionLinkerFlags, "-ldflags", versionLinkerFlags,
"-tags", buildTags, "-tags", buildTags,
"-o", binName} "-o", binName,
}
} else { } else {
if !contains(c.BuildFlags, "build") { if !contains(c.BuildFlags, "build") {
flags = []string{"build"} flags = []string{"build"}
} }
flags = append(flags, c.BuildFlags...)
if !contains(flags, "-ldflags") { if !contains(flags, "-ldflags") {
flags = append(flags, "-ldflags", versionLinkerFlags) ldflags := "-ldflags= " + versionLinkerFlags
// Add user defined build flags
for i := range c.BuildFlags {
ldflags += " -X '" + c.BuildFlags[i] + "'"
}
flags = append(flags, ldflags)
} }
if !contains(flags, "-tags") { if !contains(flags, "-tags") && buildTags != "" {
flags = append(flags, "-tags", buildTags) flags = append(flags, "-tags", buildTags)
} }
if !contains(flags, "-o") { if !contains(flags, "-o") {
@@ -167,37 +180,45 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
} }
} }
// Add in build flags
flags = append(flags, c.BuildFlags...)
// This is Go main path
gopath := c.GoPath
for _, o := range paths.ModulePathMap {
gopath += string(filepath.ListSeparator) + o
}
// Note: It's not applicable for filepath.* usage // Note: It's not applicable for filepath.* usage
flags = append(flags, path.Join(paths.ImportPath, "app", "tmp")) flags = append(flags, path.Join(paths.ImportPath, "app", "tmp"))
buildCmd := exec.Command(goPath, flags...) buildCmd := exec.Command(goPath, flags...)
buildCmd.Env = append(os.Environ(), if !c.Vendored {
"GOPATH="+gopath, // This is Go main path
) gopath := c.GoPath
utils.CmdInit(buildCmd, c.AppPath) for _, o := range paths.ModulePathMap {
utils.Logger.Info("Exec:", "args", buildCmd.Args) gopath += string(filepath.ListSeparator) + o.Path
}
buildCmd.Env = append(os.Environ(),
"GOPATH="+gopath,
)
}
utils.CmdInit(buildCmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec:", "args", buildCmd.Args, "working dir", buildCmd.Dir)
output, err := buildCmd.CombinedOutput() output, err := buildCmd.CombinedOutput()
// If the build succeeded, we're done. // If the build succeeded, we're done.
if err == nil { if err == nil {
utils.Logger.Info("Build successful continuing") utils.Logger.Info("Build successful continuing")
return NewApp(binName, paths), nil return NewApp(binName, paths, sourceInfo.PackageMap), nil
} }
// Since there was an error, capture the output in case we need to report it // Since there was an error, capture the output in case we need to report it
stOutput := string(output) stOutput := string(output)
utils.Logger.Infof("Got error on build of app %s", stOutput)
// See if it was an import error that we can go get. // See if it was an import error that we can go get.
matches := importErrorPattern.FindAllStringSubmatch(stOutput, -1) matches := importErrorPattern.FindAllStringSubmatch(stOutput, -1)
if matches == nil {
matches = importErrorPattern2.FindAllStringSubmatch(stOutput, -1)
}
if matches == nil {
matches = addPackagePattern.FindAllStringSubmatch(stOutput, -1)
}
utils.Logger.Info("Build failed checking for missing imports", "message", stOutput, "missing_imports", len(matches)) utils.Logger.Info("Build failed checking for missing imports", "message", stOutput, "missing_imports", len(matches))
if matches == nil { if matches == nil {
utils.Logger.Info("Build failed no missing imports", "message", stOutput) utils.Logger.Info("Build failed no missing imports", "message", stOutput)
@@ -214,6 +235,7 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
} }
gotten[pkgName] = struct{}{} gotten[pkgName] = struct{}{}
if err := c.PackageResolver(pkgName); err != nil { if err := c.PackageResolver(pkgName); err != nil {
panic("failed to resolve")
utils.Logger.Error("Unable to resolve package", "package", pkgName, "error", err) utils.Logger.Error("Unable to resolve package", "package", pkgName, "error", err)
return nil, newCompileError(paths, []byte(err.Error())) return nil, newCompileError(paths, []byte(err.Error()))
} }
@@ -222,9 +244,7 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
// Success getting the import, attempt to build again. // Success getting the import, attempt to build again.
} }
// TODO remove this unreachable code and document it // unreachable
utils.Logger.Fatal("Not reachable")
return nil, nil
} }
// Try to define a version string for the compiled app // Try to define a version string for the compiled app
@@ -249,7 +269,6 @@ func getAppVersion(paths *model.RevelContainer) string {
gitCmd := exec.Command(gitPath, "--git-dir="+gitDir, "--work-tree="+paths.BasePath, "describe", "--always", "--dirty") gitCmd := exec.Command(gitPath, "--git-dir="+gitDir, "--work-tree="+paths.BasePath, "describe", "--always", "--dirty")
utils.Logger.Info("Exec:", "args", gitCmd.Args) utils.Logger.Info("Exec:", "args", gitCmd.Args)
output, err := gitCmd.Output() output, err := gitCmd.Output()
if err != nil { if err != nil {
utils.Logger.Error("Cannot determine git repository version:", "error", err) utils.Logger.Error("Cannot determine git repository version:", "error", err)
return "" return ""
@@ -307,7 +326,6 @@ func cleanDir(paths *model.RevelContainer, dir string) {
// genSource renders the given template to produce source code, which it writes // genSource renders the given template to produce source code, which it writes
// to the given directory and file. // to the given directory and file.
func genSource(paths *model.RevelContainer, dir, filename, templateSource string, args map[string]interface{}) error { func genSource(paths *model.RevelContainer, dir, filename, templateSource string, args map[string]interface{}) error {
return utils.GenerateTemplate(filepath.Join(paths.AppPath, dir, filename), templateSource, args) return utils.GenerateTemplate(filepath.Join(paths.AppPath, dir, filename), templateSource, args)
} }
@@ -343,17 +361,16 @@ func calcImportAliases(src *model.SourceInfo) map[string]string {
return aliases return aliases
} }
// Adds an alias to the map of alias names // Adds an alias to the map of alias names.
func addAlias(aliases map[string]string, importPath, pkgName string) { func addAlias(aliases map[string]string, importPath, pkgName string) {
alias, ok := aliases[importPath] _, ok := aliases[importPath]
if ok { if ok {
return return
} }
alias = makePackageAlias(aliases, pkgName) aliases[importPath] = makePackageAlias(aliases, pkgName)
aliases[importPath] = alias
} }
// Generates a package alias // Generates a package alias.
func makePackageAlias(aliases map[string]string, pkgName string) string { func makePackageAlias(aliases map[string]string, pkgName string) string {
i := 0 i := 0
alias := pkgName alias := pkgName
@@ -364,7 +381,7 @@ func makePackageAlias(aliases map[string]string, pkgName string) string {
return alias return alias
} }
// Returns true if this value is in the map // Returns true if this value is in the map.
func containsValue(m map[string]string, val string) bool { func containsValue(m map[string]string, val string) bool {
for _, v := range m { for _, v := range m {
if v == val { if v == val {
@@ -376,7 +393,7 @@ func containsValue(m map[string]string, val string) bool {
// Parse the output of the "go build" command. // Parse the output of the "go build" command.
// Return a detailed Error. // Return a detailed Error.
func newCompileError(paths *model.RevelContainer, output []byte) *utils.Error { func newCompileError(paths *model.RevelContainer, output []byte) *utils.SourceError {
errorMatch := regexp.MustCompile(`(?m)^([^:#]+):(\d+):(\d+:)? (.*)$`). errorMatch := regexp.MustCompile(`(?m)^([^:#]+):(\d+):(\d+:)? (.*)$`).
FindSubmatch(output) FindSubmatch(output)
if errorMatch == nil { if errorMatch == nil {
@@ -384,7 +401,7 @@ func newCompileError(paths *model.RevelContainer, output []byte) *utils.Error {
if errorMatch == nil { if errorMatch == nil {
utils.Logger.Error("Failed to parse build errors", "error", string(output)) utils.Logger.Error("Failed to parse build errors", "error", string(output))
return &utils.Error{ return &utils.SourceError{
SourceType: "Go code", SourceType: "Go code",
Title: "Go Compilation Error", Title: "Go Compilation Error",
Description: "See console for build error.", Description: "See console for build error.",
@@ -400,7 +417,7 @@ func newCompileError(paths *model.RevelContainer, output []byte) *utils.Error {
// Extract the paths from the gopaths, and search for file there first // Extract the paths from the gopaths, and search for file there first
gopaths := filepath.SplitList(build.Default.GOPATH) gopaths := filepath.SplitList(build.Default.GOPATH)
for _, gp := range gopaths { for _, gp := range gopaths {
newPath := filepath.Join(gp,"src", paths.ImportPath, relFilename) newPath := filepath.Join(gp, "src", paths.ImportPath, relFilename)
println(newPath) println(newPath)
if utils.Exists(newPath) { if utils.Exists(newPath) {
return newPath return newPath
@@ -411,14 +428,13 @@ func newCompileError(paths *model.RevelContainer, output []byte) *utils.Error {
return newPath return newPath
} }
// Read the source for the offending file. // Read the source for the offending file.
var ( var (
relFilename = string(errorMatch[1]) // e.g. "src/revel/sample/app/controllers/app.go" relFilename = string(errorMatch[1]) // e.g. "src/revel/sample/app/controllers/app.go"
absFilename = findInPaths(relFilename) absFilename = findInPaths(relFilename)
line, _ = strconv.Atoi(string(errorMatch[2])) line, _ = strconv.Atoi(string(errorMatch[2]))
description = string(errorMatch[4]) description = string(errorMatch[4])
compileError = &utils.Error{ compileError = &utils.SourceError{
SourceType: "Go code", SourceType: "Go code",
Title: "Go Compilation Error", Title: "Go Compilation Error",
Path: relFilename, Path: relFilename,
@@ -444,7 +460,7 @@ func newCompileError(paths *model.RevelContainer, output []byte) *utils.Error {
return compileError return compileError
} }
// RevelMainTemplate template for app/tmp/main.go // RevelMainTemplate template for app/tmp/run/run.go.
const RevelRunTemplate = `// GENERATED CODE - DO NOT EDIT const RevelRunTemplate = `// GENERATED CODE - DO NOT EDIT
// This file is the run file for Revel. // This file is the run file for Revel.
// It registers all the controllers and provides details for the Revel server engine to // It registers all the controllers and provides details for the Revel server engine to
@@ -499,6 +515,7 @@ func Register() {
} }
} }
` `
const RevelMainTemplate = `// GENERATED CODE - DO NOT EDIT const RevelMainTemplate = `// GENERATED CODE - DO NOT EDIT
// This file is the main file for Revel. // This file is the main file for Revel.
// It registers all the controllers and provides details for the Revel server engine to // It registers all the controllers and provides details for the Revel server engine to
@@ -526,7 +543,7 @@ func main() {
} }
` `
// RevelRoutesTemplate template for app/conf/routes // RevelRoutesTemplate template for app/conf/routes.
const RevelRoutesTemplate = `// GENERATED CODE - DO NOT EDIT const RevelRoutesTemplate = `// GENERATED CODE - DO NOT EDIT
// This file provides a way of creating URL's based on all the actions // This file provides a way of creating URL's based on all the actions
// found in all the controllers. // found in all the controllers.

View File

@@ -15,9 +15,13 @@ package harness
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json"
"errors"
"fmt" "fmt"
"go/build" "go/build"
"html/template"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@@ -26,20 +30,21 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"time"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"github.com/revel/cmd/watcher" "github.com/revel/cmd/watcher"
"html/template"
"io/ioutil"
"sync"
) )
var ( var (
doNotWatch = []string{"tmp", "views", "routes"} doNotWatch = []string{"tmp", "views", "routes"}
lastRequestHadError int32 lastRequestHadError int32
startupError int32
startupErrorText error
) )
// Harness reverse proxies requests to the application server. // Harness reverse proxies requests to the application server.
@@ -55,6 +60,7 @@ type Harness struct {
paths *model.RevelContainer // The Revel container paths *model.RevelContainer // The Revel container
config *model.CommandConfig // The configuration config *model.CommandConfig // The configuration
runMode string // The runmode the harness is running in runMode string // The runmode the harness is running in
ranOnce bool // True app compiled once
} }
func (h *Harness) renderError(iw http.ResponseWriter, ir *http.Request, err error) { func (h *Harness) renderError(iw http.ResponseWriter, ir *http.Request, err error) {
@@ -66,6 +72,7 @@ func (h *Harness) renderError(iw http.ResponseWriter, ir *http.Request, err erro
if err == nil { if err == nil {
utils.Logger.Panic("Caller passed in a nil error") utils.Logger.Panic("Caller passed in a nil error")
} }
templateSet := template.New("__root__") templateSet := template.New("__root__")
seekViewOnPath := func(view string) (path string) { seekViewOnPath := func(view string) (path string) {
path = filepath.Join(h.paths.ViewsPath, "errors", view) path = filepath.Join(h.paths.ViewsPath, "errors", view)
@@ -83,26 +90,27 @@ func (h *Harness) renderError(iw http.ResponseWriter, ir *http.Request, err erro
} }
return return
} }
target := []string{seekViewOnPath("500.html"), seekViewOnPath("500-dev.html")} target := []string{seekViewOnPath("500.html"), seekViewOnPath("500-dev.html")}
if !utils.Exists(target[0]) { if !utils.Exists(target[0]) {
fmt.Fprintf(iw, "Target template not found not found %s<br />\n", target[0]) fmt.Fprintf(iw, "Target template not found not found %s<br />\n", target[0])
fmt.Fprintf(iw, "An error ocurred %s", err.Error()) fmt.Fprintf(iw, "An error occurred %s", err.Error())
return return
} }
var revelError *utils.Error
switch e := err.(type) { var revelError *utils.SourceError
case *utils.Error:
revelError = e if !errors.As(err, &revelError) {
case error: revelError = &utils.SourceError{
revelError = &utils.Error{
Title: "Server Error", Title: "Server Error",
Description: e.Error(), Description: err.Error(),
} }
} }
if revelError == nil { if revelError == nil {
panic("no error provided") panic("no error provided")
} }
viewArgs := map[string]interface{}{} viewArgs := map[string]interface{}{}
viewArgs["RunMode"] = h.paths.RunMode viewArgs["RunMode"] = h.paths.RunMode
viewArgs["DevMode"] = h.paths.DevMode viewArgs["DevMode"] = h.paths.DevMode
@@ -152,15 +160,16 @@ func (h *Harness) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func NewHarness(c *model.CommandConfig, paths *model.RevelContainer, runMode string, noProxy bool) *Harness { func NewHarness(c *model.CommandConfig, paths *model.RevelContainer, runMode string, noProxy bool) *Harness {
// Get a template loader to render errors. // Get a template loader to render errors.
// Prefer the app's views/errors directory, and fall back to the stock error pages. // Prefer the app's views/errors directory, and fall back to the stock error pages.
//revel.MainTemplateLoader = revel.NewTemplateLoader( // revel.MainTemplateLoader = revel.NewTemplateLoader(
// []string{filepath.Join(revel.RevelPath, "templates")}) // []string{filepath.Join(revel.RevelPath, "templates")})
//if err := revel.MainTemplateLoader.Refresh(); err != nil { // if err := revel.MainTemplateLoader.Refresh(); err != nil {
// revel.RevelLog.Error("Template loader error", "error", err) // revel.RevelLog.Error("Template loader error", "error", err)
//} // }
addr := paths.HTTPAddr addr := paths.HTTPAddr
port := paths.Config.IntDefault("harness.port", 0) port := paths.Config.IntDefault("harness.port", 0)
scheme := "http" scheme := "http"
if paths.HTTPSsl { if paths.HTTPSsl {
scheme = "https" scheme = "https"
} }
@@ -198,8 +207,23 @@ func NewHarness(c *model.CommandConfig, paths *model.RevelContainer, runMode str
} }
// Refresh method rebuilds the Revel application and run it on the given port. // Refresh method rebuilds the Revel application and run it on the given port.
// called by the watcher // called by the watcher.
func (h *Harness) Refresh() (err *utils.Error) { func (h *Harness) Refresh() (err *utils.SourceError) {
t := time.Now()
fmt.Println("Change detected, recompiling")
err = h.refresh()
if err != nil && !h.ranOnce && h.useProxy {
addr := fmt.Sprintf("%s:%d", h.paths.HTTPAddr, h.paths.HTTPPort)
fmt.Printf("\nError compiling code, to view error details see proxy running on http://%s\n\n", addr)
}
h.ranOnce = true
fmt.Printf("\nTime to recompile %s\n", time.Since(t).String())
return
}
func (h *Harness) refresh() (err *utils.SourceError) {
// Allow only one thread to rebuild the process // Allow only one thread to rebuild the process
// If multiple requests to rebuild are queued only the last one is executed on // If multiple requests to rebuild are queued only the last one is executed on
// So before a build is started we wait for a second to determine if // So before a build is started we wait for a second to determine if
@@ -217,21 +241,43 @@ func (h *Harness) Refresh() (err *utils.Error) {
h.app, newErr = Build(h.config, h.paths) h.app, newErr = Build(h.config, h.paths)
if newErr != nil { if newErr != nil {
utils.Logger.Error("Build detected an error", "error", newErr) utils.Logger.Error("Build detected an error", "error", newErr)
if castErr, ok := newErr.(*utils.Error); ok {
var castErr *utils.SourceError
if errors.As(newErr, &castErr) {
return castErr return castErr
} }
err = &utils.Error{
err = &utils.SourceError{
Title: "App failed to start up", Title: "App failed to start up",
Description: err.Error(), Description: newErr.Error(),
} }
return return
} }
if h.useProxy { if h.useProxy {
h.app.Port = h.port h.app.Port = h.port
if err2 := h.app.Cmd(h.runMode).Start(h.config); err2 != nil { runMode := h.runMode
if !h.config.HistoricMode {
// Recalulate run mode based on the config
var paths []byte
if len(h.app.PackagePathMap) > 0 {
paths, _ = json.Marshal(h.app.PackagePathMap)
}
runMode = fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v,"packagePathMap":%s}`, h.app.Paths.RunMode, h.config.GetVerbose(), string(paths))
}
if err2 := h.app.Cmd(runMode).Start(h.config); err2 != nil {
utils.Logger.Error("Could not start application", "error", err2) utils.Logger.Error("Could not start application", "error", err2)
return &utils.Error{
var serr *utils.SourceError
if errors.As(err2, &serr) {
return err
}
return &utils.SourceError{
Title: "App failed to start up", Title: "App failed to start up",
Description: err2.Error(), Description: err2.Error(),
} }
@@ -244,13 +290,13 @@ func (h *Harness) Refresh() (err *utils.Error) {
} }
// WatchDir method returns false to file matches with doNotWatch // WatchDir method returns false to file matches with doNotWatch
// otheriwse true // otheriwse true.
func (h *Harness) WatchDir(info os.FileInfo) bool { func (h *Harness) WatchDir(info os.FileInfo) bool {
return !utils.ContainsString(doNotWatch, info.Name()) return !utils.ContainsString(doNotWatch, info.Name())
} }
// WatchFile method returns true given filename HasSuffix of ".go" // WatchFile method returns true given filename HasSuffix of ".go"
// otheriwse false - implements revel.DiscerningListener // otheriwse false - implements revel.DiscerningListener.
func (h *Harness) WatchFile(filename string) bool { func (h *Harness) WatchFile(filename string) bool {
return strings.HasSuffix(filename, ".go") return strings.HasSuffix(filename, ".go")
} }
@@ -266,7 +312,12 @@ func (h *Harness) Run() {
paths = append(paths, h.paths.CodePaths...) paths = append(paths, h.paths.CodePaths...)
h.watcher = watcher.NewWatcher(h.paths, false) h.watcher = watcher.NewWatcher(h.paths, false)
h.watcher.Listen(h, paths...) h.watcher.Listen(h, paths...)
h.watcher.Notify()
go func() {
if err := h.Refresh(); err != nil {
utils.Logger.Error("Failed to refresh", "error", err)
}
}()
if h.useProxy { if h.useProxy {
go func() { go func() {
@@ -276,7 +327,6 @@ func (h *Harness) Run() {
} }
addr := fmt.Sprintf("%s:%d", h.paths.HTTPAddr, h.paths.HTTPPort) addr := fmt.Sprintf("%s:%d", h.paths.HTTPAddr, h.paths.HTTPPort)
utils.Logger.Infof("Proxy server is listening on %s", addr) utils.Logger.Infof("Proxy server is listening on %s", addr)
var err error var err error
if h.paths.HTTPSsl { if h.paths.HTTPSsl {
err = http.ListenAndServeTLS( err = http.ListenAndServeTLS(
@@ -291,19 +341,21 @@ func (h *Harness) Run() {
utils.Logger.Error("Failed to start reverse proxy:", "error", err) utils.Logger.Error("Failed to start reverse proxy:", "error", err)
} }
}() }()
} }
// Kill the app on signal.
// Make a new channel to listen for the interrupt event
ch := make(chan os.Signal) ch := make(chan os.Signal)
//nolint:staticcheck // os.Kill ineffective on Unix, useful on Windows?
signal.Notify(ch, os.Interrupt, os.Kill) signal.Notify(ch, os.Interrupt, os.Kill)
<-ch <-ch
// Kill the app and exit
if h.app != nil { if h.app != nil {
h.app.Kill() h.app.Kill()
} }
os.Exit(1) os.Exit(1)
} }
// Find an unused port // Find an unused port.
func getFreePort() (port int) { func getFreePort() (port int) {
conn, err := net.Listen("tcp", ":0") conn, err := net.Listen("tcp", ":0")
if err != nil { if err != nil {

View File

@@ -0,0 +1,178 @@
package logger
import (
"io"
"os"
"github.com/mattn/go-colorable"
"gopkg.in/natefinch/lumberjack.v2"
)
type CompositeMultiHandler struct {
DebugHandler LogHandler
InfoHandler LogHandler
WarnHandler LogHandler
ErrorHandler LogHandler
CriticalHandler LogHandler
}
func NewCompositeMultiHandler() (*CompositeMultiHandler, LogHandler) {
cw := &CompositeMultiHandler{}
return cw, cw
}
func (h *CompositeMultiHandler) Log(r *Record) (err error) {
var handler LogHandler
switch r.Level {
case LvlInfo:
handler = h.InfoHandler
case LvlDebug:
handler = h.DebugHandler
case LvlWarn:
handler = h.WarnHandler
case LvlError:
handler = h.ErrorHandler
case LvlCrit:
handler = h.CriticalHandler
}
// Embed the caller function in the context
if handler != nil {
if err := handler.Log(r); err != nil {
panic(err)
}
}
return
}
func (h *CompositeMultiHandler) SetHandler(handler LogHandler, replace bool, level LogLevel) {
if handler == nil {
// Ignore empty handler
return
}
source := &h.DebugHandler
switch level {
case LvlDebug:
source = &h.DebugHandler
case LvlInfo:
source = &h.InfoHandler
case LvlWarn:
source = &h.WarnHandler
case LvlError:
source = &h.ErrorHandler
case LvlCrit:
source = &h.CriticalHandler
}
if !replace && *source != nil {
// If we are not replacing the source make sure that the level handler is applied first
if _, isLevel := (*source).(*LevelFilterHandler); !isLevel {
*source = LevelHandler(level, *source)
}
// If this already was a list add a new logger to it
if ll, found := (*source).(*ListLogHandler); found {
ll.Add(handler)
} else {
*source = NewListLogHandler(*source, handler)
}
} else {
*source = handler
}
}
// For the multi handler set the handler, using the LogOptions defined.
func (h *CompositeMultiHandler) SetHandlers(handler LogHandler, options *LogOptions) {
if len(options.Levels) == 0 {
options.Levels = LvlAllList
}
// Set all levels
for _, lvl := range options.Levels {
h.SetHandler(handler, options.ReplaceExistingHandler, lvl)
}
}
func (h *CompositeMultiHandler) SetJSON(writer io.Writer, options *LogOptions) {
handler := CallerFileHandler(StreamHandler(writer, JSONFormatEx(
options.GetBoolDefault("pretty", false),
options.GetBoolDefault("lineSeparated", true),
)))
if options.HandlerWrap != nil {
handler = options.HandlerWrap.SetChild(handler)
}
h.SetHandlers(handler, options)
}
// Use built in rolling function.
func (h *CompositeMultiHandler) SetJSONFile(filePath string, options *LogOptions) {
writer := &lumberjack.Logger{
Filename: filePath,
MaxSize: options.GetIntDefault("maxSizeMB", 1024), // megabytes
MaxAge: options.GetIntDefault("maxAgeDays", 7), // days
MaxBackups: options.GetIntDefault("maxBackups", 7),
Compress: options.GetBoolDefault("compress", true),
}
h.SetJSON(writer, options)
}
func (h *CompositeMultiHandler) SetTerminal(writer io.Writer, options *LogOptions) {
streamHandler := StreamHandler(
writer,
TerminalFormatHandler(
options.GetBoolDefault("noColor", false),
options.GetBoolDefault("smallDate", true)))
if os.Stdout == writer {
streamHandler = StreamHandler(
colorable.NewColorableStdout(),
TerminalFormatHandler(
options.GetBoolDefault("noColor", false),
options.GetBoolDefault("smallDate", true)))
} else if os.Stderr == writer {
streamHandler = StreamHandler(
colorable.NewColorableStderr(),
TerminalFormatHandler(
options.GetBoolDefault("noColor", false),
options.GetBoolDefault("smallDate", true)))
}
handler := CallerFileHandler(streamHandler)
if options.HandlerWrap != nil {
handler = options.HandlerWrap.SetChild(handler)
}
h.SetHandlers(handler, options)
}
// Use built in rolling function.
func (h *CompositeMultiHandler) SetTerminalFile(filePath string, options *LogOptions) {
writer := &lumberjack.Logger{
Filename: filePath,
MaxSize: options.GetIntDefault("maxSizeMB", 1024), // megabytes
MaxAge: options.GetIntDefault("maxAgeDays", 7), // days
MaxBackups: options.GetIntDefault("maxBackups", 7),
Compress: options.GetBoolDefault("compress", true),
}
h.SetTerminal(writer, options)
}
func (h *CompositeMultiHandler) Disable(levels ...LogLevel) {
if len(levels) == 0 {
levels = LvlAllList
}
for _, level := range levels {
switch level {
case LvlDebug:
h.DebugHandler = nil
case LvlInfo:
h.InfoHandler = nil
case LvlWarn:
h.WarnHandler = nil
case LvlError:
h.ErrorHandler = nil
case LvlCrit:
h.CriticalHandler = nil
}
}
}

View File

@@ -1,10 +1,12 @@
/* /*
Package logger contains filters and handles for the logging utilities in Revel. Package logger contains filters and handles for the logging utilities in Revel.
These facilities all currently use the logging library called log15 at These facilities all currently use the logging library called log15 at
https://github.com/inconshreveable/log15 https://github.com/inconshreveable/log15
Wrappers for the handlers are written here to provide a kind of isolation layer for Revel
in case sometime in the future we would like to switch to another source to implement logging
Defining handlers happens as follows
1) ALL handlers (log.all.output) replace any existing handlers
2) Output handlers (log.error.output) replace any existing handlers
3) Filter handlers (log.xxx.filter, log.xxx.nfilter) append to existing handlers,
note log.all.filter is treated as a filter handler, so it will NOT replace existing ones
*/ */
package logger package logger

View File

@@ -1,172 +1,189 @@
package logger package logger
import ( import (
"fmt"
"io" "io"
"os"
colorable "github.com/mattn/go-colorable"
"github.com/revel/log15"
"gopkg.in/natefinch/lumberjack.v2"
) )
// Filters out records which do not match the level type LevelFilterHandler struct {
// Uses the `log15.FilterHandler` to perform this task Level LogLevel
func LevelHandler(lvl LogLevel, h LogHandler) LogHandler { h LogHandler
l15Lvl := log15.Lvl(lvl)
return log15.FilterHandler(func(r *log15.Record) (pass bool) {
return r.Lvl == l15Lvl
}, h)
} }
// Filters out records which do not match the level // Filters out records which do not match the level
// Uses the `log15.FilterHandler` to perform this task // Uses the `log15.FilterHandler` to perform this task.
func LevelHandler(lvl LogLevel, h LogHandler) LogHandler {
return &LevelFilterHandler{lvl, h}
}
// The implementation of the Log.
func (h LevelFilterHandler) Log(r *Record) error {
if r.Level == h.Level {
return h.h.Log(r)
}
return nil
}
// Filters out records which do not match the level
// Uses the `log15.FilterHandler` to perform this task.
func MinLevelHandler(lvl LogLevel, h LogHandler) LogHandler { func MinLevelHandler(lvl LogLevel, h LogHandler) LogHandler {
l15Lvl := log15.Lvl(lvl) return FilterHandler(func(r *Record) (pass bool) {
return log15.FilterHandler(func(r *log15.Record) (pass bool) { return r.Level <= lvl
return r.Lvl <= l15Lvl
}, h) }, h)
} }
// Filters out records which match the level // Filters out records which match the level
// Uses the `log15.FilterHandler` to perform this task // Uses the `log15.FilterHandler` to perform this task.
func NotLevelHandler(lvl LogLevel, h LogHandler) LogHandler { func NotLevelHandler(lvl LogLevel, h LogHandler) LogHandler {
l15Lvl := log15.Lvl(lvl) return FilterHandler(func(r *Record) (pass bool) {
return log15.FilterHandler(func(r *log15.Record) (pass bool) { return r.Level != lvl
return r.Lvl != l15Lvl
}, h) }, h)
} }
// Adds in a context called `caller` to the record (contains file name and line number like `foo.go:12`)
// Uses the `log15.CallerFileHandler` to perform this task
func CallerFileHandler(h LogHandler) LogHandler { func CallerFileHandler(h LogHandler) LogHandler {
return log15.CallerFileHandler(h) return FuncHandler(func(r *Record) error {
r.Context.Add("caller", fmt.Sprint(r.Call))
return h.Log(r)
})
} }
// Adds in a context called `caller` to the record (contains file name and line number like `foo.go:12`) // Adds in a context called `caller` to the record (contains file name and line number like `foo.go:12`)
// Uses the `log15.CallerFuncHandler` to perform this task // Uses the `log15.CallerFuncHandler` to perform this task.
func CallerFuncHandler(h LogHandler) LogHandler { func CallerFuncHandler(h LogHandler) LogHandler {
return log15.CallerFuncHandler(h) // TODO: infinite recursion
return CallerFuncHandler(h)
} }
// Filters out records which match the key value pair // Filters out records which match the key value pair
// Uses the `log15.MatchFilterHandler` to perform this task // Uses the `log15.MatchFilterHandler` to perform this task.
func MatchHandler(key string, value interface{}, h LogHandler) LogHandler { func MatchHandler(key string, value interface{}, h LogHandler) LogHandler {
return log15.MatchFilterHandler(key, value, h) return MatchFilterHandler(key, value, h)
} }
// If match then A handler is called otherwise B handler is called // MatchFilterHandler returns a Handler that only writes records
// to the wrapped Handler if the given key in the logged
// context matches the value. For example, to only log records
// from your ui package:
//
// log.MatchFilterHandler("pkg", "app/ui", log.StdoutHandler)
//
func MatchFilterHandler(key string, value interface{}, h LogHandler) LogHandler {
return FilterHandler(func(r *Record) (pass bool) {
return r.Context[key] == value
}, h)
}
// If match then A handler is called otherwise B handler is called.
func MatchAbHandler(key string, value interface{}, a, b LogHandler) LogHandler { func MatchAbHandler(key string, value interface{}, a, b LogHandler) LogHandler {
return log15.FuncHandler(func(r *log15.Record) error { return FuncHandler(func(r *Record) error {
for i := 0; i < len(r.Ctx); i += 2 { if r.Context[key] == value {
if r.Ctx[i] == key { return a.Log(r)
if r.Ctx[i+1] == value { } else if b != nil {
if a != nil {
return a.Log(r)
}
return nil
}
}
}
if b != nil {
return b.Log(r) return b.Log(r)
} }
return nil return nil
}) })
} }
// The nil handler is used if logging for a specific request needs to be turned off // The nil handler is used if logging for a specific request needs to be turned off.
func NilHandler() LogHandler { func NilHandler() LogHandler {
return log15.FuncHandler(func(r *log15.Record) error { return FuncHandler(func(r *Record) error {
return nil return nil
}) })
} }
// Match all values in map to log // Match all values in map to log.
func MatchMapHandler(matchMap map[string]interface{}, a LogHandler) LogHandler { func MatchMapHandler(matchMap map[string]interface{}, a LogHandler) LogHandler {
return matchMapHandler(matchMap, false, a) return matchMapHandler(matchMap, false, a)
} }
// Match !(Match all values in map to log) The inverse of MatchMapHandler // Match !(Match all values in map to log) The inverse of MatchMapHandler.
func NotMatchMapHandler(matchMap map[string]interface{}, a LogHandler) LogHandler { func NotMatchMapHandler(matchMap map[string]interface{}, a LogHandler) LogHandler {
return matchMapHandler(matchMap, true, a) return matchMapHandler(matchMap, true, a)
} }
// Rather then chaining multiple filter handlers, process all here // Rather then chaining multiple filter handlers, process all here.
func matchMapHandler(matchMap map[string]interface{}, inverse bool, a LogHandler) LogHandler { func matchMapHandler(matchMap map[string]interface{}, inverse bool, a LogHandler) LogHandler {
return log15.FuncHandler(func(r *log15.Record) error { return FuncHandler(func(r *Record) error {
checkMap := map[string]bool{} matchCount := 0
// Copy the map to a bool for k, v := range matchMap {
for i := 0; i < len(r.Ctx); i += 2 { value, found := r.Context[k]
if value, found := matchMap[r.Ctx[i].(string)]; found && value == r.Ctx[i+1] { if !found {
checkMap[r.Ctx[i].(string)] = true return nil
} }
// Test for two failure cases
if value == v && inverse || value != v && !inverse {
return nil
}
matchCount++
} }
if len(checkMap) == len(matchMap) {
if !inverse { if matchCount != len(matchMap) {
return a.Log(r) return nil
}
return a.Log(r)
})
}
// Filters out records which do not match the key value pair
// Uses the `log15.FilterHandler` to perform this task.
func NotMatchHandler(key string, value interface{}, h LogHandler) LogHandler {
return FilterHandler(func(r *Record) (pass bool) {
return r.Context[key] != value
}, h)
}
func MultiHandler(hs ...LogHandler) LogHandler {
return FuncHandler(func(r *Record) error {
for _, h := range hs {
if err := h.Log(r); err != nil {
panic(err)
} }
} else if inverse {
return a.Log(r)
} }
return nil return nil
}) })
} }
// Filters out records which do not match the key value pair // StreamHandler writes log records to an io.Writer
// Uses the `log15.FilterHandler` to perform this task // with the given format. StreamHandler can be used
func NotMatchHandler(key string, value interface{}, h LogHandler) LogHandler { // to easily begin writing log records to other
return log15.FilterHandler(func(r *log15.Record) (pass bool) { // outputs.
switch key { //
case r.KeyNames.Lvl: // StreamHandler wraps itself with LazyHandler and SyncHandler
return r.Lvl != value // to evaluate Lazy objects and perform safe concurrent writes.
case r.KeyNames.Time:
return r.Time != value
case r.KeyNames.Msg:
return r.Msg != value
}
for i := 0; i < len(r.Ctx); i += 2 {
if r.Ctx[i] == key {
return r.Ctx[i+1] == value
}
}
return true
}, h)
}
func MultiHandler(hs ...LogHandler) LogHandler {
// Convert the log handlers to log15.Handlers
handlers := []log15.Handler{}
for _, h := range hs {
if h != nil {
handlers = append(handlers, h)
}
}
return log15.MultiHandler(handlers...)
}
// Outputs the records to the passed in stream
// Uses the `log15.StreamHandler` to perform this task
func StreamHandler(wr io.Writer, fmtr LogFormat) LogHandler { func StreamHandler(wr io.Writer, fmtr LogFormat) LogHandler {
return log15.StreamHandler(wr, fmtr) h := FuncHandler(func(r *Record) error {
_, err := wr.Write(fmtr.Format(r))
return err
})
return LazyHandler(SyncHandler(h))
} }
// Filter handler, this is the only // Filter handler.
// Uses the `log15.FilterHandler` to perform this task func FilterHandler(fn func(r *Record) bool, h LogHandler) LogHandler {
func FilterHandler(fn func(r *log15.Record) bool, h LogHandler) LogHandler { return FuncHandler(func(r *Record) error {
return log15.FilterHandler(fn, h) if fn(r) {
return h.Log(r)
}
return nil
})
} }
// List log handler handles a list of LogHandlers.
type ListLogHandler struct { type ListLogHandler struct {
handlers []LogHandler handlers []LogHandler
} }
// Create a new list of log handlers.
func NewListLogHandler(h1, h2 LogHandler) *ListLogHandler { func NewListLogHandler(h1, h2 LogHandler) *ListLogHandler {
ll := &ListLogHandler{handlers: []LogHandler{h1, h2}} ll := &ListLogHandler{handlers: []LogHandler{h1, h2}}
return ll return ll
} }
func (ll *ListLogHandler) Log(r *log15.Record) (err error) {
// Log the record.
func (ll *ListLogHandler) Log(r *Record) (err error) {
for _, handler := range ll.handlers { for _, handler := range ll.handlers {
if err == nil { if err == nil {
err = handler.Log(r) err = handler.Log(r)
@@ -174,13 +191,18 @@ func (ll *ListLogHandler) Log(r *log15.Record) (err error) {
handler.Log(r) handler.Log(r)
} }
} }
return return
} }
// Add another log handler.
func (ll *ListLogHandler) Add(h LogHandler) { func (ll *ListLogHandler) Add(h LogHandler) {
if h != nil { if h != nil {
ll.handlers = append(ll.handlers, h) ll.handlers = append(ll.handlers, h)
} }
} }
// Remove a log handler.
func (ll *ListLogHandler) Del(h LogHandler) { func (ll *ListLogHandler) Del(h LogHandler) {
if h != nil { if h != nil {
for i, handler := range ll.handlers { for i, handler := range ll.handlers {
@@ -190,161 +212,3 @@ func (ll *ListLogHandler) Del(h LogHandler) {
} }
} }
} }
type CompositeMultiHandler struct {
DebugHandler LogHandler
InfoHandler LogHandler
WarnHandler LogHandler
ErrorHandler LogHandler
CriticalHandler LogHandler
}
func NewCompositeMultiHandler() (*CompositeMultiHandler, LogHandler) {
cw := &CompositeMultiHandler{}
return cw, cw
}
func (h *CompositeMultiHandler) Log(r *log15.Record) (err error) {
var handler LogHandler
switch r.Lvl {
case log15.LvlInfo:
handler = h.InfoHandler
case log15.LvlDebug:
handler = h.DebugHandler
case log15.LvlWarn:
handler = h.WarnHandler
case log15.LvlError:
handler = h.ErrorHandler
case log15.LvlCrit:
handler = h.CriticalHandler
}
// Embed the caller function in the context
if handler != nil {
handler.Log(r)
}
return
}
func (h *CompositeMultiHandler) SetHandler(handler LogHandler, replace bool, level LogLevel) {
if handler == nil {
// Ignore empty handler
return
}
source := &h.DebugHandler
switch level {
case LvlDebug:
source = &h.DebugHandler
case LvlInfo:
source = &h.InfoHandler
case LvlWarn:
source = &h.WarnHandler
case LvlError:
source = &h.ErrorHandler
case LvlCrit:
source = &h.CriticalHandler
}
if !replace && *source != nil {
// If this already was a list add a new logger to it
if ll, found := (*source).(*ListLogHandler); found {
ll.Add(handler)
} else {
*source = NewListLogHandler(*source, handler)
}
} else {
*source = handler
}
}
func (h *CompositeMultiHandler) SetHandlers(handler LogHandler, options *LogOptions) {
if len(options.Levels) == 0 {
options.Levels = LvlAllList
}
// Set all levels
for _, lvl := range options.Levels {
h.SetHandler(handler, options.ReplaceExistingHandler, lvl)
}
}
func (h *CompositeMultiHandler) SetJson(writer io.Writer, options *LogOptions) {
handler := CallerFileHandler(StreamHandler(writer, log15.JsonFormatEx(
options.GetBoolDefault("pretty", false),
options.GetBoolDefault("lineSeparated", true),
)))
if options.HandlerWrap != nil {
handler = options.HandlerWrap.SetChild(handler)
}
h.SetHandlers(handler, options)
}
// Use built in rolling function
func (h *CompositeMultiHandler) SetJsonFile(filePath string, options *LogOptions) {
writer := &lumberjack.Logger{
Filename: filePath,
MaxSize: options.GetIntDefault("maxSizeMB", 1024), // megabytes
MaxAge: options.GetIntDefault("maxAgeDays", 7), //days
MaxBackups: options.GetIntDefault("maxBackups", 7),
Compress: options.GetBoolDefault("compress", true),
}
h.SetJson(writer, options)
}
func (h *CompositeMultiHandler) SetTerminal(writer io.Writer, options *LogOptions) {
streamHandler := StreamHandler(
writer,
TerminalFormatHandler(
options.GetBoolDefault("noColor", false),
options.GetBoolDefault("smallDate", true)))
if os.Stdout == writer {
streamHandler = StreamHandler(
colorable.NewColorableStdout(),
TerminalFormatHandler(
options.GetBoolDefault("noColor", false),
options.GetBoolDefault("smallDate", true)))
} else if os.Stderr == writer {
streamHandler = StreamHandler(
colorable.NewColorableStderr(),
TerminalFormatHandler(
options.GetBoolDefault("noColor", false),
options.GetBoolDefault("smallDate", true)))
}
handler := CallerFileHandler(streamHandler)
if options.HandlerWrap != nil {
handler = options.HandlerWrap.SetChild(handler)
}
h.SetHandlers(handler, options)
}
// Use built in rolling function
func (h *CompositeMultiHandler) SetTerminalFile(filePath string, options *LogOptions) {
writer := &lumberjack.Logger{
Filename: filePath,
MaxSize: options.GetIntDefault("maxSizeMB", 1024), // megabytes
MaxAge: options.GetIntDefault("maxAgeDays", 7), //days
MaxBackups: options.GetIntDefault("maxBackups", 7),
Compress: options.GetBoolDefault("compress", true),
}
h.SetTerminal(writer, options)
}
func (h *CompositeMultiHandler) Disable(levels ...LogLevel) {
if len(levels) == 0 {
levels = LvlAllList
}
for _, level := range levels {
switch level {
case LvlDebug:
h.DebugHandler = nil
case LvlInfo:
h.InfoHandler = nil
case LvlWarn:
h.WarnHandler = nil
case LvlError:
h.ErrorHandler = nil
case LvlCrit:
h.CriticalHandler = nil
}
}
}

190
logger/init.go Normal file
View File

@@ -0,0 +1,190 @@
package logger
// Get all handlers based on the Config (if available).
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/revel/config"
)
func InitializeFromConfig(basePath string, config *config.Context) (c *CompositeMultiHandler) {
// If running in test mode suppress anything that is not an error
if config != nil && config.BoolDefault(TestModeFlag, false) {
// Preconfigure all the options
config.SetOption("log.info.output", "none")
config.SetOption("log.debug.output", "none")
config.SetOption("log.warn.output", "none")
config.SetOption("log.error.output", "stderr")
config.SetOption("log.crit.output", "stderr")
}
// If the configuration has an all option we can skip some
c, _ = NewCompositeMultiHandler()
// Filters are assigned first, non filtered items override filters
if config != nil && !config.BoolDefault(TestModeFlag, false) {
initAllLog(c, basePath, config)
}
initLogLevels(c, basePath, config)
if c.CriticalHandler == nil && c.ErrorHandler != nil {
c.CriticalHandler = c.ErrorHandler
}
if config != nil && !config.BoolDefault(TestModeFlag, false) {
initFilterLog(c, basePath, config)
if c.CriticalHandler == nil && c.ErrorHandler != nil {
c.CriticalHandler = c.ErrorHandler
}
initRequestLog(c, basePath, config)
}
return c
}
// Init the log.all configuration options.
func initAllLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
if config != nil {
extraLogFlag := config.BoolDefault(SpecialUseFlag, false)
if output, found := config.String("log.all.output"); found {
// Set all output for the specified handler
if extraLogFlag {
log.Printf("Adding standard handler for levels to >%s< ", output)
}
initHandlerFor(c, output, basePath, NewLogOptions(config, true, nil, LvlAllList...))
}
}
}
// Init the filter options
// log.all.filter ....
// log.error.filter ....
func initFilterLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
if config != nil {
extraLogFlag := config.BoolDefault(SpecialUseFlag, false)
for _, logFilter := range logFilterList {
// Init for all filters
for _, name := range []string{
"all", "debug", "info", "warn", "error", "crit",
"trace", // TODO trace is deprecated
} {
optionList := config.Options(logFilter.LogPrefix + name + logFilter.LogSuffix)
for _, option := range optionList {
splitOptions := strings.Split(option, ".")
keyMap := map[string]interface{}{}
for x := 3; x < len(splitOptions); x += 2 {
keyMap[splitOptions[x]] = splitOptions[x+1]
}
phandler := logFilter.parentHandler(keyMap)
if extraLogFlag {
log.Printf("Adding key map handler %s %s output %s", option, name, config.StringDefault(option, ""))
fmt.Printf("Adding key map handler %s %s output %s matching %#v\n", option, name, config.StringDefault(option, ""), keyMap)
}
if name == "all" {
initHandlerFor(c, config.StringDefault(option, ""), basePath, NewLogOptions(config, false, phandler))
} else {
initHandlerFor(c, config.StringDefault(option, ""), basePath, NewLogOptions(config, false, phandler, toLevel[name]))
}
}
}
}
}
}
// Init the log.error, log.warn etc configuration options.
func initLogLevels(c *CompositeMultiHandler, basePath string, config *config.Context) {
for _, name := range []string{
"debug", "info", "warn", "error", "crit",
"trace", // TODO trace is deprecated
} {
if config != nil {
extraLogFlag := config.BoolDefault(SpecialUseFlag, false)
output, found := config.String("log." + name + ".output")
if found {
if extraLogFlag {
log.Printf("Adding standard handler %s output %s", name, output)
}
initHandlerFor(c, output, basePath, NewLogOptions(config, true, nil, toLevel[name]))
}
// Gets the list of options with said prefix
} else {
initHandlerFor(c, "stderr", basePath, NewLogOptions(config, true, nil, toLevel[name]))
}
}
}
// Init the request log options.
func initRequestLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
// Request logging to a separate output handler
// This takes the InfoHandlers and adds a MatchAbHandler handler to it to direct
// context with the word "section=requestlog" to that handler.
// Note if request logging is not enabled the MatchAbHandler will not be added and the
// request log messages will be sent out the INFO handler
outputRequest := "stdout"
if config != nil {
outputRequest = config.StringDefault("log.request.output", "")
}
oldInfo := c.InfoHandler
c.InfoHandler = nil
if outputRequest != "" {
initHandlerFor(c, outputRequest, basePath, NewLogOptions(config, false, nil, LvlInfo))
}
if c.InfoHandler != nil || oldInfo != nil {
if c.InfoHandler == nil {
c.InfoHandler = oldInfo
} else {
c.InfoHandler = MatchAbHandler("section", "requestlog", c.InfoHandler, oldInfo)
}
}
}
// Returns a handler for the level using the output string
// Accept formats for output string are
// LogFunctionMap[value] callback function
// `stdout` `stderr` `full/file/path/to/location/app.log` `full/file/path/to/location/app.json`.
func initHandlerFor(c *CompositeMultiHandler, output, basePath string, options *LogOptions) {
if options.Ctx != nil {
options.SetExtendedOptions(
"noColor", !options.Ctx.BoolDefault("log.colorize", true),
"smallDate", options.Ctx.BoolDefault("log.smallDate", true),
"maxSize", options.Ctx.IntDefault("log.maxsize", 1024*10),
"maxAge", options.Ctx.IntDefault("log.maxage", 14),
"maxBackups", options.Ctx.IntDefault("log.maxbackups", 14),
"compressBackups", !options.Ctx.BoolDefault("log.compressBackups", true),
)
}
output = strings.TrimSpace(output)
if funcHandler, found := LogFunctionMap[output]; found {
funcHandler(c, options)
} else {
switch output {
case "":
fallthrough
case "off":
// No handler, discard data
default:
// Write to file specified
if !filepath.IsAbs(output) {
output = filepath.Join(basePath, output)
}
if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {
log.Panic(err)
}
if strings.HasSuffix(output, "json") {
c.SetJSONFile(output, options)
} else {
// Override defaults for a terminal file
options.SetExtendedOptions("noColor", true)
options.SetExtendedOptions("smallDate", false)
c.SetTerminalFile(output, options)
}
}
}
}

346
logger/init_test.go Normal file
View File

@@ -0,0 +1,346 @@
// Copyright (c) 2012-2018 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 logger_test
import (
"os"
"strings"
"testing"
"github.com/revel/cmd/logger"
"github.com/revel/config"
"github.com/stretchr/testify/assert"
)
type (
// A counter for the tester.
testCounter struct {
debug, info, warn, error, critical int
}
// The data to tes.
testData struct {
config []string
result testResult
tc *testCounter
}
// The test result.
testResult struct {
debug, info, warn, error, critical int
}
)
// Single test cases.
var singleCases = []testData{
{
config: []string{"log.crit.output"},
result: testResult{0, 0, 0, 0, 1},
},
{
config: []string{"log.error.output"},
result: testResult{0, 0, 0, 1, 1},
},
{
config: []string{"log.warn.output"},
result: testResult{0, 0, 1, 0, 0},
},
{
config: []string{"log.info.output"},
result: testResult{0, 1, 0, 0, 0},
},
{
config: []string{"log.debug.output"},
result: testResult{1, 0, 0, 0, 0},
},
}
// Test singles.
func TestSingleCases(t *testing.T) {
rootLog := logger.New()
for _, testCase := range singleCases {
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
// Filter test cases.
var filterCases = []testData{
{
config: []string{"log.crit.filter.module.app"},
result: testResult{0, 0, 0, 0, 1},
},
{
config: []string{"log.crit.filter.module.appa"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.error.filter.module.app"},
result: testResult{0, 0, 0, 1, 1},
},
{
config: []string{"log.error.filter.module.appa"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.warn.filter.module.app"},
result: testResult{0, 0, 1, 0, 0},
},
{
config: []string{"log.warn.filter.module.appa"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.info.filter.module.app"},
result: testResult{0, 1, 0, 0, 0},
},
{
config: []string{"log.info.filter.module.appa"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.debug.filter.module.app"},
result: testResult{1, 0, 0, 0, 0},
},
{
config: []string{"log.debug.filter.module.appa"},
result: testResult{0, 0, 0, 0, 0},
},
}
// Filter test.
func TestFilterCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range filterCases {
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
// Inverse test cases.
var nfilterCases = []testData{
{
config: []string{"log.crit.nfilter.module.appa"},
result: testResult{0, 0, 0, 0, 1},
},
{
config: []string{"log.crit.nfilter.modules.appa"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.crit.nfilter.module.app"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.error.nfilter.module.appa"}, // Special case, when error is not nill critical inherits from error
result: testResult{0, 0, 0, 1, 1},
},
{
config: []string{"log.error.nfilter.module.app"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.warn.nfilter.module.appa"},
result: testResult{0, 0, 1, 0, 0},
},
{
config: []string{"log.warn.nfilter.module.app"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.info.nfilter.module.appa"},
result: testResult{0, 1, 0, 0, 0},
},
{
config: []string{"log.info.nfilter.module.app"},
result: testResult{0, 0, 0, 0, 0},
},
{
config: []string{"log.debug.nfilter.module.appa"},
result: testResult{1, 0, 0, 0, 0},
},
{
config: []string{"log.debug.nfilter.module.app"},
result: testResult{0, 0, 0, 0, 0},
},
}
// Inverse test.
func TestNotFilterCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range nfilterCases {
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
// off test cases.
var offCases = []testData{
{
config: []string{"log.all.output", "log.error.output=off"},
result: testResult{1, 1, 1, 0, 1},
},
}
// Off test.
func TestOffCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range offCases {
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
// Duplicate test cases.
var duplicateCases = []testData{
{
config: []string{"log.all.output", "log.error.output", "log.error.filter.module.app"},
result: testResult{1, 1, 1, 2, 1},
},
}
// test duplicate cases.
func TestDuplicateCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range duplicateCases {
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
// Contradicting cases.
var contradictCases = []testData{
{
config: []string{"log.all.output", "log.error.output=off", "log.all.output"},
result: testResult{1, 1, 1, 0, 1},
},
{
config: []string{"log.all.output", "log.error.output=off", "log.debug.filter.module.app"},
result: testResult{2, 1, 1, 0, 1},
},
{
config: []string{"log.all.filter.module.app", "log.info.output=off", "log.info.filter.module.app"},
result: testResult{1, 2, 1, 1, 1},
},
{
config: []string{"log.all.output", "log.info.output=off", "log.info.filter.module.app"},
result: testResult{1, 1, 1, 1, 1},
},
}
// Contradiction test.
func TestContradictCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range contradictCases {
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
// All test cases.
var allCases = []testData{
{
config: []string{"log.all.filter.module.app"},
result: testResult{1, 1, 1, 1, 1},
},
{
config: []string{"log.all.output"},
result: testResult{2, 2, 2, 2, 2},
},
}
// All tests.
func TestAllCases(t *testing.T) {
rootLog := logger.New("module", "app")
for i, testCase := range allCases {
testCase.logTest(t, rootLog)
allCases[i] = testCase
}
rootLog = logger.New()
for i, testCase := range allCases {
testCase.logTest(t, rootLog)
allCases[i] = testCase
}
for _, testCase := range allCases {
testCase.validate(t)
}
}
func (c *testCounter) Log(r *logger.Record) error {
switch r.Level {
case logger.LvlDebug:
c.debug++
case logger.LvlInfo:
c.info++
case logger.LvlWarn:
c.warn++
case logger.LvlError:
c.error++
case logger.LvlCrit:
c.critical++
default:
panic("Unknown log level")
}
return nil
}
func (td *testData) logTest(t *testing.T, rootLog logger.MultiLogger) {
t.Helper()
if td.tc == nil {
td.tc = &testCounter{}
counterInit(td.tc)
}
newContext := config.NewContext()
for _, i := range td.config {
iout := strings.Split(i, "=")
if len(iout) > 1 {
newContext.SetOption(iout[0], iout[1])
} else {
newContext.SetOption(i, "test")
}
}
newContext.SetOption("specialUseFlag", "true")
handler := logger.InitializeFromConfig("test", newContext)
rootLog.SetHandler(handler)
td.runLogTest(rootLog)
}
func (td *testData) runLogTest(log logger.MultiLogger) {
log.Debug("test")
log.Info("test")
log.Warn("test")
log.Error("test")
log.Crit("test")
}
func (td *testData) validate(t *testing.T) {
t.Helper()
t.Logf("Test %#v expected %#v", td.tc, td.result)
assert.Equal(t, td.result.debug, td.tc.debug, "Debug failed "+strings.Join(td.config, " "))
assert.Equal(t, td.result.info, td.tc.info, "Info failed "+strings.Join(td.config, " "))
assert.Equal(t, td.result.warn, td.tc.warn, "Warn failed "+strings.Join(td.config, " "))
assert.Equal(t, td.result.error, td.tc.error, "Error failed "+strings.Join(td.config, " "))
assert.Equal(t, td.result.critical, td.tc.critical, "Critical failed "+strings.Join(td.config, " "))
}
// Add test to the function map.
func counterInit(tc *testCounter) {
logger.LogFunctionMap["test"] = func(c *logger.CompositeMultiHandler, logOptions *logger.LogOptions) {
// Output to the test log and the stdout
outHandler := logger.LogHandler(
logger.NewListLogHandler(tc,
logger.StreamHandler(os.Stdout, logger.TerminalFormatHandler(false, true))),
)
if logOptions.HandlerWrap != nil {
outHandler = logOptions.HandlerWrap.SetChild(outHandler)
}
c.SetHandlers(outHandler, logOptions)
}
}

View File

@@ -1,656 +0,0 @@
package logger
// LoggedError is wrapper to differentiate logged panics from unexpected ones.
import (
"os"
"fmt"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sync"
"go.uber.org/zap/buffer"
"time"
"encoding/base64"
"unicode/utf8"
"encoding/json"
"math"
"io"
)
type (
MultiLogger interface {
//log15.Logger
//// New returns a new Logger that has this logger's context plus the given context
New(ctx ...interface{}) MultiLogger
//
// The encoders job is to encode the
SetHandler(h LogHandler)
SetStackDepth(int) MultiLogger
//
//// Log a message at the given level with context key/value pairs
Debug(msg string, ctx ...interface{})
Debugf(msg string, params ...interface{})
Info(msg string, ctx ...interface{})
Infof(msg string, params ...interface{})
Warn(msg string, ctx ...interface{})
Warnf(msg string, params ...interface{})
Error(msg string, ctx ...interface{})
Errorf(msg string, params ...interface{})
Crit(msg string, ctx ...interface{})
Critf(msg string, params ...interface{})
//// Logs a message as an Crit and exits
Fatal(msg string, ctx ...interface{})
Fatalf(msg string, params ...interface{})
//// Logs a message as an Crit and panics
Panic(msg string, ctx ...interface{})
Panicf(msg string, params ...interface{})
}
// The log han
LogHandler interface {
Encode(Record) ([]byte, error)
GetLevel() Level
GetWriter() io.Writer
}
// The Record
Record struct {
Level Level
Time time.Time
LoggerName string
Message string
Caller EntryCaller
Stack string
Context []Field
}
// The fields passed in
Field interface {
GetKey() string
GetValueAsString() string
GetValue() interface{}
}
EntryCaller interface {
IsDefined() bool
GetPC() uintptr
GetFile() string
GetLine() int
}
// Called only if the logger needs
ResolveLaterLogger func() interface{}
FieldType int
Level int
)
type (
zapLogger struct {
logger *zap.SugaredLogger
coreList []*zapcore.Core
}
zapField struct {
Key string
Type FieldType
Integer int64
String string
Interface interface{}
}
zapEntryCaller struct {
Defined bool
PC uintptr
File string
Line int
}
zapEncoder struct {
lh LogHandler
}
)
func newLogger(addCaller bool) MultiLogger {
logger := zap.New(nil).WithOptions(zap.AddCaller())
l := &zapLogger{logger:logger.Sugar()}
return l
}
// It is up to the handler to determine the synchronization to the output
// streams
func (z *zapLogger) SetHandler(lh LogHandler) {
// Swap out the logger when a new handler is attached
encoder := &zapEncoder{lh}
levelHandler := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.Level(lh.GetLevel())
})
logger := zap.New(zapcore.NewCore(encoder, nil, levelHandler)).WithOptions(zap.AddCaller())
Logger.With("foo","bar").Desugar().Core()
}
var Logger *zap.SugaredLogger
func InitLogger(logLevel zapcore.Level) {
config :=zap.NewDevelopmentEncoderConfig()
config.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleEncoder := NewConsoleEncoder(config)
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= logLevel
})
consoleDebugging := zapcore.Lock(os.Stdout)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
)
logger := zap.New(core).WithOptions(zap.AddCaller())
Logger = logger.Sugar()
}
type LoggedError struct{ error }
func NewLoggedError(err error) *LoggedError {
return &LoggedError{err}
}
func Errorf(format string, args ...interface{}) {
// Ensure the user's command prompt starts on the next line.
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
fmt.Fprintf(os.Stderr, format, args...)
panic(format) // Panic instead of os.Exit so that deferred will run.
}
// This is all for the Console logger - a little wordy but it works
var _sliceEncoderPool = sync.Pool{
New: func() interface{} {
return &sliceArrayEncoder{elems: make([]interface{}, 0, 2)}
},
}
func getSliceEncoder() *sliceArrayEncoder {
return _sliceEncoderPool.Get().(*sliceArrayEncoder)
}
func putSliceEncoder(e *sliceArrayEncoder) {
e.elems = e.elems[:0]
_sliceEncoderPool.Put(e)
}
type consoleEncoder struct {
*zapcore.EncoderConfig
openNamespaces int
buf *buffer.Buffer
reflectBuf *buffer.Buffer
reflectEnc *json.Encoder
}
var (
_pool = buffer.NewPool()
// Get retrieves a buffer from the pool, creating one if necessary.
Get = _pool.Get
)
// NewConsoleEncoder creates an encoder whose output is designed for human -
// rather than machine - consumption. It serializes the core log entry data
// (message, level, timestamp, etc.) in a plain-text format and leaves the
// structured context as JSON.
//
// Note that although the console encoder doesn't use the keys specified in the
// encoder configuration, it will omit any element whose key is set to the empty
// string.
func NewConsoleEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
ec := &consoleEncoder{buf : Get(), reflectBuf: Get()}
ec.EncoderConfig = &cfg
return ec
}
func (c consoleEncoder) Clone() zapcore.Encoder {
return &consoleEncoder{buf : Get(), reflectBuf: Get()}
}
func (c consoleEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
line := Get()
var color = 0
switch ent.Level {
case zap.PanicLevel:
// Magenta
color = 35
case zap.ErrorLevel:
// Red
color = 31
case zap.WarnLevel:
// Yellow
color = 33
case zap.InfoLevel:
// Green
color = 32
case zap.DebugLevel:
// Cyan
color = 36
}
// We don't want the entry's metadata to be quoted and escaped (if it's
// encoded as strings), which means that we can't use the JSON encoder. The
// simplest option is to use the memory encoder and fmt.Fprint.
//
// If this ever becomes a performance bottleneck, we can implement
// ArrayEncoder for our plain-text format.
arr := getSliceEncoder()
if c.LevelKey != "" && c.EncodeLevel != nil {
arr.AppendString(fmt.Sprintf("\x1b[%dm%-5s\x1b[0m",color,ent.Level.CapitalString()))
}
if ent.LoggerName != "" && c.NameKey != "" {
nameEncoder := c.EncodeName
if nameEncoder == nil {
// Fall back to FullNameEncoder for backward compatibility.
nameEncoder = zapcore.FullNameEncoder
}
nameEncoder(ent.LoggerName, arr)
}
if c.TimeKey != "" && c.EncodeTime != nil {
arr.AppendString(ent.Time.Format("15:04:05"))
}
if ent.Caller.Defined && c.CallerKey != "" && c.EncodeCaller != nil {
c.EncodeCaller(ent.Caller, arr)
}
for i := range arr.elems {
if i > 0 {
line.AppendByte(' ')
}
fmt.Fprint(line, arr.elems[i])
}
putSliceEncoder(arr)
// Add the message itself.
if c.MessageKey != "" {
c.addTabIfNecessary(line)
line.AppendString(ent.Message)
}
// Add any structured context.
c.writeContext(line, fields)
// If there's no stacktrace key, honor that; this allows users to force
// single-line output.
if ent.Stack != "" && c.StacktraceKey != "" {
line.AppendByte('\n')
line.AppendString(ent.Stack)
}
if c.LineEnding != "" {
line.AppendString(c.LineEnding)
} else {
line.AppendString(zapcore.DefaultLineEnding)
}
return line, nil
}
func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []zapcore.Field) {
context := c.Clone().(*consoleEncoder)
defer context.buf.Free()
//
addFields(context, extra)
context.closeOpenNamespaces()
if context.buf.Len() == 0 {
return
}
//
line.Write(context.buf.Bytes())
}
func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
for i := range fields {
fields[i].AddTo(enc)
}
}
func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
if line.Len() > 0 {
line.AppendByte('\t')
}
}
func (enc *consoleEncoder) AddArray(key string, arr zapcore.ArrayMarshaler) error {
enc.addKey(key)
return enc.AppendArray(arr)
}
func (enc *consoleEncoder) AddObject(key string, obj zapcore.ObjectMarshaler) error {
enc.addKey(key)
return enc.AppendObject(obj)
}
func (enc *consoleEncoder) AddBinary(key string, val []byte) {
enc.AddString(key, base64.StdEncoding.EncodeToString(val))
}
func (enc *consoleEncoder) AddByteString(key string, val []byte) {
enc.addKey(key)
enc.AppendByteString(val)
}
func (enc *consoleEncoder) AddBool(key string, val bool) {
enc.addKey(key)
enc.AppendBool(val)
}
func (enc *consoleEncoder) AddComplex128(key string, val complex128) {
enc.addKey(key)
enc.AppendComplex128(val)
}
func (enc *consoleEncoder) AddDuration(key string, val time.Duration) {
enc.addKey(key)
enc.AppendDuration(val)
}
func (enc *consoleEncoder) AddFloat64(key string, val float64) {
enc.addKey(key)
enc.AppendFloat64(val)
}
func (enc *consoleEncoder) AddInt64(key string, val int64) {
enc.addKey(key)
enc.AppendInt64(val)
}
func (enc *consoleEncoder) AddReflected(key string, obj interface{}) error {
enc.resetReflectBuf()
err := enc.reflectEnc.Encode(obj)
if err != nil {
return err
}
enc.reflectBuf.TrimNewline()
enc.addKey(key)
_, err = enc.buf.Write(enc.reflectBuf.Bytes())
return err
}
func (enc *consoleEncoder) OpenNamespace(key string) {
enc.addKey(key)
enc.buf.AppendByte('{')
enc.openNamespaces++
}
func (enc *consoleEncoder) AddString(key, val string) {
enc.addKey(key)
enc.AppendString(val)
}
func (enc *consoleEncoder) AddTime(key string, val time.Time) {
enc.addKey(key)
enc.AppendTime(val)
}
func (enc *consoleEncoder) AddUint64(key string, val uint64) {
enc.addKey(key)
enc.AppendUint64(val)
}
func (enc *consoleEncoder) addKey(key string) {
// Print key in different color
enc.buf.AppendString(fmt.Sprintf(" \x1b[%dm%s\x1b[0m",36,key))
enc.buf.AppendByte('=')
}
func (enc *consoleEncoder) AppendArray(arr zapcore.ArrayMarshaler) error {
enc.buf.AppendByte('[')
err := arr.MarshalLogArray(enc)
enc.buf.AppendByte(']')
return err
}
func (enc *consoleEncoder) AppendObject(obj zapcore.ObjectMarshaler) error {
enc.buf.AppendByte('{')
err := obj.MarshalLogObject(enc)
enc.buf.AppendByte('}')
return err
}
func (enc *consoleEncoder) AppendBool(val bool) {
enc.buf.AppendBool(val)
}
func (enc *consoleEncoder) AppendByteString(val []byte) {
enc.buf.AppendByte('"')
enc.safeAddByteString(val)
enc.buf.AppendByte('"')
}
func (enc *consoleEncoder) AppendComplex128(val complex128) {
// Cast to a platform-independent, fixed-size type.
r, i := float64(real(val)), float64(imag(val))
enc.buf.AppendByte('"')
// Because we're always in a quoted string, we can use strconv without
// special-casing NaN and +/-Inf.
enc.buf.AppendFloat(r, 64)
enc.buf.AppendByte('+')
enc.buf.AppendFloat(i, 64)
enc.buf.AppendByte('i')
enc.buf.AppendByte('"')
}
func (enc *consoleEncoder) AppendDuration(val time.Duration) {
cur := enc.buf.Len()
enc.EncodeDuration(val, enc)
if cur == enc.buf.Len() {
// User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
// JSON valid.
enc.AppendInt64(int64(val))
}
}
func (enc *consoleEncoder) AppendInt64(val int64) {
enc.buf.AppendInt(val)
}
func (enc *consoleEncoder) resetReflectBuf() {
if enc.reflectBuf == nil {
enc.reflectBuf = Get()
enc.reflectEnc = json.NewEncoder(enc.reflectBuf)
} else {
enc.reflectBuf.Reset()
}
}
func (enc *consoleEncoder) AppendReflected(val interface{}) error {
enc.resetReflectBuf()
err := enc.reflectEnc.Encode(val)
if err != nil {
return err
}
enc.reflectBuf.TrimNewline()
_, err = enc.buf.Write(enc.reflectBuf.Bytes())
return err
}
func (enc *consoleEncoder) AppendString(val string) {
enc.safeAddString(val)
}
func (enc *consoleEncoder) AppendTime(val time.Time) {
cur := enc.buf.Len()
enc.EncodeTime(val, enc)
if cur == enc.buf.Len() {
// User-supplied EncodeTime is a no-op. Fall back to nanos since epoch to keep
// output JSON valid.
enc.AppendInt64(val.UnixNano())
}
}
func (enc *consoleEncoder) AppendUint64(val uint64) {
enc.buf.AppendUint(val)
}
func (enc *consoleEncoder) appendFloat(val float64, bitSize int) {
switch {
case math.IsNaN(val):
enc.buf.AppendString(`"NaN"`)
case math.IsInf(val, 1):
enc.buf.AppendString(`"+Inf"`)
case math.IsInf(val, -1):
enc.buf.AppendString(`"-Inf"`)
default:
enc.buf.AppendFloat(val, bitSize)
}
}
// safeAddString JSON-escapes a string and appends it to the internal buffer.
// Unlike the standard library's encoder, it doesn't attempt to protect the
// user from browser vulnerabilities or JSONP-related problems.
func (enc *consoleEncoder) safeAddString(s string) {
for i := 0; i < len(s); {
if enc.tryAddRuneSelf(s[i]) {
i++
continue
}
r, size := utf8.DecodeRuneInString(s[i:])
if enc.tryAddRuneError(r, size) {
i++
continue
}
enc.buf.AppendString(s[i : i+size])
i += size
}
}
// safeAddByteString is no-alloc equivalent of safeAddString(string(s)) for s []byte.
func (enc *consoleEncoder) safeAddByteString(s []byte) {
for i := 0; i < len(s); {
if enc.tryAddRuneSelf(s[i]) {
i++
continue
}
r, size := utf8.DecodeRune(s[i:])
if enc.tryAddRuneError(r, size) {
i++
continue
}
enc.buf.Write(s[i : i+size])
i += size
}
}
// tryAddRuneSelf appends b if it is valid UTF-8 character represented in a single byte.
func (enc *consoleEncoder) tryAddRuneSelf(b byte) bool {
if b >= utf8.RuneSelf {
return false
}
if 0x20 <= b && b != '\\' && b != '"' {
enc.buf.AppendByte(b)
return true
}
switch b {
case '\\', '"':
enc.buf.AppendByte('\\')
enc.buf.AppendByte(b)
case '\n':
enc.buf.AppendByte('\\')
enc.buf.AppendByte('n')
case '\r':
enc.buf.AppendByte('\\')
enc.buf.AppendByte('r')
case '\t':
enc.buf.AppendByte('\\')
enc.buf.AppendByte('t')
default:
// Encode bytes < 0x20, except for the escape sequences above.
enc.buf.AppendString(`\u00`)
enc.buf.AppendByte(_hex[b>>4])
enc.buf.AppendByte(_hex[b&0xF])
}
return true
}
func (enc *consoleEncoder) closeOpenNamespaces() {
for i := 0; i < enc.openNamespaces; i++ {
enc.buf.AppendByte('}')
}
}
func (enc *consoleEncoder) tryAddRuneError(r rune, size int) bool {
if r == utf8.RuneError && size == 1 {
enc.buf.AppendString(`\ufffd`)
return true
}
return false
}
func (enc *consoleEncoder) AddComplex64(k string, v complex64) { enc.AddComplex128(k, complex128(v)) }
func (enc *consoleEncoder) AddFloat32(k string, v float32) { enc.AddFloat64(k, float64(v)) }
func (enc *consoleEncoder) AddInt(k string, v int) { enc.AddInt64(k, int64(v)) }
func (enc *consoleEncoder) AddInt32(k string, v int32) { enc.AddInt64(k, int64(v)) }
func (enc *consoleEncoder) AddInt16(k string, v int16) { enc.AddInt64(k, int64(v)) }
func (enc *consoleEncoder) AddInt8(k string, v int8) { enc.AddInt64(k, int64(v)) }
func (enc *consoleEncoder) AddUint(k string, v uint) { enc.AddUint64(k, uint64(v)) }
func (enc *consoleEncoder) AddUint32(k string, v uint32) { enc.AddUint64(k, uint64(v)) }
func (enc *consoleEncoder) AddUint16(k string, v uint16) { enc.AddUint64(k, uint64(v)) }
func (enc *consoleEncoder) AddUint8(k string, v uint8) { enc.AddUint64(k, uint64(v)) }
func (enc *consoleEncoder) AddUintptr(k string, v uintptr) { enc.AddUint64(k, uint64(v)) }
func (enc *consoleEncoder) AppendComplex64(v complex64) { enc.AppendComplex128(complex128(v)) }
func (enc *consoleEncoder) AppendFloat64(v float64) { enc.appendFloat(v, 64) }
func (enc *consoleEncoder) AppendFloat32(v float32) { enc.appendFloat(float64(v), 32) }
func (enc *consoleEncoder) AppendInt(v int) { enc.AppendInt64(int64(v)) }
func (enc *consoleEncoder) AppendInt32(v int32) { enc.AppendInt64(int64(v)) }
func (enc *consoleEncoder) AppendInt16(v int16) { enc.AppendInt64(int64(v)) }
func (enc *consoleEncoder) AppendInt8(v int8) { enc.AppendInt64(int64(v)) }
func (enc *consoleEncoder) AppendUint(v uint) { enc.AppendUint64(uint64(v)) }
func (enc *consoleEncoder) AppendUint32(v uint32) { enc.AppendUint64(uint64(v)) }
func (enc *consoleEncoder) AppendUint16(v uint16) { enc.AppendUint64(uint64(v)) }
func (enc *consoleEncoder) AppendUint8(v uint8) { enc.AppendUint64(uint64(v)) }
func (enc *consoleEncoder) AppendUintptr(v uintptr) { enc.AppendUint64(uint64(v)) }
const _hex = "0123456789abcdef"
type sliceArrayEncoder struct {
elems []interface{}
}
func (s *sliceArrayEncoder) AppendArray(v zapcore.ArrayMarshaler) error {
enc := &sliceArrayEncoder{}
err := v.MarshalLogArray(enc)
s.elems = append(s.elems, enc.elems)
return err
}
func (s *sliceArrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
m := zapcore.NewMapObjectEncoder()
err := v.MarshalLogObject(m)
s.elems = append(s.elems, m.Fields)
return err
}
func (s *sliceArrayEncoder) AppendReflected(v interface{}) error {
s.elems = append(s.elems, v)
return nil
}
func (s *sliceArrayEncoder) AppendBool(v bool) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendByteString(v []byte) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendComplex128(v complex128) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendComplex64(v complex64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendDuration(v time.Duration) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendFloat64(v float64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendFloat32(v float32) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt(v int) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt64(v int64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt32(v int32) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt16(v int16) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt8(v int8) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendString(v string) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendTime(v time.Time) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint(v uint) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint64(v uint64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint32(v uint32) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint16(v uint16) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint8(v uint8) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUintptr(v uintptr) { s.elems = append(s.elems, v) }

View File

@@ -0,0 +1,37 @@
package logger
import (
"os"
)
// LogFunctionMap can be added to, so that you can specify your own logging mechanism
// it has defaults for off, stdout, stderr.
var LogFunctionMap = map[string]func(*CompositeMultiHandler, *LogOptions){
// Do nothing - set the logger off
"off": func(c *CompositeMultiHandler, logOptions *LogOptions) {
// Only drop the results if there is a parent handler defined
if logOptions.HandlerWrap != nil {
for _, l := range logOptions.Levels {
c.SetHandler(logOptions.HandlerWrap.SetChild(NilHandler()), logOptions.ReplaceExistingHandler, l)
}
} else {
// Clear existing handler
c.SetHandlers(NilHandler(), logOptions)
}
},
// Do nothing - set the logger off
"": func(*CompositeMultiHandler, *LogOptions) {},
// Set the levels to stdout, replace existing
"stdout": func(c *CompositeMultiHandler, logOptions *LogOptions) {
if logOptions.Ctx != nil {
logOptions.SetExtendedOptions(
"noColor", !logOptions.Ctx.BoolDefault("log.colorize", true),
"smallDate", logOptions.Ctx.BoolDefault("log.smallDate", true))
}
c.SetTerminal(os.Stdout, logOptions)
},
// Set the levels to stderr output to terminal
"stderr": func(c *CompositeMultiHandler, logOptions *LogOptions) {
c.SetTerminal(os.Stderr, logOptions)
},
}

View File

@@ -2,64 +2,94 @@ package logger
import ( import (
"fmt" "fmt"
"time"
"github.com/revel/config" "github.com/revel/config"
"github.com/revel/log15"
"log"
"os"
) )
// The LogHandler defines the interface to handle the log records // The LogHandler defines the interface to handle the log records.
type ( type (
// The Multilogger reduces the number of exposed defined logging variables, // The Multilogger reduces the number of exposed defined logging variables,
// and allows the output to be easily refined // and allows the output to be easily refined.
MultiLogger interface { MultiLogger interface {
//log15.Logger // New returns a new Logger that has this logger's context plus the given context
//// New returns a new Logger that has this logger's context plus the given context
New(ctx ...interface{}) MultiLogger New(ctx ...interface{}) MultiLogger
//
//// SetHandler updates the logger to write records to the specified handler. // SetHandler updates the logger to write records to the specified handler.
SetHandler(h LogHandler) SetHandler(h LogHandler)
// Set the stack depth for the logger
SetStackDepth(int) MultiLogger SetStackDepth(int) MultiLogger
//
//// Log a message at the given level with context key/value pairs // Log a message at the given level with context key/value pairs
Debug(msg string, ctx ...interface{}) Debug(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters
Debugf(msg string, params ...interface{}) Debugf(msg string, params ...interface{})
// Log a message at the given level with context key/value pairs
Info(msg string, ctx ...interface{}) Info(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters
Infof(msg string, params ...interface{}) Infof(msg string, params ...interface{})
// Log a message at the given level with context key/value pairs
Warn(msg string, ctx ...interface{}) Warn(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters
Warnf(msg string, params ...interface{}) Warnf(msg string, params ...interface{})
// Log a message at the given level with context key/value pairs
Error(msg string, ctx ...interface{}) Error(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters
Errorf(msg string, params ...interface{}) Errorf(msg string, params ...interface{})
// Log a message at the given level with context key/value pairs
Crit(msg string, ctx ...interface{}) Crit(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters
Critf(msg string, params ...interface{}) Critf(msg string, params ...interface{})
//// Logs a message as an Crit and exits // Log a message at the given level with context key/value pairs and exits
Fatal(msg string, ctx ...interface{}) Fatal(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters and exits
Fatalf(msg string, params ...interface{}) Fatalf(msg string, params ...interface{})
//// Logs a message as an Crit and panics
// Log a message at the given level with context key/value pairs and panics
Panic(msg string, ctx ...interface{}) Panic(msg string, ctx ...interface{})
// Log a message at the given level formatting message with the parameters and panics
Panicf(msg string, params ...interface{}) Panicf(msg string, params ...interface{})
} }
// The log handler interface.
LogHandler interface { LogHandler interface {
log15.Handler Log(*Record) error
// log15.Handler
} }
// The log stack handler interface.
LogStackHandler interface { LogStackHandler interface {
LogHandler LogHandler
GetStack() int GetStack() int
} }
// The log handler interface which has child logs.
ParentLogHandler interface { ParentLogHandler interface {
SetChild(handler LogHandler) LogHandler SetChild(handler LogHandler) LogHandler
} }
// The log format interface.
LogFormat interface { LogFormat interface {
log15.Format Format(r *Record) []byte
} }
LogLevel log15.Lvl // The log level type.
RevelLogger struct { LogLevel int
log15.Logger
}
// Used for the callback to LogFunctionMap // Used for the callback to LogFunctionMap.
LogOptions struct { LogOptions struct {
Ctx *config.Context Ctx *config.Context
ReplaceExistingHandler bool ReplaceExistingHandler bool
@@ -67,133 +97,74 @@ type (
Levels []LogLevel Levels []LogLevel
ExtendedOptions map[string]interface{} ExtendedOptions map[string]interface{}
} }
// The log record.
Record struct {
Message string // The message
Time time.Time // The time
Level LogLevel // The level
Call CallStack // The call stack if built
Context ContextMap // The context
}
// The lazy structure to implement a function to be invoked only if needed.
Lazy struct {
Fn interface{} // the function
}
// Currently the only requirement for the callstack is to support the Formatter method
// which stack.Call does so we use that.
CallStack interface {
fmt.Formatter // Requirement
}
) )
// FormatFunc returns a new Format object which uses
// the given function to perform record formatting.
func FormatFunc(f func(*Record) []byte) LogFormat {
return formatFunc(f)
}
type formatFunc func(*Record) []byte
func (f formatFunc) Format(r *Record) []byte {
return f(r)
}
func NewRecord(message string, level LogLevel) *Record {
return &Record{Message: message, Context: ContextMap{}, Level: level}
}
const ( const (
LvlDebug = LogLevel(log15.LvlDebug) LvlCrit LogLevel = iota // Critical
LvlInfo = LogLevel(log15.LvlInfo) LvlError // Error
LvlWarn = LogLevel(log15.LvlWarn) LvlWarn // Warning
LvlError = LogLevel(log15.LvlError) LvlInfo // Information
LvlCrit = LogLevel(log15.LvlCrit) LvlDebug // Debug
) )
// A list of all the log levels // LvlAllList is a list of all the log levels.
var LvlAllList = []LogLevel{LvlDebug, LvlInfo, LvlWarn, LvlError, LvlCrit} var LvlAllList = []LogLevel{LvlDebug, LvlInfo, LvlWarn, LvlError, LvlCrit}
// The log function map can be added to, so that you can specify your own logging mechanism // Implements the ParentLogHandler.
var LogFunctionMap = map[string]func(*CompositeMultiHandler, *LogOptions){
// Do nothing - set the logger off
"off": func(c *CompositeMultiHandler, logOptions *LogOptions) {
// Only drop the results if there is a parent handler defined
if logOptions.HandlerWrap != nil {
for _, l := range logOptions.Levels {
c.SetHandler(logOptions.HandlerWrap.SetChild(NilHandler()), logOptions.ReplaceExistingHandler, l)
}
}
},
// Do nothing - set the logger off
"": func(*CompositeMultiHandler, *LogOptions) {},
// Set the levels to stdout, replace existing
"stdout": func(c *CompositeMultiHandler, logOptions *LogOptions) {
if logOptions.Ctx != nil {
logOptions.SetExtendedOptions(
"noColor", !logOptions.Ctx.BoolDefault("log.colorize", true),
"smallDate", logOptions.Ctx.BoolDefault("log.smallDate", true))
}
c.SetTerminal(os.Stdout, logOptions)
},
// Set the levels to stderr output to terminal
"stderr": func(c *CompositeMultiHandler, logOptions *LogOptions) {
c.SetTerminal(os.Stderr, logOptions)
},
}
// Set the systems default logger
// Default logs will be captured and handled by revel at level info
func SetDefaultLog(fromLog MultiLogger) {
log.SetOutput(loggerRewrite{Logger: fromLog, Level: log15.LvlInfo, hideDeprecated: true})
// No need to show date and time, that will be logged with revel
log.SetFlags(0)
}
// Formatted debug call
func (rl *RevelLogger) Debugf(msg string, param ...interface{}) {
rl.Debug(fmt.Sprintf(msg, param...))
}
// Formatted info call
func (rl *RevelLogger) Infof(msg string, param ...interface{}) {
rl.Info(fmt.Sprintf(msg, param...))
}
func (rl *RevelLogger) Warnf(msg string, param ...interface{}) {
rl.Warn(fmt.Sprintf(msg, param...))
}
func (rl *RevelLogger) Errorf(msg string, param ...interface{}) {
rl.Error(fmt.Sprintf(msg, param...))
}
func (rl *RevelLogger) Critf(msg string, param ...interface{}) {
rl.Crit(fmt.Sprintf(msg, param...))
}
func (rl *RevelLogger) Fatalf(msg string, param ...interface{}) {
rl.Crit(fmt.Sprintf(msg, param...))
os.Exit(1)
}
func (rl *RevelLogger) Panicf(msg string, param ...interface{}) {
rl.Crit(fmt.Sprintf(msg, param...))
panic(msg)
}
func (rl *RevelLogger) Fatal(msg string, ctx ...interface{}) {
rl.Crit(msg, ctx...)
os.Exit(1)
}
func (rl *RevelLogger) Panic(msg string, ctx ...interface{}) {
rl.Crit(msg, ctx...)
panic(msg)
}
// Override log15 method
func (rl *RevelLogger) New(ctx ...interface{}) MultiLogger {
old := &RevelLogger{Logger: rl.Logger.New(ctx...)}
return old
}
// Set the stack level to check for the caller
func (rl *RevelLogger) SetStackDepth(amount int) MultiLogger {
rl.Logger.SetStackDepth(amount) // Ignore the logger returned
return rl
}
// Create a new logger
func New(ctx ...interface{}) MultiLogger {
r := &RevelLogger{Logger: log15.New(ctx...)}
r.SetStackDepth(1)
return r
}
// Set the handler in the Logger
func (rl *RevelLogger) SetHandler(h LogHandler) {
rl.Logger.SetHandler(h)
}
type parentLogHandler struct { type parentLogHandler struct {
setChild func(handler LogHandler) LogHandler setChild func(handler LogHandler) LogHandler
} }
// Create a new parent log handler.
func NewParentLogHandler(callBack func(child LogHandler) LogHandler) ParentLogHandler { func NewParentLogHandler(callBack func(child LogHandler) LogHandler) ParentLogHandler {
return &parentLogHandler{callBack} return &parentLogHandler{callBack}
} }
// Sets the child of the log handler.
func (p *parentLogHandler) SetChild(child LogHandler) LogHandler { func (p *parentLogHandler) SetChild(child LogHandler) LogHandler {
return p.setChild(child) return p.setChild(child)
} }
// Create a new log options // Create a new log options.
func NewLogOptions(cfg *config.Context, replaceHandler bool, phandler ParentLogHandler, lvl ...LogLevel) (logOptions *LogOptions) { func NewLogOptions(cfg *config.Context, replaceHandler bool, phandler ParentLogHandler, lvl ...LogLevel) (logOptions *LogOptions) {
logOptions = &LogOptions{ logOptions = &LogOptions{
Ctx: cfg, Ctx: cfg,
ReplaceExistingHandler: replaceHandler, ReplaceExistingHandler: replaceHandler,
HandlerWrap: phandler, HandlerWrap: phandler,
Levels: lvl, Levels: lvl,
@@ -202,24 +173,30 @@ func NewLogOptions(cfg *config.Context, replaceHandler bool, phandler ParentLogH
return return
} }
// Assumes options will be an even number and have a string, value syntax // Assumes options will be an even number and have a string, value syntax.
func (l *LogOptions) SetExtendedOptions(options ...interface{}) { func (l *LogOptions) SetExtendedOptions(options ...interface{}) {
for x := 0; x < len(options); x += 2 { for x := 0; x < len(options); x += 2 {
l.ExtendedOptions[options[x].(string)] = options[x+1] l.ExtendedOptions[options[x].(string)] = options[x+1]
} }
} }
// Gets a string option with default.
func (l *LogOptions) GetStringDefault(option, value string) string { func (l *LogOptions) GetStringDefault(option, value string) string {
if v, found := l.ExtendedOptions[option]; found { if v, found := l.ExtendedOptions[option]; found {
return v.(string) return v.(string)
} }
return value return value
} }
// Gets an int option with default.
func (l *LogOptions) GetIntDefault(option string, value int) int { func (l *LogOptions) GetIntDefault(option string, value int) int {
if v, found := l.ExtendedOptions[option]; found { if v, found := l.ExtendedOptions[option]; found {
return v.(int) return v.(int)
} }
return value return value
} }
// Gets a boolean option with default.
func (l *LogOptions) GetBoolDefault(option string, value bool) bool { func (l *LogOptions) GetBoolDefault(option string, value bool) bool {
if v, found := l.ExtendedOptions[option]; found { if v, found := l.ExtendedOptions[option]; found {
return v.(bool) return v.(bool)

144
logger/revel_logger.go Normal file
View File

@@ -0,0 +1,144 @@
package logger
import (
"fmt"
"log"
"os"
"github.com/revel/log15"
)
// This type implements the MultiLogger.
type RevelLogger struct {
log15.Logger
}
// Set the systems default logger
// Default logs will be captured and handled by revel at level info.
func SetDefaultLog(fromLog MultiLogger) {
log.SetOutput(loggerRewrite{Logger: fromLog, Level: log15.LvlInfo, hideDeprecated: true})
// No need to show date and time, that will be logged with revel
log.SetFlags(0)
}
func (rl *RevelLogger) Debugf(msg string, param ...interface{}) {
rl.Debug(fmt.Sprintf(msg, param...))
}
// Print a formatted info message.
func (rl *RevelLogger) Infof(msg string, param ...interface{}) {
rl.Info(fmt.Sprintf(msg, param...))
}
// Print a formatted warn message.
func (rl *RevelLogger) Warnf(msg string, param ...interface{}) {
rl.Warn(fmt.Sprintf(msg, param...))
}
// Print a formatted error message.
func (rl *RevelLogger) Errorf(msg string, param ...interface{}) {
rl.Error(fmt.Sprintf(msg, param...))
}
// Print a formatted critical message.
func (rl *RevelLogger) Critf(msg string, param ...interface{}) {
rl.Crit(fmt.Sprintf(msg, param...))
}
// Print a formatted fatal message.
func (rl *RevelLogger) Fatalf(msg string, param ...interface{}) {
rl.Fatal(fmt.Sprintf(msg, param...))
}
// Print a formatted panic message.
func (rl *RevelLogger) Panicf(msg string, param ...interface{}) {
rl.Panic(fmt.Sprintf(msg, param...))
}
// Print a critical message and call os.Exit(1).
func (rl *RevelLogger) Fatal(msg string, ctx ...interface{}) {
rl.Crit(msg, ctx...)
os.Exit(1)
}
// Print a critical message and panic.
func (rl *RevelLogger) Panic(msg string, ctx ...interface{}) {
rl.Crit(msg, ctx...)
panic(msg)
}
// Override log15 method.
func (rl *RevelLogger) New(ctx ...interface{}) MultiLogger {
old := &RevelLogger{Logger: rl.Logger.New(ctx...)}
return old
}
// Set the stack level to check for the caller.
func (rl *RevelLogger) SetStackDepth(amount int) MultiLogger {
rl.Logger.SetStackDepth(amount) // Ignore the logger returned
return rl
}
// Create a new logger.
func New(ctx ...interface{}) MultiLogger {
r := &RevelLogger{Logger: log15.New(ctx...)}
r.SetStackDepth(0)
return r
}
// Set the handler in the Logger.
func (rl *RevelLogger) SetHandler(h LogHandler) {
rl.Logger.SetHandler(callHandler(h.Log))
}
// The function wrapper to implement the callback.
type callHandler func(r *Record) error
// Log implementation, reads the record and extracts the details from the log record
// Hiding the implementation.
func (c callHandler) Log(log *log15.Record) error {
ctx := log.Ctx
var ctxMap ContextMap
if len(ctx) > 0 {
ctxMap = make(ContextMap, len(ctx)/2)
for i := 0; i < len(ctx); i += 2 {
v := ctx[i]
key, ok := v.(string)
if !ok {
key = fmt.Sprintf("LOGGER_INVALID_KEY %v", v)
}
var value interface{}
if len(ctx) > i+1 {
value = ctx[i+1]
} else {
value = "LOGGER_VALUE_MISSING"
}
ctxMap[key] = value
}
} else {
ctxMap = make(ContextMap)
}
r := &Record{Message: log.Msg, Context: ctxMap, Time: log.Time, Level: LogLevel(log.Lvl), Call: CallStack(log.Call)}
return c(r)
}
// Internally used contextMap, allows conversion of map to map[string]string.
type ContextMap map[string]interface{}
// Convert the context map to be string only values, any non string values are ignored.
func (m ContextMap) StringMap() (newMap map[string]string) {
if m != nil {
newMap = map[string]string{}
for key, value := range m {
if svalue, isstring := value.(string); isstring {
newMap[key] = svalue
}
}
}
return
}
func (m ContextMap) Add(key string, value interface{}) {
m[key] = value
}

View File

@@ -2,8 +2,8 @@ package logger
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"github.com/revel/log15"
"reflect" "reflect"
"strconv" "strconv"
"sync" "sync"
@@ -18,69 +18,66 @@ const (
errorKey = "REVEL_ERROR" errorKey = "REVEL_ERROR"
) )
var ( var levelString = map[LogLevel]string{
// Name the log level LvlDebug: "DEBUG",
toRevel = map[log15.Lvl]string{log15.LvlDebug: "DEBUG", LvlInfo: "INFO", LvlWarn: "WARN", LvlError: "ERROR", LvlCrit: "CRIT",
log15.LvlInfo: "INFO", log15.LvlWarn: "WARN", log15.LvlError: "ERROR", log15.LvlCrit: "CRIT"} }
)
// Outputs to the terminal in a format like below // Outputs to the terminal in a format like below
// INFO 09:11:32 server-engine.go:169: Request Stats // INFO 09:11:32 server-engine.go:169: Request Stats.
func TerminalFormatHandler(noColor bool, smallDate bool) LogFormat { func TerminalFormatHandler(noColor bool, smallDate bool) LogFormat {
dateFormat := termTimeFormat dateFormat := termTimeFormat
if smallDate { if smallDate {
dateFormat = termSmallTimeFormat dateFormat = termSmallTimeFormat
} }
return log15.FormatFunc(func(r *log15.Record) []byte { return FormatFunc(func(r *Record) []byte {
// Bash coloring http://misc.flogisoft.com/bash/tip_colors_and_formatting // Bash coloring http://misc.flogisoft.com/bash/tip_colors_and_formatting
var color = 0 color := 0
switch r.Lvl { switch r.Level {
case log15.LvlCrit: case LvlCrit:
// Magenta // Magenta
color = 35 color = 35
case log15.LvlError: case LvlError:
// Red // Red
color = 31 color = 31
case log15.LvlWarn: case LvlWarn:
// Yellow // Yellow
color = 33 color = 33
case log15.LvlInfo: case LvlInfo:
// Green // Green
color = 32 color = 32
case log15.LvlDebug: case LvlDebug:
// Cyan // Cyan
color = 36 color = 36
} }
b := &bytes.Buffer{} b := &bytes.Buffer{}
caller := findInContext("caller", r.Ctx) caller, _ := r.Context["caller"].(string)
module := findInContext("module", r.Ctx) module, _ := r.Context["module"].(string)
if noColor == false && color > 0 { if !noColor && color > 0 {
if len(module) > 0 { if len(module) > 0 {
fmt.Fprintf(b, "\x1b[%dm%-5s\x1b[0m %s %6s %13s: %-40s ", color, toRevel[r.Lvl], r.Time.Format(dateFormat), module, caller, r.Msg) fmt.Fprintf(b, "\x1b[%dm%-5s\x1b[0m %s %6s %13s: %-40s ", color, levelString[r.Level], r.Time.Format(dateFormat), module, caller, r.Message)
} else { } else {
fmt.Fprintf(b, "\x1b[%dm%-5s\x1b[0m %s %13s: %-40s ", color, toRevel[r.Lvl], r.Time.Format(dateFormat), caller, r.Msg) fmt.Fprintf(b, "\x1b[%dm%-5s\x1b[0m %s %13s: %-40s ", color, levelString[r.Level], r.Time.Format(dateFormat), caller, r.Message)
} }
} else { } else {
fmt.Fprintf(b, "%-5s %s %6s %13s: %-40s", toRevel[r.Lvl], r.Time.Format(dateFormat), module, caller, r.Msg) fmt.Fprintf(b, "%-5s %s %6s %13s: %-40s", levelString[r.Level], r.Time.Format(dateFormat), module, caller, r.Message)
} }
for i := 0; i < len(r.Ctx); i += 2 { i := 0
for k, v := range r.Context {
if i != 0 { if i != 0 {
b.WriteByte(' ') b.WriteByte(' ')
} }
i++
k, ok := r.Ctx[i].(string) if k == "module" || k == "caller" {
if k == "caller" || k == "fn" || k == "module" {
continue continue
} }
v := formatLogfmtValue(r.Ctx[i+1])
if !ok { v := formatLogfmtValue(v)
k, v = errorKey, formatLogfmtValue(k)
}
// TODO: we should probably check that all of your key bytes aren't invalid // TODO: we should probably check that all of your key bytes aren't invalid
if noColor == false && color > 0 { if !noColor && color > 0 {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m=%s", color, k, v) fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m=%s", color, k, v)
} else { } else {
b.WriteString(k) b.WriteString(k)
@@ -94,17 +91,8 @@ func TerminalFormatHandler(noColor bool, smallDate bool) LogFormat {
return b.Bytes() return b.Bytes()
}) })
} }
func findInContext(key string, ctx []interface{}) string {
for i := 0; i < len(ctx); i += 2 {
k := ctx[i].(string)
if key == k {
return formatLogfmtValue(ctx[i+1])
}
}
return ""
}
// formatValue formats a value for serialization // formatValue formats a value for serialization.
func formatLogfmtValue(value interface{}) string { func formatLogfmtValue(value interface{}) string {
if value == nil { if value == nil {
return "nil" return "nil"
@@ -132,6 +120,8 @@ func formatLogfmtValue(value interface{}) string {
return escapeString(fmt.Sprintf("%+v", value)) return escapeString(fmt.Sprintf("%+v", value))
} }
} }
// Format the value in json format.
func formatShared(value interface{}) (result interface{}) { func formatShared(value interface{}) (result interface{}) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@@ -158,10 +148,12 @@ func formatShared(value interface{}) (result interface{}) {
} }
} }
// A reusuable buffer for outputting data.
var stringBufPool = sync.Pool{ var stringBufPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) }, New: func() interface{} { return new(bytes.Buffer) },
} }
// Escape the string when needed.
func escapeString(s string) string { func escapeString(s string) string {
needsQuotes := false needsQuotes := false
needsEscape := false needsEscape := false
@@ -173,7 +165,7 @@ func escapeString(s string) string {
needsEscape = true needsEscape = true
} }
} }
if needsEscape == false && needsQuotes == false { if !needsEscape && !needsQuotes {
return s return s
} }
e := stringBufPool.Get().(*bytes.Buffer) e := stringBufPool.Get().(*bytes.Buffer)
@@ -204,3 +196,50 @@ func escapeString(s string) string {
stringBufPool.Put(e) stringBufPool.Put(e)
return ret return ret
} }
// JSONFormatEx formats log records as JSON objects. If pretty is true,
// records will be pretty-printed. If lineSeparated is true, records
// will be logged with a new line between each record.
func JSONFormatEx(pretty, lineSeparated bool) LogFormat {
jsonMarshal := json.Marshal
if pretty {
jsonMarshal = func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}
}
return FormatFunc(func(r *Record) []byte {
props := make(map[string]interface{})
props["t"] = r.Time
props["lvl"] = levelString[r.Level]
props["msg"] = r.Message
for k, v := range r.Context {
props[k] = formatJSONValue(v)
}
b, err := jsonMarshal(props)
if err != nil {
b, _ = jsonMarshal(map[string]string{
errorKey: err.Error(),
})
return b
}
if lineSeparated {
b = append(b, '\n')
}
return b
})
}
func formatJSONValue(value interface{}) interface{} {
value = formatShared(value)
switch value.(type) {
case int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, string:
return value
default:
return fmt.Sprintf("%+v", value)
}
}

View File

@@ -1,25 +1,31 @@
package logger package logger
import ( import (
"github.com/revel/config" "log"
"github.com/revel/log15" "github.com/revel/log15"
"gopkg.in/stack.v0" "gopkg.in/stack.v0"
"log"
"os"
"path/filepath"
"strings"
) )
// Utility package to make existing logging backwards compatible // Utility package to make existing logging backwards compatible.
var ( var (
// Convert the string to LogLevel // Convert the string to LogLevel.
toLevel = map[string]LogLevel{"debug": LogLevel(log15.LvlDebug), toLevel = map[string]LogLevel{
"info": LogLevel(log15.LvlInfo), "request": LogLevel(log15.LvlInfo), "warn": LogLevel(log15.LvlWarn), "debug": LogLevel(log15.LvlDebug),
"info": LogLevel(log15.LvlInfo), "request": LogLevel(log15.LvlInfo), "warn": LogLevel(log15.LvlWarn),
"error": LogLevel(log15.LvlError), "crit": LogLevel(log15.LvlCrit), "error": LogLevel(log15.LvlError), "crit": LogLevel(log15.LvlCrit),
"trace": LogLevel(log15.LvlDebug), // TODO trace is deprecated, replaced by debug "trace": LogLevel(log15.LvlDebug), // TODO trace is deprecated, replaced by debug
} }
) )
const (
// The test mode flag overrides the default log level and shows only errors.
TestModeFlag = "testModeFlag"
// The special use flag enables showing messages when the logger is setup.
SpecialUseFlag = "specialUseFlag"
)
// Returns the logger for the name.
func GetLogger(name string, logger MultiLogger) (l *log.Logger) { func GetLogger(name string, logger MultiLogger) (l *log.Logger) {
switch name { switch name {
case "trace": // TODO trace is deprecated, replaced by debug case "trace": // TODO trace is deprecated, replaced by debug
@@ -37,214 +43,42 @@ func GetLogger(name string, logger MultiLogger) (l *log.Logger) {
} }
return l return l
} }
// Get all handlers based on the Config (if available) // Used by the initFilterLog to handle the filters.
func InitializeFromConfig(basePath string, config *config.Context) (c *CompositeMultiHandler) { var logFilterList = []struct {
// If running in test mode suppress anything that is not an error LogPrefix, LogSuffix string
if config != nil && config.BoolDefault("testModeFlag", false) { parentHandler func(map[string]interface{}) ParentLogHandler
config.SetOption("log.info.output", "off") }{{
config.SetOption("log.debug.output", "off") "log.", ".filter",
config.SetOption("log.warn.output", "off") func(keyMap map[string]interface{}) ParentLogHandler {
config.SetOption("log.error.output", "stderr") return NewParentLogHandler(func(child LogHandler) LogHandler {
config.SetOption("log.crit.output", "stderr") return MatchMapHandler(keyMap, child)
} })
},
}, {
"log.", ".nfilter",
func(keyMap map[string]interface{}) ParentLogHandler {
return NewParentLogHandler(func(child LogHandler) LogHandler {
return NotMatchMapHandler(keyMap, child)
})
},
}}
// If the configuration has an all option we can skip some // This structure and method will handle the old output format and log it to the new format.
c, _ = NewCompositeMultiHandler()
// Filters are assigned first, non filtered items override filters
initAllLog(c, basePath, config)
initLogLevels(c, basePath, config)
if c.CriticalHandler == nil && c.ErrorHandler != nil {
c.CriticalHandler = c.ErrorHandler
}
initFilterLog(c, basePath, config)
if c.CriticalHandler == nil && c.ErrorHandler != nil {
c.CriticalHandler = c.ErrorHandler
}
initRequestLog(c, basePath, config)
return c
}
// Init the log.all configuration options
func initAllLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
if config != nil {
extraLogFlag := config.BoolDefault("specialUseFlag", false)
if output, found := config.String("log.all.output"); found {
// Set all output for the specified handler
if extraLogFlag {
log.Printf("Adding standard handler for levels to >%s< ", output)
}
initHandlerFor(c, output, basePath, NewLogOptions(config, true, nil, LvlAllList...))
}
}
}
// Init the filter options
// log.all.filter ....
// log.error.filter ....
func initFilterLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
if config != nil {
extraLogFlag := config.BoolDefault("specialUseFlag", false)
// The commands to use
logFilterList := []struct {
LogPrefix, LogSuffix string
parentHandler func(map[string]interface{}) ParentLogHandler
}{{
"log.", ".filter",
func(keyMap map[string]interface{}) ParentLogHandler {
return NewParentLogHandler(func(child LogHandler) LogHandler {
return MatchMapHandler(keyMap, child)
})
},
}, {
"log.", ".nfilter",
func(keyMap map[string]interface{}) ParentLogHandler {
return NewParentLogHandler(func(child LogHandler) LogHandler {
return NotMatchMapHandler(keyMap, child)
})
},
}}
for _, logFilter := range logFilterList {
// Init for all filters
for _, name := range []string{"all", "debug", "info", "warn", "error", "crit",
"trace", // TODO trace is deprecated
} {
optionList := config.Options(logFilter.LogPrefix + name + logFilter.LogSuffix)
for _, option := range optionList {
splitOptions := strings.Split(option, ".")
keyMap := map[string]interface{}{}
for x := 3; x < len(splitOptions); x += 2 {
keyMap[splitOptions[x]] = splitOptions[x+1]
}
phandler := logFilter.parentHandler(keyMap)
if extraLogFlag {
log.Printf("Adding key map handler %s %s output %s", option, name, config.StringDefault(option, ""))
}
if name == "all" {
initHandlerFor(c, config.StringDefault(option, ""), basePath, NewLogOptions(config, false, phandler))
} else {
initHandlerFor(c, config.StringDefault(option, ""), basePath, NewLogOptions(config, false, phandler, toLevel[name]))
}
}
}
}
}
}
// Init the log.error, log.warn etc configuration options
func initLogLevels(c *CompositeMultiHandler, basePath string, config *config.Context) {
for _, name := range []string{"debug", "info", "warn", "error", "crit",
"trace", // TODO trace is deprecated
} {
if config != nil {
extraLogFlag := config.BoolDefault("specialUseFlag", false)
output, found := config.String("log." + name + ".output")
if found {
if extraLogFlag {
log.Printf("Adding standard handler %s output %s", name, output)
}
initHandlerFor(c, output, basePath, NewLogOptions(config, true, nil, toLevel[name]))
}
// Gets the list of options with said prefix
} else {
initHandlerFor(c, "stderr", basePath, NewLogOptions(config, true, nil, toLevel[name]))
}
}
}
// Init the request log options
func initRequestLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
// Request logging to a separate output handler
// This takes the InfoHandlers and adds a MatchAbHandler handler to it to direct
// context with the word "section=requestlog" to that handler.
// Note if request logging is not enabled the MatchAbHandler will not be added and the
// request log messages will be sent out the INFO handler
outputRequest := "stdout"
if config != nil {
outputRequest = config.StringDefault("log.request.output", "")
}
oldInfo := c.InfoHandler
c.InfoHandler = nil
if outputRequest != "" {
initHandlerFor(c, outputRequest, basePath, NewLogOptions(config, false, nil, LvlInfo))
}
if c.InfoHandler != nil || oldInfo != nil {
if c.InfoHandler == nil {
c.InfoHandler = oldInfo
} else {
c.InfoHandler = MatchAbHandler("section", "requestlog", c.InfoHandler, oldInfo)
}
}
}
// Returns a handler for the level using the output string
// Accept formats for output string are
// LogFunctionMap[value] callback function
// `stdout` `stderr` `full/file/path/to/location/app.log` `full/file/path/to/location/app.json`
func initHandlerFor(c *CompositeMultiHandler, output, basePath string, options *LogOptions) {
if options.Ctx != nil {
options.SetExtendedOptions(
"noColor", !options.Ctx.BoolDefault("log.colorize", true),
"smallDate", options.Ctx.BoolDefault("log.smallDate", true),
"maxSize", options.Ctx.IntDefault("log.maxsize", 1024*10),
"maxAge", options.Ctx.IntDefault("log.maxage", 14),
"maxBackups", options.Ctx.IntDefault("log.maxbackups", 14),
"compressBackups", !options.Ctx.BoolDefault("log.compressBackups", true),
)
}
output = strings.TrimSpace(output)
if funcHandler, found := LogFunctionMap[output]; found {
funcHandler(c, options)
} else {
switch output {
case "":
fallthrough
case "off":
// No handler, discard data
default:
// Write to file specified
if !filepath.IsAbs(output) {
output = filepath.Join(basePath, output)
}
if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {
log.Panic(err)
}
if strings.HasSuffix(output, "json") {
c.SetJsonFile(output, options)
} else {
// Override defaults for a terminal file
options.SetExtendedOptions("noColor", true)
options.SetExtendedOptions("smallDate", false)
c.SetTerminalFile(output, options)
}
}
}
return
}
// This structure and method will handle the old output format and log it to the new format
type loggerRewrite struct { type loggerRewrite struct {
Logger MultiLogger Logger MultiLogger
Level log15.Lvl Level log15.Lvl
hideDeprecated bool hideDeprecated bool
} }
var log_deprecated = []byte("* LOG DEPRECATED * ") // The message indicating that a logger is using a deprecated log mechanism.
var logDeprecated = []byte("* LOG DEPRECATED * ")
// Implements the Write of the logger.
func (lr loggerRewrite) Write(p []byte) (n int, err error) { func (lr loggerRewrite) Write(p []byte) (n int, err error) {
if !lr.hideDeprecated { if !lr.hideDeprecated {
p = append(log_deprecated, p...) p = append(logDeprecated, p...)
} }
n = len(p) n = len(p)
if len(p) > 0 && p[n-1] == '\n' { if len(p) > 0 && p[n-1] == '\n' {
@@ -270,7 +104,7 @@ func (lr loggerRewrite) Write(p []byte) (n int, err error) {
// For logging purposes the call stack can be used to record the stack trace of a bad error // For logging purposes the call stack can be used to record the stack trace of a bad error
// simply pass it as a context field in your log statement like // simply pass it as a context field in your log statement like
// `controller.Log.Critc("This should not occur","stack",revel.NewCallStack())` // `controller.Log.Crit("This should not occur","stack",revel.NewCallStack())`.
func NewCallStack() interface{} { func NewCallStack() interface{} {
return stack.Trace() return stack.Trace()
} }

111
logger/wrap_handlers.go Normal file
View File

@@ -0,0 +1,111 @@
package logger
// FuncHandler returns a Handler that logs records with the given
// function.
import (
"fmt"
"reflect"
"sync"
"time"
)
// Error is used for constant errors.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
const (
ErrNotFunc Error = "not a function"
ErrTakesArgs Error = "takes arguments"
ErrNoReturn Error = "no return value"
)
// Function handler wraps the declared function and returns the handler for it.
func FuncHandler(fn func(r *Record) error) LogHandler {
return funcHandler(fn)
}
// The type declaration for the function.
type funcHandler func(r *Record) error
// The implementation of the Log.
func (h funcHandler) Log(r *Record) error {
return h(r)
}
// This function allows you to do a full declaration for the log,
// it is recommended you use FuncHandler instead.
func HandlerFunc(log func(message string, time time.Time, level LogLevel, call CallStack, context ContextMap) error) LogHandler {
return remoteHandler(log)
}
// The type used for the HandlerFunc.
type remoteHandler func(message string, time time.Time, level LogLevel, call CallStack, context ContextMap) error
// The Log implementation.
func (c remoteHandler) Log(record *Record) error {
return c(record.Message, record.Time, record.Level, record.Call, record.Context)
}
// SyncHandler can be wrapped around a handler to guarantee that
// only a single Log operation can proceed at a time. It's necessary
// for thread-safe concurrent writes.
func SyncHandler(h LogHandler) LogHandler {
var mu sync.Mutex
return FuncHandler(func(r *Record) error {
defer mu.Unlock()
mu.Lock()
return h.Log(r)
})
}
// LazyHandler writes all values to the wrapped handler after evaluating
// any lazy functions in the record's context. It is already wrapped
// around StreamHandler and SyslogHandler in this library, you'll only need
// it if you write your own Handler.
func LazyHandler(h LogHandler) LogHandler {
return FuncHandler(func(r *Record) error {
for k, v := range r.Context {
if lz, ok := v.(Lazy); ok {
_, err := evaluateLazy(lz)
if err != nil {
r.Context[errorKey] = "bad lazy " + k
}
}
}
return h.Log(r)
})
}
func evaluateLazy(lz Lazy) (interface{}, error) {
t := reflect.TypeOf(lz.Fn)
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("%w %+v", ErrNotFunc, lz.Fn)
}
if t.NumIn() > 0 {
return nil, fmt.Errorf("%w %+v", ErrTakesArgs, lz.Fn)
}
if t.NumOut() == 0 {
return nil, fmt.Errorf("%w %+v", ErrNoReturn, lz.Fn)
}
value := reflect.ValueOf(lz.Fn)
results := value.Call([]reflect.Value{})
if len(results) == 1 {
return results[0].Interface(), nil
}
values := make([]interface{}, len(results))
for i, v := range results {
values[i] = v.Interface()
}
return values, nil
}

10
model/command/build.go Normal file
View File

@@ -0,0 +1,10 @@
package command
type (
Build struct {
ImportCommand
TargetPath string `short:"t" long:"target-path" description:"Path to target folder. Folder will be completely deleted if it exists" required:"false"`
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
CopySource bool `short:"s" long:"include-source" description:"Copy the source code as well"`
}
)

7
model/command/clean.go Normal file
View File

@@ -0,0 +1,7 @@
package command
type (
Clean struct {
ImportCommand
}
)

View File

@@ -0,0 +1,7 @@
package command
type (
ImportCommand struct {
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
}
)

12
model/command/new.go Normal file
View File

@@ -0,0 +1,12 @@
package command
type (
New struct {
ImportCommand
SkeletonPath string `short:"s" long:"skeleton" description:"Path to skeleton folder (Must exist on GO PATH)" required:"false"`
Package string `short:"p" long:"package" description:"The package name, this becomes the repfix to the app name, if defined vendored is set to true" required:"false"`
NotVendored bool `long:"no-vendor" description:"True if project should not be configured with a go.mod, this requires you to have the project on the GOPATH, this is only compatible with go versions v1.12 or older"`
Run bool `short:"r" long:"run" description:"True if you want to run the application right away"`
Callback func() error
}
)

10
model/command/package.go Normal file
View File

@@ -0,0 +1,10 @@
package command
type (
Package struct {
ImportCommand
TargetPath string `short:"t" long:"target-path" description:"Full path and filename of target package to deploy" required:"false"`
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
CopySource bool `short:"s" long:"include-source" description:"Copy the source code as well"`
}
)

10
model/command/run.go Normal file
View File

@@ -0,0 +1,10 @@
package command
type (
Run struct {
ImportCommand
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
Port int `short:"p" long:"port" default:"-1" description:"The port to listen" `
NoProxy bool `short:"n" long:"no-proxy" description:"True if proxy server should not be started. This will only update the main and routes files on change"`
}
)

View File

@@ -0,0 +1,9 @@
package command
type (
Test struct {
ImportCommand
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
Function string `short:"f" long:"suite-function" description:"The suite.function"`
}
)

7
model/command/version.go Normal file
View File

@@ -0,0 +1,7 @@
package command
type (
Version struct {
ImportCommand
}
)

View File

@@ -1,17 +1,23 @@
package model package model
// The constants
import ( import (
"fmt" "fmt"
"github.com/revel/cmd/logger" "go/ast"
"github.com/revel/cmd/utils"
"go/build" "go/build"
"go/parser"
"go/token"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/revel/cmd"
"github.com/revel/cmd/model/command"
"github.com/revel/cmd/utils"
) )
// The constants.
const ( const (
NEW COMMAND = iota + 1 NEW COMMAND = iota + 1
RUN RUN
@@ -22,70 +28,45 @@ const (
VERSION VERSION
) )
const (
ErrImportInvalid Error = "invalid import path, working dir is in GOPATH root"
ErrUnableToImport Error = "unable to determine import path from"
)
type ( type (
// The Revel command type // The Revel command type.
COMMAND int COMMAND int
// The Command config for the line input // The Command config for the line input.
CommandConfig struct { CommandConfig struct {
Index COMMAND // The index Index COMMAND // The index
Verbose []bool `short:"v" long:"debug" description:"If set the logger is set to verbose"` // True if debug is active Verbose []bool `short:"v" long:"debug" description:"If set the logger is set to verbose"` // True if debug is active
HistoricMode bool `long:"historic-run-mode" description:"If set the runmode is passed a string not json"` // True if debug is active FrameworkVersion *Version // The framework version
ImportPath string // The import path (relative to a GOPATH) CommandVersion *Version // The command version
GoPath string // The GoPath HistoricMode bool `long:"historic-run-mode" description:"If set the runmode is passed a string not json"` // True if debug is active
GoCmd string // The full path to the go executable ImportPath string // The import path (relative to a GOPATH)
SrcRoot string // The source root GoPath string // The GoPath
AppPath string // The application path (absolute) GoCmd string // The full path to the go executable
AppName string // The application name // SrcRoot string // The source root
PackageResolver func(pkgName string) error // a packge resolver for the config AppPath string // The application path (absolute)
BuildFlags []string `short:"X" long:"build-flags" description:"These flags will be used when building the application. May be specified multiple times, only applicable for Build, Run, Package, Test commands"` AppName string // The application name
// The new command HistoricBuildMode bool `long:"historic-build-mode" description:"If set the code is scanned using the original parsers, not the go.1.11+"` // True if debug is active
New struct { Vendored bool // True if the application is vendored
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"` PackageResolver func(pkgName string) error // a package resolver for the config
SkeletonPath string `short:"s" long:"skeleton" description:"Path to skeleton folder (Must exist on GO PATH)" required:"false"` BuildFlags []string `short:"X" long:"build-flags" description:"These flags will be used when building the application. May be specified multiple times, only applicable for Build, Run, Package, Test commands"`
Vendored bool `short:"V" long:"vendor" description:"True if project should contain a vendor folder to be initialized. Creates the vendor folder and the 'Gopkg.toml' file in the root"` GoModFlags []string `long:"gomod-flags" description:"These flags will execute go mod commands for each flag, this happens during the build process"`
Run bool `short:"r" long:"run" description:"True if you want to run the application right away"` New command.New `command:"new"`
} `command:"new"` Build command.Build `command:"build"`
// The build command Run command.Run `command:"run"`
Build struct { Package command.Package `command:"package"`
TargetPath string `short:"t" long:"target-path" description:"Path to target folder. Folder will be completely deleted if it exists" required:"false"` Clean command.Clean `command:"clean"`
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"` Test command.Test `command:"test"`
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"` Version command.Version `command:"version"`
CopySource bool `short:"s" long:"include-source" description:"Copy the source code as well"`
} `command:"build"`
// The run command
Run struct {
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
Port int `short:"p" long:"port" default:"-1" description:"The port to listen" `
NoProxy bool `short:"n" long:"no-proxy" description:"True if proxy server should not be started. This will only update the main and routes files on change"`
} `command:"run"`
// The package command
Package struct {
TargetPath string `short:"t" long:"target-path" description:"Full path and filename of target package to deploy" required:"false"`
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
CopySource bool `short:"s" long:"include-source" description:"Copy the source code as well"`
} `command:"package"`
// The clean command
Clean struct {
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
} `command:"clean"`
// The test command
Test struct {
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
Function string `short:"f" long:"suite-function" description:"The suite.function"`
} `command:"test"`
// The version command
Version struct {
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
} `command:"version"`
} }
) )
// Updates the import path depending on the command // Updates the import path depending on the command.
func (c *CommandConfig) UpdateImportPath() bool { func (c *CommandConfig) UpdateImportPath() error {
var importPath string var importPath string
required := true required := true
switch c.Index { switch c.Index {
@@ -93,20 +74,26 @@ func (c *CommandConfig) UpdateImportPath() bool {
importPath = c.New.ImportPath importPath = c.New.ImportPath
case RUN: case RUN:
importPath = c.Run.ImportPath importPath = c.Run.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath, "go.mod"))
case BUILD: case BUILD:
importPath = c.Build.ImportPath importPath = c.Build.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath, "go.mod"))
case PACKAGE: case PACKAGE:
importPath = c.Package.ImportPath importPath = c.Package.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath, "go.mod"))
case CLEAN: case CLEAN:
importPath = c.Clean.ImportPath importPath = c.Clean.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath, "go.mod"))
case TEST: case TEST:
importPath = c.Test.ImportPath importPath = c.Test.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath, "go.mod"))
case VERSION: case VERSION:
importPath = c.Version.ImportPath importPath = c.Version.ImportPath
required = false required = false
} }
if len(importPath) == 0 || filepath.IsAbs(importPath) || importPath[0] == '.' { if len(importPath) == 0 || filepath.IsAbs(importPath) || importPath[0] == '.' {
utils.Logger.Info("Import path is absolute or not specified", "path", importPath)
// Try to determine the import path from the GO paths and the command line // Try to determine the import path from the GO paths and the command line
currentPath, err := os.Getwd() currentPath, err := os.Getwd()
if len(importPath) > 0 { if len(importPath) > 0 {
@@ -116,7 +103,6 @@ func (c *CommandConfig) UpdateImportPath() bool {
} }
// For an absolute path // For an absolute path
currentPath, _ = filepath.Abs(importPath) currentPath, _ = filepath.Abs(importPath)
} }
if err == nil { if err == nil {
@@ -125,9 +111,12 @@ func (c *CommandConfig) UpdateImportPath() bool {
if strings.HasPrefix(currentPath, path) && len(currentPath) > len(path)+1 { if strings.HasPrefix(currentPath, path) && len(currentPath) > len(path)+1 {
importPath = currentPath[len(path)+1:] importPath = currentPath[len(path)+1:]
// Remove the source from the path if it is there // Remove the source from the path if it is there
if len(importPath) > 4 && strings.ToLower(importPath[0:4]) == "src/" { if len(importPath) > 4 && (strings.ToLower(importPath[0:4]) == "src/" || strings.ToLower(importPath[0:4]) == "src\\") {
importPath = importPath[4:] importPath = importPath[4:]
} else if importPath == "src" { } else if importPath == "src" {
if c.Index != VERSION {
return ErrImportInvalid
}
importPath = "" importPath = ""
} }
utils.Logger.Info("Updated import path", "path", importPath) utils.Logger.Info("Updated import path", "path", importPath)
@@ -137,77 +126,144 @@ func (c *CommandConfig) UpdateImportPath() bool {
} }
c.ImportPath = importPath c.ImportPath = importPath
utils.Logger.Info("Returned import path", "path", importPath, "buildpath", build.Default.GOPATH) // We need the source root determined at this point to check the setversions
return (len(importPath) > 0 || !required) if err := c.initAppFolder(); err != nil {
utils.Logger.Error("Error initing app folder", "error", err)
}
utils.Logger.Info("Returned import path", "path", importPath)
if required && c.Index != NEW {
if err := c.SetVersions(); err != nil {
utils.Logger.Panic("Failed to fetch revel versions", "error", err)
}
if err := c.FrameworkVersion.CompatibleFramework(c); err != nil {
utils.Logger.Fatal("Compatibility Error", "message", err,
"Revel framework version", c.FrameworkVersion.String(), "Revel tool version", c.CommandVersion.String())
}
utils.Logger.Info("Revel versions", "revel-tool", c.CommandVersion.String(), "Revel Framework", c.FrameworkVersion.String())
}
if !required {
return nil
}
if len(importPath) == 0 {
return fmt.Errorf("%w: %s", ErrUnableToImport, importPath)
}
return nil
} }
// Used to initialize the package resolver func (c *CommandConfig) initAppFolder() (err error) {
func (c *CommandConfig) InitPackageResolver() { utils.Logger.Info("initAppFolder", "vendored", c.Vendored, "build-gopath", build.Default.GOPATH, "gopath-env", os.Getenv("GOPATH"))
useVendor := utils.DirExists(filepath.Join(c.AppPath, "vendor"))
if c.Index == NEW && c.New.Vendored { // check for go executable
useVendor = true c.GoCmd, err = exec.LookPath("go")
if err != nil {
utils.Logger.Fatal("Go executable not found in PATH.")
} }
utils.Logger.Info("InitPackageResolver", "useVendor", useVendor, "path", c.AppPath)
var ( // First try to determine where the application is located - this should be the import value
depPath string appFolder := c.ImportPath
err error wd, _ := os.Getwd()
) if len(appFolder) == 0 {
// We will assume the working directory is the appFolder
if useVendor { appFolder = wd
utils.Logger.Info("Vendor folder detected, scanning for deps in path") } else if strings.LastIndex(wd, appFolder) == len(wd)-len(appFolder) {
depPath, err = exec.LookPath("dep") // Check for existence of an /app folder
if err != nil { if utils.Exists(filepath.Join(wd, "app")) {
// Do not halt build unless a new package needs to be imported appFolder = wd
utils.Logger.Fatal("Build: `dep` executable not found in PATH, but vendor folder detected." + } else {
"Packages can only be added automatically to the vendor folder using the `dep` tool. " + appFolder = filepath.Join(wd, appFolder)
"You can install the `dep` tool by doing a `go get -u github.com/golang/dep/cmd/dep`")
} }
} else if !filepath.IsAbs(appFolder) {
appFolder = filepath.Join(wd, appFolder)
} }
utils.Logger.Info("Determined app folder to be", "appfolder", appFolder, "working", wd, "importPath", c.ImportPath)
// Use app folder to read the go.mod if it exists and extract the package information
goModFile := filepath.Join(appFolder, "go.mod")
utils.Logger.Info("Checking gomod, extracting from file", "path", goModFile, "exists", utils.Exists(goModFile))
if utils.Exists(goModFile) {
c.Vendored = true
utils.Logger.Info("Found go mod, extracting from file", "path", goModFile)
file, err := ioutil.ReadFile(goModFile)
if err != nil {
return err
}
for _, line := range strings.Split(string(file), "\n") {
if strings.Index(line, "module ") == 0 {
c.ImportPath = strings.TrimSpace(strings.Split(line, "module")[1])
c.AppPath = appFolder
// c.SrcRoot = appFolder
utils.Logger.Info("Set application path and package based on go mod", "path", c.AppPath)
return nil
}
}
// c.SrcRoot = appFolder
c.AppPath = appFolder
} else if c.Index != NEW || (c.Index == NEW && c.New.NotVendored) {
workingDir, _ := os.Getwd()
goPathList := filepath.SplitList(c.GoPath)
bestpath := ""
for _, path := range goPathList {
if c.Index == NEW {
// If the GOPATH is part of the working dir this is the most likely target
if strings.HasPrefix(workingDir, path) {
bestpath = path
}
} else {
if utils.Exists(filepath.Join(path, "src", c.ImportPath)) {
bestpath = path
break
}
}
}
utils.Logger.Info("Source root", "cwd", workingDir, "gopath", c.GoPath, "c.ImportPath", c.ImportPath, "bestpath", bestpath)
if len(bestpath) > 0 {
c.AppPath = filepath.Join(bestpath, "src", c.ImportPath)
}
// Recalculate the appFolder because we are using a GOPATH
} else {
// This is new and not vendored, so the app path is the appFolder
c.AppPath = appFolder
}
utils.Logger.Info("Set application path", "path", c.AppPath, "vendored", c.Vendored, "importpath", c.ImportPath)
return nil
}
// Used to initialize the package resolver.
func (c *CommandConfig) InitPackageResolver() {
c.initGoPaths()
utils.Logger.Info("InitPackageResolver", "useVendor", c.Vendored, "path", c.AppPath)
// This should get called when needed // This should get called when needed
c.PackageResolver = func(pkgName string) error { c.PackageResolver = func(pkgName string) error {
//useVendor := utils.DirExists(filepath.Join(c.AppPath, "vendor")) utils.Logger.Info("Request for package ", "package", pkgName, "use vendor", c.Vendored)
var getCmd *exec.Cmd var getCmd *exec.Cmd
utils.Logger.Info("Request for package ", "package", pkgName, "use vendor", useVendor) print("Downloading related packages ...")
if useVendor { if c.Vendored {
utils.Logger.Info("Using dependency manager to import package", "package", pkgName) getCmd = exec.Command(c.GoCmd, "mod", "tidy", "-v")
if depPath == "" {
utils.Logger.Error("Build: Vendor folder found, but the `dep` tool was not found, " +
"if you use a different vendoring (package management) tool please add the following packages by hand, " +
"or install the `dep` tool into your gopath by doing a `go get -u github.com/golang/dep/cmd/dep`. " +
"For more information and usage of the tool please see http://github.com/golang/dep")
utils.Logger.Error("Missing package", "package", pkgName)
return fmt.Errorf("Missing package %s", pkgName)
}
getCmd = exec.Command(depPath, "ensure", "-add", pkgName)
} else { } else {
utils.Logger.Info("No vendor folder detected, not using dependency manager to import package", "package", pkgName) utils.Logger.Info("No vendor folder detected, not using dependency manager to import package", "package", pkgName)
getCmd = exec.Command(c.GoCmd, "get", pkgName) getCmd = exec.Command(c.GoCmd, "get", "-u", pkgName)
} }
utils.CmdInit(getCmd, c.AppPath) utils.CmdInit(getCmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Go get command ", "exec", getCmd.Path, "dir", getCmd.Dir, "args", getCmd.Args, "env", getCmd.Env, "package", pkgName) utils.Logger.Info("Go get command ", "exec", getCmd.Path, "dir", getCmd.Dir, "args", getCmd.Args, "env", getCmd.Env, "package", pkgName)
output, err := getCmd.CombinedOutput() output, err := getCmd.CombinedOutput()
if err != nil { if err != nil {
fmt.Printf("Error stack %v\n", logger.NewCallStack())
utils.Logger.Error("Failed to import package", "error", err, "gopath", build.Default.GOPATH, "GO-ROOT", build.Default.GOROOT, "output", string(output)) utils.Logger.Error("Failed to import package", "error", err, "gopath", build.Default.GOPATH, "GO-ROOT", build.Default.GOROOT, "output", string(output))
} }
return err println(" completed.")
return nil
} }
} }
// lookup and set Go related variables // lookup and set Go related variables.
func (c *CommandConfig) InitGoPaths() { func (c *CommandConfig) initGoPaths() {
// lookup go path utils.Logger.Info("InitGoPaths", "vendored", c.Vendored)
c.GoPath = build.Default.GOPATH
if c.GoPath == "" {
utils.Logger.Fatal("Abort: GOPATH environment variable is not set. " +
"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
}
// check for go executable // check for go executable
var err error var err error
c.GoCmd, err = exec.LookPath("go") c.GoCmd, err = exec.LookPath("go")
@@ -215,44 +271,93 @@ func (c *CommandConfig) InitGoPaths() {
utils.Logger.Fatal("Go executable not found in PATH.") utils.Logger.Fatal("Go executable not found in PATH.")
} }
if c.Vendored {
return
}
// lookup go path
c.GoPath = build.Default.GOPATH
if c.GoPath == "" {
utils.Logger.Fatal("Abort: GOPATH environment variable is not set. " +
"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
}
// todo determine if the rest needs to happen
// revel/revel#1004 choose go path relative to current working directory // revel/revel#1004 choose go path relative to current working directory
// What we want to do is to add the import to the end of the // What we want to do is to add the import to the end of the
// gopath, and discover which import exists - If none exist this is an error except in the case // gopath, and discover which import exists - If none exist this is an error except in the case
// where we are dealing with new which is a special case where we will attempt to target the working directory first // where we are dealing with new which is a special case where we will attempt to target the working directory first
workingDir, _ := os.Getwd() /*
goPathList := filepath.SplitList(c.GoPath) // If source root is empty and this isn't a version then skip it
bestpath := "" if len(c.SrcRoot) == 0 {
for _, path := range goPathList { if c.Index == NEW {
if c.Index == NEW { c.SrcRoot = c.New.ImportPath
// If the GOPATH is part of the working dir this is the most likely target } else {
if strings.HasPrefix(workingDir, path) { if c.Index != VERSION {
bestpath = path utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
} }
} else { return
if utils.Exists(filepath.Join(path, "src", c.ImportPath)) {
c.SrcRoot = path
break
} }
} }
}
if len(c.SrcRoot)==0 && len(bestpath) > 0 { // set go src path
c.SrcRoot = bestpath c.SrcRoot = filepath.Join(c.SrcRoot, "src")
}
utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath)
// If source root is empty and this isn't a version then skip it c.AppPath = filepath.Join(c.SrcRoot, filepath.FromSlash(c.ImportPath))
if len(c.SrcRoot) == 0 { utils.Logger.Info("Set application path", "path", c.AppPath)
if c.Index != VERSION {
utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
}
return
}
// set go src path */
c.SrcRoot = filepath.Join(c.SrcRoot, "src") }
c.AppPath = filepath.Join(c.SrcRoot, filepath.FromSlash(c.ImportPath)) // Sets the versions on the command config.
utils.Logger.Info("Set application path", "path", c.AppPath) func (c *CommandConfig) GetVerbose() (verbose bool) {
if len(c.Verbose) > 0 {
verbose = c.Verbose[0]
}
return
}
// Sets the versions on the command config.
func (c *CommandConfig) SetVersions() (err error) {
c.CommandVersion, _ = ParseVersion(cmd.Version)
pathMap, err := utils.FindSrcPaths(c.AppPath, []string{RevelImportPath}, c.PackageResolver)
if err == nil {
utils.Logger.Info("Fullpath to revel", "dir", pathMap[RevelImportPath])
fset := token.NewFileSet() // positions are relative to fset
versionData, err := ioutil.ReadFile(filepath.Join(pathMap[RevelImportPath], "version.go"))
if err != nil {
utils.Logger.Error("Failed to find Revel version:", "error", err, "path", pathMap[RevelImportPath])
}
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, "", versionData, parser.ParseComments)
if err != nil {
return utils.NewBuildError("Failed to parse Revel version error:", "error", err)
}
// Print the imports from the file's AST.
for _, s := range f.Decls {
genDecl, ok := s.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok != token.CONST {
continue
}
for _, a := range genDecl.Specs {
spec := a.(*ast.ValueSpec)
r := spec.Values[0].(*ast.BasicLit)
if spec.Names[0].Name == "Version" {
c.FrameworkVersion, err = ParseVersion(strings.ReplaceAll(r.Value, `"`, ``))
if err != nil {
utils.Logger.Errorf("Failed to parse version")
} else {
utils.Logger.Info("Parsed revel version", "version", c.FrameworkVersion.String())
}
}
}
}
}
return
} }

View File

@@ -1,11 +1,11 @@
package model package model
// The embedded type name takes the import path and structure name // The embedded type name takes the import path and structure name.
type EmbeddedTypeName struct { type EmbeddedTypeName struct {
ImportPath, StructName string ImportPath, StructName string
} }
// Convert the type to a properly formatted import line // Convert the type to a properly formatted import line.
func (s *EmbeddedTypeName) String() string { func (s *EmbeddedTypeName) String() string {
return s.ImportPath + "." + s.StructName return s.ImportPath + "." + s.StructName
} }

View File

@@ -1,11 +1,11 @@
package model package model
type ( type (
// The event type // The event type.
Event int Event int
// The event response // The event response.
EventResponse int EventResponse int
// The handler signature // The handler signature.
EventHandler func(typeOf Event, value interface{}) (responseOf EventResponse) EventHandler func(typeOf Event, value interface{}) (responseOf EventResponse)
RevelCallback interface { RevelCallback interface {
FireEvent(key Event, value interface{}) (response EventResponse) FireEvent(key Event, value interface{}) (response EventResponse)
@@ -14,40 +14,40 @@ type (
) )
const ( const (
// Event type when templates are going to be refreshed (receivers are registered template engines added to the template.engine conf option) // Event type when templates are going to be refreshed (receivers are registered template engines added to the template.engine conf option).
TEMPLATE_REFRESH_REQUESTED Event = iota TEMPLATE_REFRESH_REQUESTED Event = iota
// Event type when templates are refreshed (receivers are registered template engines added to the template.engine conf option) // Event type when templates are refreshed (receivers are registered template engines added to the template.engine conf option).
TEMPLATE_REFRESH_COMPLETED TEMPLATE_REFRESH_COMPLETED
// Event type before all module loads, events thrown to handlers added to AddInitEventHandler // Event type before all module loads, events thrown to handlers added to AddInitEventHandler.
// Event type before all module loads, events thrown to handlers added to AddInitEventHandler // Event type before all module loads, events thrown to handlers added to AddInitEventHandler.
REVEL_BEFORE_MODULES_LOADED REVEL_BEFORE_MODULES_LOADED
// Event type before module loads, events thrown to handlers added to AddInitEventHandler // Event type before module loads, events thrown to handlers added to AddInitEventHandler.
REVEL_BEFORE_MODULE_LOADED REVEL_BEFORE_MODULE_LOADED
// Event type after module loads, events thrown to handlers added to AddInitEventHandler // Event type after module loads, events thrown to handlers added to AddInitEventHandler.
REVEL_AFTER_MODULE_LOADED REVEL_AFTER_MODULE_LOADED
// Event type after all module loads, events thrown to handlers added to AddInitEventHandler // Event type after all module loads, events thrown to handlers added to AddInitEventHandler.
REVEL_AFTER_MODULES_LOADED REVEL_AFTER_MODULES_LOADED
// Event type before server engine is initialized, receivers are active server engine and handlers added to AddInitEventHandler // Event type before server engine is initialized, receivers are active server engine and handlers added to AddInitEventHandler.
ENGINE_BEFORE_INITIALIZED ENGINE_BEFORE_INITIALIZED
// Event type before server engine is started, receivers are active server engine and handlers added to AddInitEventHandler // Event type before server engine is started, receivers are active server engine and handlers added to AddInitEventHandler.
ENGINE_STARTED ENGINE_STARTED
// Event type after server engine is stopped, receivers are active server engine and handlers added to AddInitEventHandler // Event type after server engine is stopped, receivers are active server engine and handlers added to AddInitEventHandler.
ENGINE_SHUTDOWN ENGINE_SHUTDOWN
// Called before routes are refreshed // Called before routes are refreshed.
ROUTE_REFRESH_REQUESTED ROUTE_REFRESH_REQUESTED
// Called after routes have been refreshed // Called after routes have been refreshed.
ROUTE_REFRESH_COMPLETED ROUTE_REFRESH_COMPLETED
// Fired when a panic is caught during the startup process // Fired when a panic is caught during the startup process.
REVEL_FAILURE REVEL_FAILURE
) )
var initEventList = []EventHandler{} // Event handler list for receiving events var initEventList = []EventHandler{} // Event handler list for receiving events
// Fires system events from revel // Fires system events from revel.
func RaiseEvent(key Event, value interface{}) (response EventResponse) { func RaiseEvent(key Event, value interface{}) (response EventResponse) {
for _, handler := range initEventList { for _, handler := range initEventList {
response |= handler(key, value) response |= handler(key, value)
@@ -55,8 +55,7 @@ func RaiseEvent(key Event, value interface{}) (response EventResponse) {
return return
} }
// Add event handler to listen for all system events // Add event handler to listen for all system events.
func AddInitEventHandler(handler EventHandler) { func AddInitEventHandler(handler EventHandler) {
initEventList = append(initEventList, handler) initEventList = append(initEventList, handler)
return
} }

View File

@@ -1,24 +1,24 @@
package model_test package model_test
import ( import (
"testing"
"github.com/revel/revel" "github.com/revel/revel"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
// Test that the event handler can be attached and it dispatches the event received // Test that the event handler can be attached and it dispatches the event received.
func TestEventHandler(t *testing.T) { func TestEventHandler(t *testing.T) {
counter := 0 counter := 0
newListener := func(typeOf revel.Event, value interface{}) (responseOf revel.EventResponse) { newListener := func(typeOf revel.Event, value interface{}) (responseOf revel.EventResponse) {
if typeOf == revel.REVEL_FAILURE { if typeOf == revel.ENGINE_SHUTDOWN_REQUEST {
counter++ counter++
} }
return return
} }
// Attach the same handlder twice so we expect to see the response twice as well // Attach the same handler twice so we expect to see the response twice as well
revel.AddInitEventHandler(newListener) revel.AddInitEventHandler(newListener)
revel.AddInitEventHandler(newListener) revel.AddInitEventHandler(newListener)
revel.RaiseEvent(revel.REVEL_AFTER_MODULES_LOADED, nil) revel.StopServer(1)
revel.RaiseEvent(revel.REVEL_FAILURE, nil)
assert.Equal(t, counter, 2, "Expected event handler to have been called") assert.Equal(t, counter, 2, "Expected event handler to have been called")
} }

View File

@@ -8,14 +8,14 @@ type MethodCall struct {
Names []string Names []string
} }
// MethodSpec holds the information of one Method // MethodSpec holds the information of one Method.
type MethodSpec struct { type MethodSpec struct {
Name string // Name of the method, e.g. "Index" Name string // Name of the method, e.g. "Index"
Args []*MethodArg // Argument descriptors Args []*MethodArg // Argument descriptors
RenderCalls []*MethodCall // Descriptions of Render() invocations from this Method. RenderCalls []*MethodCall // Descriptions of Render() invocations from this Method.
} }
// MethodArg holds the information of one argument // MethodArg holds the information of one argument.
type MethodArg struct { type MethodArg struct {
Name string // Name of the argument. Name string // Name of the argument.
TypeExpr TypeExpr // The name of the type, e.g. "int", "*pkg.UserType" TypeExpr TypeExpr // The name of the type, e.g. "int", "*pkg.UserType"

View File

@@ -2,19 +2,36 @@
package model package model
import ( import (
"github.com/revel/cmd/utils"
"github.com/revel/config"
"go/build"
"errors"
"fmt" "fmt"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"github.com/revel/cmd/utils"
"github.com/revel/config"
"golang.org/x/tools/go/packages"
)
// Error is used for constant errors.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
const (
ErrNoApp Error = "no app found at path"
ErrNoConfig Error = "no config found at path"
ErrNotFound Error = "not found"
ErrMissingCert Error = "no http.sslcert provided"
ErrMissingKey Error = "no http.sslkey provided"
ErrNoFiles Error = "no files found in import path"
ErrNoPackages Error = "no packages found for import"
) )
type ( type (
// The container object for describing all Revels variables // The container object for describing all Revels variables.
RevelContainer struct { RevelContainer struct {
BuildPaths struct { BuildPaths struct {
Revel string Revel string
@@ -40,32 +57,36 @@ type (
Root string Root string
} }
ImportPath string // The import path ImportPath string // The import path
SourcePath string // The full source path SourcePath string // The full source path
RunMode string // The current run mode RunMode string // The current run mode
RevelPath string // The path to the Revel source code RevelPath string // The path to the Revel source code
BasePath string // The base path to the application BasePath string // The base path to the application
AppPath string // The application path (BasePath + "/app") AppPath string // The application path (BasePath + "/app")
ViewsPath string // The application views path ViewsPath string // The application views path
CodePaths []string // All the code paths CodePaths []string // All the code paths
TemplatePaths []string // All the template paths TemplatePaths []string // All the template paths
ConfPaths []string // All the configuration paths ConfPaths []string // All the configuration paths
Config *config.Context // The global config object Config *config.Context // The global config object
Packaged bool // True if packaged Packaged bool // True if packaged
DevMode bool // True if running in dev mode DevMode bool // True if running in dev mode
HTTPPort int // The http port HTTPPort int // The http port
HTTPAddr string // The http address HTTPAddr string // The http address
HTTPSsl bool // True if running https HTTPSsl bool // True if running https
HTTPSslCert string // The SSL certificate HTTPSslCert string // The SSL certificate
HTTPSslKey string // The SSL key HTTPSslKey string // The SSL key
AppName string // The application name AppName string // The application name
AppRoot string // The application root from the config `app.root` AppRoot string // The application root from the config `app.root`
CookiePrefix string // The cookie prefix CookiePrefix string // The cookie prefix
CookieDomain string // The cookie domain CookieDomain string // The cookie domain
CookieSecure bool // True if cookie is secure CookieSecure bool // True if cookie is secure
SecretStr string // The secret string SecretStr string // The secret string
MimeConfig *config.Context // The mime configuration MimeConfig *config.Context // The mime configuration
ModulePathMap map[string]string // The module path map ModulePathMap map[string]*ModuleInfo // The module path map
}
ModuleInfo struct {
ImportPath string
Path string
} }
WrappedRevelCallback struct { WrappedRevelCallback struct {
@@ -74,54 +95,58 @@ type (
} }
) )
// Simple Wrapped RevelCallback // Simple Wrapped RevelCallback.
func NewWrappedRevelCallback(fe func(key Event, value interface{}) (response EventResponse), ie func(pkgName string) error) RevelCallback { func NewWrappedRevelCallback(fe func(key Event, value interface{}) (response EventResponse), ie func(pkgName string) error) RevelCallback {
return &WrappedRevelCallback{fe, ie} return &WrappedRevelCallback{fe, ie}
} }
// Function to implement the FireEvent // Function to implement the FireEvent.
func (w *WrappedRevelCallback) FireEvent(key Event, value interface{}) (response EventResponse) { func (w *WrappedRevelCallback) FireEvent(key Event, value interface{}) (response EventResponse) {
if w.FireEventFunction != nil { if w.FireEventFunction != nil {
response = w.FireEventFunction(key, value) response = w.FireEventFunction(key, value)
} }
return return
} }
func (w *WrappedRevelCallback) PackageResolver(pkgName string) error { func (w *WrappedRevelCallback) PackageResolver(pkgName string) error {
return w.ImportFunction(pkgName) return w.ImportFunction(pkgName)
} }
// RevelImportPath Revel framework import path // RevelImportPath Revel framework import path.
var RevelImportPath = "github.com/revel/revel" var (
RevelImportPath = "github.com/revel/revel"
RevelModulesImportPath = "github.com/revel/modules"
)
// This function returns a container object describing the revel application // This function returns a container object describing the revel application
// eventually this type of function will replace the global variables. // eventually this type of function will replace the global variables.
func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp *RevelContainer, err error) { func NewRevelPaths(mode, importPath, appSrcPath string, callback RevelCallback) (rp *RevelContainer, err error) {
rp = &RevelContainer{ModulePathMap: map[string]string{}} rp = &RevelContainer{ModulePathMap: map[string]*ModuleInfo{}}
// Ignore trailing slashes. // Ignore trailing slashes.
rp.ImportPath = strings.TrimRight(importPath, "/") rp.ImportPath = strings.TrimRight(importPath, "/")
rp.SourcePath = srcPath rp.SourcePath = appSrcPath
rp.RunMode = mode rp.RunMode = mode
// If the SourcePath is not specified, find it using build.Import. // We always need to determine the paths for files
var revelSourcePath string // may be different from the app source path pathMap, err := utils.FindSrcPaths(appSrcPath, []string{importPath + "/app", RevelImportPath}, callback.PackageResolver)
if rp.SourcePath == "" { if err != nil {
rp.SourcePath, revelSourcePath, err = utils.FindSrcPaths(importPath, RevelImportPath, callback.PackageResolver) return
if err != nil { }
return rp.AppPath, rp.RevelPath = pathMap[importPath], pathMap[RevelImportPath]
} // Setup paths for application
} else { rp.BasePath = rp.SourcePath
// If the SourcePath was specified, assume both Revel and the app are within it. rp.PackageInfo.Vendor = utils.Exists(filepath.Join(rp.BasePath, "go.mod"))
rp.SourcePath = filepath.Clean(rp.SourcePath) rp.AppPath = filepath.Join(rp.BasePath, "app")
revelSourcePath = rp.SourcePath
// Sanity check , ensure app and conf paths exist
if !utils.DirExists(rp.AppPath) {
return rp, fmt.Errorf("%w: %s", ErrNoApp, rp.AppPath)
}
if !utils.DirExists(filepath.Join(rp.BasePath, "conf")) {
return rp, fmt.Errorf("%w: %s", ErrNoConfig, filepath.Join(rp.BasePath, "conf"))
} }
// Setup paths for application
rp.RevelPath = filepath.Join(revelSourcePath, filepath.FromSlash(RevelImportPath))
rp.BasePath = filepath.Join(rp.SourcePath, filepath.FromSlash(importPath))
rp.PackageInfo.Vendor = utils.Exists(filepath.Join(rp.BasePath, "vendor"))
rp.AppPath = filepath.Join(rp.BasePath, "app")
rp.ViewsPath = filepath.Join(rp.AppPath, "views") rp.ViewsPath = filepath.Join(rp.AppPath, "views")
rp.CodePaths = []string{rp.AppPath} rp.CodePaths = []string{rp.AppPath}
rp.TemplatePaths = []string{} rp.TemplatePaths = []string{}
@@ -142,7 +167,7 @@ func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp
rp.Config, err = config.LoadContext("app.conf", rp.ConfPaths) rp.Config, err = config.LoadContext("app.conf", rp.ConfPaths)
if err != nil { if err != nil {
return rp, fmt.Errorf("Unable to load configuartion file %s", err) return rp, fmt.Errorf("unable to load configuration file %w", err)
} }
// Ensure that the selected runmode appears in app.conf. // Ensure that the selected runmode appears in app.conf.
@@ -151,7 +176,7 @@ func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp
mode = config.DefaultSection mode = config.DefaultSection
} }
if !rp.Config.HasSection(mode) { if !rp.Config.HasSection(mode) {
return rp, fmt.Errorf("app.conf: No mode found: %s %s", "run-mode", mode) return rp, fmt.Errorf("app.conf: %w %s %s", ErrNotFound, "run-mode", mode)
} }
rp.Config.SetSection(mode) rp.Config.SetSection(mode)
@@ -164,13 +189,14 @@ func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp
rp.HTTPSslKey = rp.Config.StringDefault("http.sslkey", "") rp.HTTPSslKey = rp.Config.StringDefault("http.sslkey", "")
if rp.HTTPSsl { if rp.HTTPSsl {
if rp.HTTPSslCert == "" { if rp.HTTPSslCert == "" {
return rp, errors.New("No http.sslcert provided.") return rp, ErrMissingCert
} }
if rp.HTTPSslKey == "" { if rp.HTTPSslKey == "" {
return rp, errors.New("No http.sslkey provided.") return rp, ErrMissingKey
} }
} }
//
rp.AppName = rp.Config.StringDefault("app.name", "(not set)") rp.AppName = rp.Config.StringDefault("app.name", "(not set)")
rp.AppRoot = rp.Config.StringDefault("app.root", "") rp.AppRoot = rp.Config.StringDefault("app.root", "")
rp.CookiePrefix = rp.Config.StringDefault("cookie.prefix", "REVEL") rp.CookiePrefix = rp.Config.StringDefault("cookie.prefix", "REVEL")
@@ -179,6 +205,7 @@ func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp
rp.SecretStr = rp.Config.StringDefault("app.secret", "") rp.SecretStr = rp.Config.StringDefault("app.secret", "")
callback.FireEvent(REVEL_BEFORE_MODULES_LOADED, nil) callback.FireEvent(REVEL_BEFORE_MODULES_LOADED, nil)
utils.Logger.Info("Loading modules")
if err := rp.loadModules(callback); err != nil { if err := rp.loadModules(callback); err != nil {
return rp, err return rp, err
} }
@@ -192,7 +219,7 @@ func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp
func (rp *RevelContainer) LoadMimeConfig() (err error) { func (rp *RevelContainer) LoadMimeConfig() (err error) {
rp.MimeConfig, err = config.LoadContext("mime-types.conf", rp.ConfPaths) rp.MimeConfig, err = config.LoadContext("mime-types.conf", rp.ConfPaths)
if err != nil { if err != nil {
return fmt.Errorf("Failed to load mime type config: %s %s", "error", err) return fmt.Errorf("failed to load mime type config: %s %w", "error", err)
} }
return return
} }
@@ -201,12 +228,10 @@ func (rp *RevelContainer) LoadMimeConfig() (err error) {
// This will fire the REVEL_BEFORE_MODULE_LOADED, REVEL_AFTER_MODULE_LOADED // This will fire the REVEL_BEFORE_MODULE_LOADED, REVEL_AFTER_MODULE_LOADED
// for each module loaded. The callback will receive the RevelContainer, name, moduleImportPath and modulePath // for each module loaded. The callback will receive the RevelContainer, name, moduleImportPath and modulePath
// It will automatically add in the code paths for the module to the // It will automatically add in the code paths for the module to the
// container object // container object.
func (rp *RevelContainer) loadModules(callback RevelCallback) (err error) { func (rp *RevelContainer) loadModules(callback RevelCallback) (err error) {
keys := []string{} keys := []string{}
for _, key := range rp.Config.Options("module.") { keys = append(keys, rp.Config.Options("module.")...)
keys = append(keys, key)
}
// Reorder module order by key name, a poor mans sort but at least it is consistent // Reorder module order by key name, a poor mans sort but at least it is consistent
sort.Strings(keys) sort.Strings(keys)
@@ -218,11 +243,15 @@ func (rp *RevelContainer) loadModules(callback RevelCallback) (err error) {
modulePath, err := rp.ResolveImportPath(moduleImportPath) modulePath, err := rp.ResolveImportPath(moduleImportPath)
if err != nil { if err != nil {
utils.Logger.Info("Missing module ", "module", moduleImportPath, "error",err) utils.Logger.Info("Missing module ", "module_import_path", moduleImportPath, "error", err)
callback.PackageResolver(moduleImportPath)
if err := callback.PackageResolver(moduleImportPath); err != nil {
return fmt.Errorf("failed to resolve package %w", err)
}
modulePath, err = rp.ResolveImportPath(moduleImportPath) modulePath, err = rp.ResolveImportPath(moduleImportPath)
if err != nil { if err != nil {
return fmt.Errorf("Failed to load module. Import of path failed %s:%s %s:%s ", "modulePath", moduleImportPath, "error", err) return fmt.Errorf("failed to load module. Import of path failed %s:%s %s:%w ", "modulePath", moduleImportPath, "error", err)
} }
} }
// Drop anything between module.???.<name of module> // Drop anything between module.???.<name of module>
@@ -237,11 +266,12 @@ func (rp *RevelContainer) loadModules(callback RevelCallback) (err error) {
return return
} }
// Adds a module paths to the container object // Adds a module paths to the container object.
func (rp *RevelContainer) addModulePaths(name, importPath, modulePath string) { func (rp *RevelContainer) addModulePaths(name, importPath, modulePath string) {
utils.Logger.Info("Adding module path", "name", name, "import path", importPath, "system path", modulePath)
if codePath := filepath.Join(modulePath, "app"); utils.DirExists(codePath) { if codePath := filepath.Join(modulePath, "app"); utils.DirExists(codePath) {
rp.CodePaths = append(rp.CodePaths, codePath) rp.CodePaths = append(rp.CodePaths, codePath)
rp.ModulePathMap[name] = modulePath rp.ModulePathMap[name] = &ModuleInfo{importPath, modulePath}
if viewsPath := filepath.Join(modulePath, "app", "views"); utils.DirExists(viewsPath) { if viewsPath := filepath.Join(modulePath, "app", "views"); utils.DirExists(viewsPath) {
rp.TemplatePaths = append(rp.TemplatePaths, viewsPath) rp.TemplatePaths = append(rp.TemplatePaths, viewsPath)
} }
@@ -264,13 +294,22 @@ func (rp *RevelContainer) ResolveImportPath(importPath string) (string, error) {
if rp.Packaged { if rp.Packaged {
return filepath.Join(rp.SourcePath, importPath), nil return filepath.Join(rp.SourcePath, importPath), nil
} }
config := &packages.Config{
modPkg, err := build.Import(importPath, rp.AppPath, build.FindOnly) Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports |
packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo,
Dir: rp.AppPath,
}
config.Env = utils.ReducedEnv(false)
pkgs, err := packages.Load(config, importPath)
if len(pkgs) == 0 {
return "", fmt.Errorf("%w %s using app path %s", ErrNoPackages, importPath, rp.AppPath)
}
// modPkg, err := build.Import(importPath, rp.AppPath, build.FindOnly)
if err != nil { if err != nil {
return "", err return "", err
} }
if rp.PackageInfo.Vendor && !strings.HasPrefix(modPkg.Dir,rp.BasePath) { if len(pkgs[0].GoFiles) > 0 {
return "", fmt.Errorf("Module %s was found outside of path %s.",importPath, modPkg.Dir) return filepath.Dir(pkgs[0].GoFiles[0]), nil
} }
return modPkg.Dir, nil return pkgs[0].PkgPath, fmt.Errorf("%w: %s", ErrNoFiles, importPath)
} }

View File

@@ -3,10 +3,11 @@ package model
// SourceInfo is the top-level struct containing all extracted information // SourceInfo is the top-level struct containing all extracted information
// about the app source code, used to generate main.go. // about the app source code, used to generate main.go.
import ( import (
"github.com/revel/cmd/utils"
"path/filepath" "path/filepath"
"strings" "strings"
"unicode" "unicode"
"github.com/revel/cmd/utils"
) )
type SourceInfo struct { type SourceInfo struct {
@@ -29,11 +30,13 @@ type SourceInfo struct {
controllerSpecs []*TypeInfo controllerSpecs []*TypeInfo
// testSuites list the types that constitute the set of application tests. // testSuites list the types that constitute the set of application tests.
testSuites []*TypeInfo testSuites []*TypeInfo
// packageMap a map of import to system directory (if available)
PackageMap map[string]string
} }
// TypesThatEmbed returns all types that (directly or indirectly) embed the // TypesThatEmbed returns all types that (directly or indirectly) embed the
// target type, which must be a fully qualified type name, // target type, which must be a fully qualified type name,
// e.g. "github.com/revel/revel.Controller" // e.g. "github.com/revel/revel.Controller".
func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered []*TypeInfo) { func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered []*TypeInfo) {
// Do a search in the "embedded type graph", starting with the target type. // Do a search in the "embedded type graph", starting with the target type.
var ( var (
@@ -55,7 +58,6 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
// Look through the embedded types to see if the current type is among them. // Look through the embedded types to see if the current type is among them.
for _, embeddedType := range spec.EmbeddedTypes { for _, embeddedType := range spec.EmbeddedTypes {
// If so, add this type's simple name to the nodeQueue, and its spec to // If so, add this type's simple name to the nodeQueue, and its spec to
// the filtered list. // the filtered list.
if typeSimpleName == embeddedType.String() { if typeSimpleName == embeddedType.String() {
@@ -75,6 +77,7 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
"type", filteredItem.StructName, "type", filteredItem.StructName,
"package", filteredItem.ImportPath) "package", filteredItem.ImportPath)
filtered = append(filtered[:i], filtered[i+1:]...) filtered = append(filtered[:i], filtered[i+1:]...)
//nolint:ineffassign // huh?
exit = false exit = false
break break
} }
@@ -107,8 +110,9 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
} }
// ControllerSpecs returns the all the controllers that embeds // ControllerSpecs returns the all the controllers that embeds
// `revel.Controller` // `revel.Controller`.
func (s *SourceInfo) ControllerSpecs() []*TypeInfo { func (s *SourceInfo) ControllerSpecs() []*TypeInfo {
utils.Logger.Info("Scanning controller specifications for types ", "typePath", RevelImportPath+".Controller", "speclen", len(s.controllerSpecs))
if s.controllerSpecs == nil { if s.controllerSpecs == nil {
s.controllerSpecs = s.TypesThatEmbed(RevelImportPath+".Controller", "controllers") s.controllerSpecs = s.TypesThatEmbed(RevelImportPath+".Controller", "controllers")
} }
@@ -116,10 +120,22 @@ func (s *SourceInfo) ControllerSpecs() []*TypeInfo {
} }
// TestSuites returns the all the Application tests that embeds // TestSuites returns the all the Application tests that embeds
// `testing.TestSuite` // `testing.TestSuite`.
func (s *SourceInfo) TestSuites() []*TypeInfo { func (s *SourceInfo) TestSuites() []*TypeInfo {
if s.testSuites == nil { if s.testSuites == nil {
s.testSuites = s.TypesThatEmbed(RevelImportPath+"/testing.TestSuite", "testsuite") s.testSuites = s.TypesThatEmbed(RevelImportPath+"/testing.TestSuite", "testsuite")
} }
return s.testSuites return s.testSuites
} }
func (s *SourceInfo) Merge(srcInfo2 *SourceInfo) {
s.StructSpecs = append(s.StructSpecs, srcInfo2.StructSpecs...)
s.InitImportPaths = append(s.InitImportPaths, srcInfo2.InitImportPaths...)
for k, v := range srcInfo2.ValidationKeys {
if _, ok := s.ValidationKeys[k]; ok {
utils.Logger.Warn("Warn: Key conflict when scanning validation calls:", "key", k)
continue
}
s.ValidationKeys[k] = v
}
}

View File

@@ -13,14 +13,14 @@ type TypeExpr struct {
Valid bool Valid bool
} }
// Returns a new type from the data // Returns a new type from the data.
func NewTypeExprFromData(expr, pkgName string, pkgIndex int, valid bool) TypeExpr { func NewTypeExprFromData(expr, pkgName string, pkgIndex int, valid bool) TypeExpr {
return TypeExpr{expr, pkgName, pkgIndex, valid} return TypeExpr{expr, pkgName, pkgIndex, valid}
} }
// NewTypeExpr returns the syntactic expression for referencing this type in Go. // NewTypeExpr returns the syntactic expression for referencing this type in Go.
func NewTypeExprFromAst(pkgName string, expr ast.Expr) TypeExpr { func NewTypeExprFromAst(pkgName string, expr ast.Expr) TypeExpr {
error := "" err := ""
switch t := expr.(type) { switch t := expr.(type) {
case *ast.Ident: case *ast.Ident:
if IsBuiltinType(t.Name) { if IsBuiltinType(t.Name) {
@@ -41,15 +41,14 @@ func NewTypeExprFromAst(pkgName string, expr ast.Expr) TypeExpr {
e := NewTypeExprFromAst(pkgName, t.Value) e := NewTypeExprFromAst(pkgName, t.Value)
return NewTypeExprFromData("map["+identKey.Name+"]"+e.Expr, e.PkgName, e.pkgIndex+len("map["+identKey.Name+"]"), e.Valid) return NewTypeExprFromData("map["+identKey.Name+"]"+e.Expr, e.PkgName, e.pkgIndex+len("map["+identKey.Name+"]"), e.Valid)
} }
error = fmt.Sprintf("Failed to generate name for Map field :%v. Make sure the field name is valid.", t.Key) err = fmt.Sprintf("Failed to generate name for Map field :%v. Make sure the field name is valid.", t.Key)
case *ast.Ellipsis: case *ast.Ellipsis:
e := NewTypeExprFromAst(pkgName, t.Elt) e := NewTypeExprFromAst(pkgName, t.Elt)
return NewTypeExprFromData("[]"+e.Expr, e.PkgName, e.pkgIndex+2, e.Valid) return NewTypeExprFromData("[]"+e.Expr, e.PkgName, e.pkgIndex+2, e.Valid)
default: default:
error = fmt.Sprintf("Failed to generate name for field: %v Package: %v. Make sure the field name is valid.", expr, pkgName) err = fmt.Sprintf("Failed to generate name for field: %v Package: %v. Make sure the field name is valid.", expr, pkgName)
} }
return NewTypeExprFromData(error, "", 0, false) return NewTypeExprFromData(err, "", 0, false)
} }
// TypeName returns the fully-qualified type name for this expression. // TypeName returns the fully-qualified type name for this expression.
@@ -62,7 +61,7 @@ func (e TypeExpr) TypeName(pkgOverride string) string {
return e.Expr[:e.pkgIndex] + pkgName + "." + e.Expr[e.pkgIndex:] return e.Expr[:e.pkgIndex] + pkgName + "." + e.Expr[e.pkgIndex:]
} }
var builtInTypes = map[string]struct{}{ var builtInTypes = map[string]struct{}{ //nolint:gochecknoglobals
"bool": {}, "bool": {},
"byte": {}, "byte": {},
"complex128": {}, "complex128": {},
@@ -85,13 +84,13 @@ var builtInTypes = map[string]struct{}{
"uintptr": {}, "uintptr": {},
} }
// IsBuiltinType checks the given type is built-in types of Go // IsBuiltinType checks the given type is built-in types of Go.
func IsBuiltinType(name string) bool { func IsBuiltinType(name string) bool {
_, ok := builtInTypes[name] _, ok := builtInTypes[name]
return ok return ok
} }
// Returns the first non empty string from a list of arguements // Returns the first non empty string from a list of arguments.
func FirstNonEmpty(strs ...string) string { func FirstNonEmpty(strs ...string) string {
for _, str := range strs { for _, str := range strs {
if len(str) > 0 { if len(str) > 0 {

View File

@@ -9,7 +9,7 @@ type TypeInfo struct {
EmbeddedTypes []*EmbeddedTypeName // Used internally to identify controllers that indirectly embed *revel.Controller. EmbeddedTypes []*EmbeddedTypeName // Used internally to identify controllers that indirectly embed *revel.Controller.
} }
// Return the type information as a properly formatted import string // Return the type information as a properly formatted import string.
func (s *TypeInfo) String() string { func (s *TypeInfo) String() string {
return s.ImportPath + "." + s.StructName return s.ImportPath + "." + s.StructName
} }

126
model/version.go Normal file
View File

@@ -0,0 +1,126 @@
package model
import (
"fmt"
"regexp"
"strconv"
"github.com/pkg/errors"
)
type Version struct {
Prefix string
Major int
Minor int
Maintenance int
Suffix string
BuildDate string
MinGoVersion string
}
// The compatibility list.
var frameworkCompatibleRangeList = [][]string{
{"0.0.0", "0.20.0"}, // minimum Revel version to use with this version of the tool
{"0.19.99", "0.30.0"}, // Compatible with Framework V 0.19.99 - 0.30.0
{"1.0.0", "1.9.0"}, // Compatible with Framework V 1.0 - 1.9
}
// Parses a version like v1.2.3a or 1.2.
var versionRegExp = regexp.MustCompile(`([^\d]*)?([0-9]*)\.([0-9]*)(\.([0-9]*))?(.*)`)
// Parse the version and return it as a Version object.
func ParseVersion(version string) (v *Version, err error) {
v = &Version{}
return v, v.ParseVersion(version)
}
// Parse the version and return it as a Version object.
func (v *Version) ParseVersion(version string) (err error) {
parsedResult := versionRegExp.FindAllStringSubmatch(version, -1)
if len(parsedResult) != 1 {
err = errors.Errorf("Invalid version %s", version)
return
}
if len(parsedResult[0]) != 7 {
err = errors.Errorf("Invalid version %s", version)
return
}
v.Prefix = parsedResult[0][1]
v.Major = v.intOrZero(parsedResult[0][2])
v.Minor = v.intOrZero(parsedResult[0][3])
v.Maintenance = v.intOrZero(parsedResult[0][5])
v.Suffix = parsedResult[0][6]
return
}
// Returns 0 or an int value for the string, errors are returned as 0.
func (v *Version) intOrZero(input string) (value int) {
if input != "" {
value, _ = strconv.Atoi(input)
}
return value
}
// Returns true if this major revision is compatible.
func (v *Version) CompatibleFramework(c *CommandConfig) error {
for i, rv := range frameworkCompatibleRangeList {
start, _ := ParseVersion(rv[0])
end, _ := ParseVersion(rv[1])
if !v.Newer(start) || v.Newer(end) {
continue
}
// Framework is older then 0.20, turn on historic mode
if i == 0 {
c.HistoricMode = true
}
return nil
}
return errors.New("Tool out of date - do a 'go get -u github.com/revel/cmd/revel'")
}
// Returns true if this major revision is newer then the passed in.
func (v *Version) MajorNewer(o *Version) bool {
if v.Major != o.Major {
return v.Major > o.Major
}
return false
}
// Returns true if this major or major and minor revision is newer then the value passed in.
func (v *Version) MinorNewer(o *Version) bool {
if v.Major != o.Major {
return v.Major > o.Major
}
if v.Minor != o.Minor {
return v.Minor > o.Minor
}
return false
}
// Returns true if the version is newer then the current on.
func (v *Version) Newer(o *Version) bool {
if v.Major != o.Major {
return v.Major > o.Major
}
if v.Minor != o.Minor {
return v.Minor > o.Minor
}
if v.Maintenance != o.Maintenance {
return v.Maintenance > o.Maintenance
}
return true
}
// Convert the version to a string.
func (v *Version) VersionString() string {
return fmt.Sprintf("%s%d.%d.%d%s", v.Prefix, v.Major, v.Minor, v.Maintenance, v.Suffix)
}
// Convert the version build date and go version to a string.
func (v *Version) String() string {
return fmt.Sprintf("Version: %s%d.%d.%d%s\nBuild Date: %s\n Minimum Go Version: %s",
v.Prefix, v.Major, v.Minor, v.Maintenance, v.Suffix, v.BuildDate, v.MinGoVersion)
}

35
model/version_test.go Normal file
View File

@@ -0,0 +1,35 @@
package model_test
import (
"testing"
"github.com/revel/cmd/model"
"github.com/stretchr/testify/assert"
)
var versionTests = [][]string{
{"v0.20.0-dev", "v0.20.0-dev"},
{"v0.20-dev", "v0.20.0-dev"},
{"v0.20.", "v0.20.0"},
{"2.0", "2.0.0"},
}
// Test that the event handler can be attached and it dispatches the event received.
func TestVersion(t *testing.T) {
for _, v := range versionTests {
p, e := model.ParseVersion(v[0])
assert.Nil(t, e, "Should have parsed %s", v)
assert.Equal(t, p.String(), v[1], "Should be equal %s==%s", p.String(), v)
}
}
// test the ranges.
func TestVersionRange(t *testing.T) {
a, _ := model.ParseVersion("0.1.2")
b, _ := model.ParseVersion("0.2.1")
c, _ := model.ParseVersion("1.0.1")
assert.True(t, b.MinorNewer(a), "B is newer then A")
assert.False(t, b.MajorNewer(a), "B is not major newer then A")
assert.False(t, b.MajorNewer(c), "B is not major newer then A")
assert.True(t, c.MajorNewer(b), "C is major newer then b")
}

View File

@@ -2,9 +2,10 @@ package parser
import ( import (
"go/ast" "go/ast"
"github.com/revel/cmd/utils"
"github.com/revel/cmd/model"
"go/token" "go/token"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
) )
// If this Decl is a struct type definition, it is summarized and added to specs. // If this Decl is a struct type definition, it is summarized and added to specs.
@@ -91,7 +92,7 @@ func appendStruct(fileName string, specs []*model.TypeInfo, pkgImportPath string
// If decl is a Method declaration, it is summarized and added to the array // If decl is a Method declaration, it is summarized and added to the array
// underneath its receiver type. // underneath its receiver type.
// e.g. "Login" => {MethodSpec, MethodSpec, ..} // e.g. "Login" => {MethodSpec, MethodSpec, ..}.
func appendAction(fset *token.FileSet, mm methodMap, decl ast.Decl, pkgImportPath, pkgName string, imports map[string]string) { func appendAction(fset *token.FileSet, mm methodMap, decl ast.Decl, pkgImportPath, pkgName string, imports map[string]string) {
// Func declaration? // Func declaration?
funcDecl, ok := decl.(*ast.FuncDecl) funcDecl, ok := decl.(*ast.FuncDecl)
@@ -134,7 +135,7 @@ func appendAction(fset *token.FileSet, mm methodMap, decl ast.Decl, pkgImportPat
var importPath string var importPath string
typeExpr := model.NewTypeExprFromAst(pkgName, field.Type) typeExpr := model.NewTypeExprFromAst(pkgName, field.Type)
if !typeExpr.Valid { if !typeExpr.Valid {
utils.Logger.Warn("Warn: Didn't understand argument '%s' of action %s. Ignoring.", name, getFuncName(funcDecl)) utils.Logger.Warnf("Warn: Didn't understand argument '%s' of action %s. Ignoring.", name, getFuncName(funcDecl))
return // We didn't understand one of the args. Ignore this action. return // We didn't understand one of the args. Ignore this action.
} }
// Local object // Local object
@@ -194,7 +195,7 @@ func appendAction(fset *token.FileSet, mm methodMap, decl ast.Decl, pkgImportPat
}) })
var recvTypeName string var recvTypeName string
var recvType = funcDecl.Recv.List[0].Type recvType := funcDecl.Recv.List[0].Type
if recvStarType, ok := recvType.(*ast.StarExpr); ok { if recvStarType, ok := recvType.(*ast.StarExpr); ok {
recvTypeName = recvStarType.X.(*ast.Ident).Name recvTypeName = recvStarType.X.(*ast.Ident).Name
} else { } else {
@@ -204,7 +205,7 @@ func appendAction(fset *token.FileSet, mm methodMap, decl ast.Decl, pkgImportPat
mm[recvTypeName] = append(mm[recvTypeName], method) mm[recvTypeName] = append(mm[recvTypeName], method)
} }
// Combine the 2 source info models into one // Combine the 2 source info models into one.
func appendSourceInfo(srcInfo1, srcInfo2 *model.SourceInfo) *model.SourceInfo { func appendSourceInfo(srcInfo1, srcInfo2 *model.SourceInfo) *model.SourceInfo {
if srcInfo1 == nil { if srcInfo1 == nil {
return srcInfo2 return srcInfo2

View File

@@ -1,15 +1,16 @@
package parser package parser
import ( import (
"github.com/revel/cmd/utils"
"go/ast" "go/ast"
"go/build" "go/build"
"go/token" "go/token"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/revel/cmd/utils"
) )
// Add imports to the map from the source dir // Add imports to the map from the source dir.
func addImports(imports map[string]string, decl ast.Decl, srcDir string) { func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
genDecl, ok := decl.(*ast.GenDecl) genDecl, ok := decl.(*ast.GenDecl)
if !ok { if !ok {
@@ -29,7 +30,10 @@ func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
continue continue
} }
} }
quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\"" quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
if quotedPath == `"C"` {
continue
}
fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes
// If the package was not aliased (common case), we have to import it // If the package was not aliased (common case), we have to import it
@@ -45,7 +49,7 @@ func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
// We expect this to happen for apps using reverse routing (since we // We expect this to happen for apps using reverse routing (since we
// have not yet generated the routes). Don't log that. // have not yet generated the routes). Don't log that.
if !strings.HasSuffix(fullPath, "/app/routes") { if !strings.HasSuffix(fullPath, "/app/routes") {
utils.Logger.Error("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err) utils.Logger.Warn("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err)
} }
continue continue
} else { } else {
@@ -59,10 +63,11 @@ func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
} }
// Returns a valid import string from the path // Returns a valid import string from the path
// using the build.Defaul.GOPATH to determine the root // using the build.Defaul.GOPATH to determine the root.
func importPathFromPath(root string) string { func importPathFromPath(root, basePath string) string {
if vendorIdx := strings.Index(root, "/vendor/"); vendorIdx != -1 { vendorTest := filepath.Join(basePath, "vendor")
return filepath.ToSlash(root[vendorIdx+8:]) if len(root) > len(vendorTest) && root[:len(vendorTest)] == vendorTest {
return filepath.ToSlash(root[len(vendorTest)+1:])
} }
for _, gopath := range filepath.SplitList(build.Default.GOPATH) { for _, gopath := range filepath.SplitList(build.Default.GOPATH) {
srcPath := filepath.Join(gopath, "src") srcPath := filepath.Join(gopath, "src")

View File

@@ -8,6 +8,7 @@ package parser
// It catalogs the controllers, their methods, and their arguments. // It catalogs the controllers, their methods, and their arguments.
import ( import (
"errors"
"go/ast" "go/ast"
"go/parser" "go/parser"
"go/scanner" "go/scanner"
@@ -20,7 +21,7 @@ import (
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
) )
// A container used to support the reflection package // A container used to support the reflection package.
type processContainer struct { type processContainer struct {
root, rootImportPath string // The paths root, rootImportPath string // The paths
paths *model.RevelContainer // The Revel paths paths *model.RevelContainer // The Revel paths
@@ -37,7 +38,7 @@ type methodMap map[string][]*model.MethodSpec
func ProcessSource(paths *model.RevelContainer) (_ *model.SourceInfo, compileError error) { func ProcessSource(paths *model.RevelContainer) (_ *model.SourceInfo, compileError error) {
pc := &processContainer{paths: paths} pc := &processContainer{paths: paths}
for _, root := range paths.CodePaths { for _, root := range paths.CodePaths {
rootImportPath := importPathFromPath(root) rootImportPath := importPathFromPath(root, paths.BasePath)
if rootImportPath == "" { if rootImportPath == "" {
utils.Logger.Info("Skipping empty code path", "path", root) utils.Logger.Info("Skipping empty code path", "path", root)
continue continue
@@ -46,12 +47,15 @@ func ProcessSource(paths *model.RevelContainer) (_ *model.SourceInfo, compileErr
// Start walking the directory tree. // Start walking the directory tree.
compileError = utils.Walk(root, pc.processPath) compileError = utils.Walk(root, pc.processPath)
if compileError != nil {
return
}
} }
return pc.srcInfo, compileError return pc.srcInfo, compileError
} }
// Called during the "walk process" // Called during the "walk process".
func (pc *processContainer) processPath(path string, info os.FileInfo, err error) error { func (pc *processContainer) processPath(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
utils.Logger.Error("Error scanning app source:", "error", err) utils.Logger.Error("Error scanning app source:", "error", err)
@@ -80,9 +84,10 @@ func (pc *processContainer) processPath(path string, info os.FileInfo, err error
0) 0)
if err != nil { if err != nil {
if errList, ok := err.(scanner.ErrorList); ok { var errList scanner.ErrorList
var pos = errList[0].Pos if errors.As(err, &errList) {
newError := &utils.Error{ pos := errList[0].Pos
newError := &utils.SourceError{
SourceType: ".go source", SourceType: ".go source",
Title: "Go Compilation Error", Title: "Go Compilation Error",
Path: pos.Filename, Path: pos.Filename,
@@ -111,7 +116,7 @@ func (pc *processContainer) processPath(path string, info os.FileInfo, err error
// These cannot be included in source code that is not generated specifically as a test // These cannot be included in source code that is not generated specifically as a test
for i := range pkgs { for i := range pkgs {
if len(i) > 6 { if len(i) > 6 {
if string(i[len(i)-5:]) == "_test" { if i[len(i)-5:] == "_test" {
delete(pkgs, i) delete(pkgs, i)
} }
} }
@@ -143,7 +148,7 @@ func (pc *processContainer) processPath(path string, info os.FileInfo, err error
return nil return nil
} }
// Process a single package within a file // Process a single package within a file.
func processPackage(fset *token.FileSet, pkgImportPath, pkgPath string, pkg *ast.Package) *model.SourceInfo { func processPackage(fset *token.FileSet, pkgImportPath, pkgPath string, pkg *ast.Package) *model.SourceInfo {
var ( var (
structSpecs []*model.TypeInfo structSpecs []*model.TypeInfo

View File

@@ -67,7 +67,7 @@ var expectedValidationKeys = []map[int]string{
}, },
} }
// This tests the recording of line number to validation key of the preceeding // This tests the recording of line number to validation key of the preceding
// example source. // example source.
func TestGetValidationKeys(t *testing.T) { func TestGetValidationKeys(t *testing.T) {
fset := token.NewFileSet() fset := token.NewFileSet()

View File

@@ -1,10 +1,11 @@
package parser package parser
import ( import (
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
"go/ast" "go/ast"
"go/token" "go/token"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
) )
// Scan app source code for calls to X.Y(), where X is of type *Validation. // Scan app source code for calls to X.Y(), where X is of type *Validation.

View File

@@ -0,0 +1,431 @@
package parser2
import (
"go/ast"
"go/token"
"path/filepath"
"strings"
"github.com/revel/cmd/logger"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
"golang.org/x/tools/go/packages"
)
type (
SourceInfoProcessor struct {
sourceProcessor *SourceProcessor
}
)
func NewSourceInfoProcessor(sourceProcessor *SourceProcessor) *SourceInfoProcessor {
return &SourceInfoProcessor{sourceProcessor: sourceProcessor}
}
func (s *SourceInfoProcessor) processPackage(p *packages.Package) (sourceInfo *model.SourceInfo) {
sourceInfo = &model.SourceInfo{
ValidationKeys: map[string]map[int]string{},
}
var (
isController = strings.HasSuffix(p.PkgPath, "/controllers") ||
strings.Contains(p.PkgPath, "/controllers/")
isTest = strings.HasSuffix(p.PkgPath, "/tests") ||
strings.Contains(p.PkgPath, "/tests/")
methodMap = map[string][]*model.MethodSpec{}
)
localImportMap := map[string]string{}
log := s.sourceProcessor.log.New("package", p.PkgPath)
log.Info("Processing package")
for _, tree := range p.Syntax {
for _, decl := range tree.Decls {
s.sourceProcessor.packageMap[p.PkgPath] = filepath.Dir(p.Fset.Position(decl.Pos()).Filename)
if !s.addImport(decl, p, localImportMap, log) {
continue
}
spec, found := s.getStructTypeDecl(decl, p.Fset)
// log.Info("Checking file","filename", p.Fset.Position(decl.Pos()).Filename,"found",found)
if found {
if isController || isTest {
controllerSpec := s.getControllerSpec(spec, p, localImportMap)
sourceInfo.StructSpecs = append(sourceInfo.StructSpecs, controllerSpec)
}
} else {
// Not a type definition, this could be a method for a controller try to extract that
// Func declaration?
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
// This could be a controller action endpoint, check and add if needed
if isController &&
funcDecl.Recv != nil && // Must have a receiver
funcDecl.Name.IsExported() && // be public
funcDecl.Type.Results != nil && len(funcDecl.Type.Results.List) == 1 {
// return one result
if m, receiver := s.getControllerFunc(funcDecl, p, localImportMap); m != nil {
methodMap[receiver] = append(methodMap[receiver], m)
log.Info("Added method map to ", "receiver", receiver, "method", m.Name)
}
}
// Check for validation
if lineKeyMap := s.getValidation(funcDecl, p); len(lineKeyMap) > 1 {
sourceInfo.ValidationKeys[p.PkgPath+"."+s.getFuncName(funcDecl)] = lineKeyMap
}
if funcDecl.Name.Name == "init" {
sourceInfo.InitImportPaths = append(sourceInfo.InitImportPaths, p.PkgPath)
}
}
}
}
// Add the method specs to the struct specs.
for _, spec := range sourceInfo.StructSpecs {
spec.MethodSpecs = methodMap[spec.StructName]
}
return
}
// Scan app source code for calls to X.Y(), where X is of type *Validation.
//
// Recognize these scenarios:
// - "Y" = "Validation" and is a member of the receiver.
// (The common case for inline validation)
// - "X" is passed in to the func as a parameter.
// (For structs implementing Validated)
//
// The line number to which a validation call is attributed is that of the
// surrounding ExprStmt. This is so that it matches what runtime.Callers()
// reports.
//
// The end result is that we can set the default validation key for each call to
// be the same as the local variable.
func (s *SourceInfoProcessor) getValidation(funcDecl *ast.FuncDecl, p *packages.Package) map[int]string {
var (
lineKeys = make(map[int]string)
// Check the func parameters and the receiver's members for the *revel.Validation type.
validationParam = s.getValidationParameter(funcDecl)
)
ast.Inspect(funcDecl.Body, func(node ast.Node) bool {
// e.g. c.Validation.Required(arg) or v.Required(arg)
callExpr, ok := node.(*ast.CallExpr)
if !ok {
return true
}
// e.g. c.Validation.Required or v.Required
funcSelector, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
switch x := funcSelector.X.(type) {
case *ast.SelectorExpr: // e.g. c.Validation
if x.Sel.Name != "Validation" {
return true
}
case *ast.Ident: // e.g. v
if validationParam == nil || x.Obj != validationParam {
return true
}
default:
return true
}
if len(callExpr.Args) == 0 {
return true
}
// Given the validation expression, extract the key.
key := callExpr.Args[0]
switch expr := key.(type) {
case *ast.BinaryExpr:
// If the argument is a binary expression, take the first expression.
// (e.g. c.Validation.Required(myName != ""))
key = expr.X
case *ast.UnaryExpr:
// If the argument is a unary expression, drill in.
// (e.g. c.Validation.Required(!myBool)
key = expr.X
case *ast.BasicLit:
// If it's a literal, skip it.
return true
}
if typeExpr := model.NewTypeExprFromAst("", key); typeExpr.Valid {
lineKeys[p.Fset.Position(callExpr.End()).Line] = typeExpr.TypeName("")
} else {
s.sourceProcessor.log.Error("Error: Failed to generate key for field validation. Make sure the field name is valid.", "file", p.PkgPath,
"line", p.Fset.Position(callExpr.End()).Line, "function", funcDecl.Name.String())
}
return true
})
return lineKeys
}
// Check to see if there is a *revel.Validation as an argument.
func (s *SourceInfoProcessor) getValidationParameter(funcDecl *ast.FuncDecl) *ast.Object {
for _, field := range funcDecl.Type.Params.List {
starExpr, ok := field.Type.(*ast.StarExpr) // e.g. *revel.Validation
if !ok {
continue
}
selExpr, ok := starExpr.X.(*ast.SelectorExpr) // e.g. revel.Validation
if !ok {
continue
}
xIdent, ok := selExpr.X.(*ast.Ident) // e.g. rev
if !ok {
continue
}
if selExpr.Sel.Name == "Validation" && s.sourceProcessor.importMap[xIdent.Name] == model.RevelImportPath {
return field.Names[0].Obj
}
}
return nil
}
func (s *SourceInfoProcessor) getControllerFunc(funcDecl *ast.FuncDecl, p *packages.Package, localImportMap map[string]string) (method *model.MethodSpec, recvTypeName string) {
selExpr, ok := funcDecl.Type.Results.List[0].Type.(*ast.SelectorExpr)
if !ok {
return
}
if selExpr.Sel.Name != "Result" {
return
}
if pkgIdent, ok := selExpr.X.(*ast.Ident); !ok || s.sourceProcessor.importMap[pkgIdent.Name] != model.RevelImportPath {
return
}
method = &model.MethodSpec{
Name: funcDecl.Name.Name,
}
// Add a description of the arguments to the method.
for _, field := range funcDecl.Type.Params.List {
for _, name := range field.Names {
var importPath string
typeExpr := model.NewTypeExprFromAst(p.Name, field.Type)
if !typeExpr.Valid {
utils.Logger.Warn("Warn: Didn't understand argument '%s' of action %s. Ignoring.", name, s.getFuncName(funcDecl))
return // We didn't understand one of the args. Ignore this action.
}
// Local object
if typeExpr.PkgName == p.Name {
importPath = p.PkgPath
} else if typeExpr.PkgName != "" {
var ok bool
if importPath, ok = localImportMap[typeExpr.PkgName]; !ok {
if importPath, ok = s.sourceProcessor.importMap[typeExpr.PkgName]; !ok {
utils.Logger.Error("Unable to find import", "importMap", s.sourceProcessor.importMap, "localimport", localImportMap)
utils.Logger.Fatalf("Failed to find import for arg of type: %s , %s", typeExpr.PkgName, typeExpr.TypeName(""))
}
}
}
method.Args = append(method.Args, &model.MethodArg{
Name: name.Name,
TypeExpr: typeExpr,
ImportPath: importPath,
})
}
}
// Add a description of the calls to Render from the method.
// Inspect every node (e.g. always return true).
method.RenderCalls = []*model.MethodCall{}
ast.Inspect(funcDecl.Body, func(node ast.Node) bool {
// Is it a function call?
callExpr, ok := node.(*ast.CallExpr)
if !ok {
return true
}
// Is it calling (*Controller).Render?
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
// The type of the receiver is not easily available, so just store every
// call to any method called Render.
if selExpr.Sel.Name != "Render" {
return true
}
// Add this call's args to the renderArgs.
pos := p.Fset.Position(callExpr.Lparen)
methodCall := &model.MethodCall{
Line: pos.Line,
Names: []string{},
}
for _, arg := range callExpr.Args {
argIdent, ok := arg.(*ast.Ident)
if !ok {
continue
}
methodCall.Names = append(methodCall.Names, argIdent.Name)
}
method.RenderCalls = append(method.RenderCalls, methodCall)
return true
})
recvType := funcDecl.Recv.List[0].Type
if recvStarType, ok := recvType.(*ast.StarExpr); ok {
recvTypeName = recvStarType.X.(*ast.Ident).Name
} else {
recvTypeName = recvType.(*ast.Ident).Name
}
return
}
func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.Package, localImportMap map[string]string) (controllerSpec *model.TypeInfo) {
structType := spec.Type.(*ast.StructType)
// At this point we know it's a type declaration for a struct.
// Fill in the rest of the info by diving into the fields.
// Add it provisionally to the Controller list -- it's later filtered using field info.
controllerSpec = &model.TypeInfo{
StructName: spec.Name.Name,
ImportPath: p.PkgPath,
PackageName: p.Name,
}
log := s.sourceProcessor.log.New("file", p.Fset.Position(spec.Pos()).Filename, "position", p.Fset.Position(spec.Pos()).Line)
for _, field := range structType.Fields.List {
// If field.Names is set, it's not an embedded type.
if field.Names != nil {
continue
}
// A direct "sub-type" has an ast.Field as either:
// Ident { "AppController" }
// SelectorExpr { "rev", "Controller" }
// Additionally, that can be wrapped by StarExprs.
fieldType := field.Type
pkgName, typeName := func() (string, string) {
// Drill through any StarExprs.
for {
if starExpr, ok := fieldType.(*ast.StarExpr); ok {
fieldType = starExpr.X
continue
}
break
}
// If the embedded type is in the same package, it's an Ident.
if ident, ok := fieldType.(*ast.Ident); ok {
return "", ident.Name
}
if selectorExpr, ok := fieldType.(*ast.SelectorExpr); ok {
if pkgIdent, ok := selectorExpr.X.(*ast.Ident); ok {
return pkgIdent.Name, selectorExpr.Sel.Name
}
}
return "", ""
}()
// If a typename wasn't found, skip it.
if typeName == "" {
continue
}
// Find the import path for this type.
// If it was referenced without a package name, use the current package import path.
// Else, look up the package's import path by name.
var importPath string
if pkgName == "" {
importPath = p.PkgPath
} else {
var ok bool
if importPath, ok = localImportMap[pkgName]; !ok {
log.Debug("Debug: Unusual, failed to find package locally ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap, "usedin")
if importPath, ok = s.sourceProcessor.importMap[pkgName]; !ok {
log.Error("Error: Failed to find import path for ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap, "usedin")
continue
}
}
}
controllerSpec.EmbeddedTypes = append(controllerSpec.EmbeddedTypes, &model.EmbeddedTypeName{
ImportPath: importPath,
StructName: typeName,
})
}
s.sourceProcessor.log.Info("Added controller spec", "name", controllerSpec.StructName, "package", controllerSpec.ImportPath)
return
}
func (s *SourceInfoProcessor) getStructTypeDecl(decl ast.Decl, fset *token.FileSet) (spec *ast.TypeSpec, found bool) {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
return
}
if genDecl.Tok != token.TYPE {
return
}
if len(genDecl.Specs) == 0 {
utils.Logger.Warn("Warn: Surprising: %s:%d Decl contains no specifications", fset.Position(decl.Pos()).Filename, fset.Position(decl.Pos()).Line)
return
}
spec = genDecl.Specs[0].(*ast.TypeSpec)
_, found = spec.Type.(*ast.StructType)
return
}
func (s *SourceInfoProcessor) getFuncName(funcDecl *ast.FuncDecl) string {
prefix := ""
if funcDecl.Recv != nil {
recvType := funcDecl.Recv.List[0].Type
if recvStarType, ok := recvType.(*ast.StarExpr); ok {
prefix = "(*" + recvStarType.X.(*ast.Ident).Name + ")"
} else {
prefix = recvType.(*ast.Ident).Name
}
prefix += "."
}
return prefix + funcDecl.Name.Name
}
func (s *SourceInfoProcessor) addImport(decl ast.Decl, p *packages.Package, localImportMap map[string]string, log logger.MultiLogger) (shouldContinue bool) {
shouldContinue = true
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
return
}
if genDecl.Tok == token.IMPORT {
shouldContinue = false
for _, spec := range genDecl.Specs {
importSpec := spec.(*ast.ImportSpec)
// fmt.Printf("*** import specification %#v\n", importSpec)
var pkgAlias string
if importSpec.Name != nil {
pkgAlias = importSpec.Name.Name
if pkgAlias == "_" {
continue
}
}
quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes
if pkgAlias == "" {
pkgAlias = fullPath
if index := strings.LastIndex(pkgAlias, "/"); index > 0 {
pkgAlias = pkgAlias[index+1:]
}
}
localImportMap[pkgAlias] = fullPath
}
}
return
}

253
parser2/source_processor.go Normal file
View File

@@ -0,0 +1,253 @@
package parser2
import (
"errors"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"os"
"path/filepath"
"strings"
"github.com/revel/cmd/logger"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
"golang.org/x/tools/go/packages"
)
type (
SourceProcessor struct {
revelContainer *model.RevelContainer
log logger.MultiLogger
packageList []*packages.Package
importMap map[string]string
packageMap map[string]string
sourceInfoProcessor *SourceInfoProcessor
sourceInfo *model.SourceInfo
}
)
func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.SourceInfo, compileError error) {
utils.Logger.Info("ProcessSource")
processor := NewSourceProcessor(revelContainer)
compileError = processor.parse()
sourceInfo = processor.sourceInfo
if compileError == nil {
processor.log.Infof("From parsers : Structures:%d InitImports:%d ValidationKeys:%d %v", len(sourceInfo.StructSpecs), len(sourceInfo.InitImportPaths), len(sourceInfo.ValidationKeys), sourceInfo.PackageMap)
}
return
}
func NewSourceProcessor(revelContainer *model.RevelContainer) *SourceProcessor {
s := &SourceProcessor{revelContainer: revelContainer, log: utils.Logger.New("parser", "SourceProcessor")}
s.sourceInfoProcessor = NewSourceInfoProcessor(s)
return s
}
func (s *SourceProcessor) parse() (compileError error) {
print("Parsing packages, (may require download if not cached)...")
if compileError = s.addPackages(); compileError != nil {
return
}
println(" Completed")
if compileError = s.addImportMap(); compileError != nil {
return
}
if compileError = s.addSourceInfo(); compileError != nil {
return
}
s.sourceInfo.PackageMap = map[string]string{}
getImportFromMap := func(packagePath string) string {
for path := range s.packageMap {
if strings.Index(path, packagePath) == 0 {
fullPath := s.packageMap[path]
return fullPath[:(len(fullPath) - len(path) + len(packagePath))]
}
}
return ""
}
s.sourceInfo.PackageMap[model.RevelImportPath] = getImportFromMap(model.RevelImportPath)
s.sourceInfo.PackageMap[s.revelContainer.ImportPath] = getImportFromMap(s.revelContainer.ImportPath)
for _, module := range s.revelContainer.ModulePathMap {
s.sourceInfo.PackageMap[module.ImportPath] = getImportFromMap(module.ImportPath)
}
return
}
// Using the packages.Load function load all the packages and type specifications (forces compile).
// this sets the SourceProcessor.packageList []*packages.Package.
func (s *SourceProcessor) addPackages() (err error) {
allPackages := []string{model.RevelImportPath + "/..."}
for _, module := range s.revelContainer.ModulePathMap {
allPackages = append(allPackages, module.ImportPath+"/...") // +"/app/controllers/...")
}
s.log.Info("Reading packages", "packageList", allPackages)
// allPackages = []string{s.revelContainer.ImportPath + "/..."} //+"/app/controllers/..."}
config := &packages.Config{
// ode: packages.NeedSyntax | packages.NeedCompiledGoFiles,
Mode: packages.NeedTypes | // For compile error
packages.NeedDeps | // To load dependent files
packages.NeedName | // Loads the full package name
packages.NeedSyntax, // To load ast tree (for end points)
// Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
// packages.NeedImports | packages.NeedDeps | packages.NeedExportsFile |
// packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo |
// packages.NeedTypesSizes,
// Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps | packages.NeedExportsFile | packages.NeedFiles |
// packages.NeedCompiledGoFiles | packages.NeedTypesSizes |
// packages.NeedSyntax | packages.NeedCompiledGoFiles ,
// Mode: packages.NeedSyntax | packages.NeedCompiledGoFiles | packages.NeedName | packages.NeedFiles |
// packages.LoadTypes | packages.NeedTypes | packages.NeedDeps, //, // |
// packages.NeedTypes, // packages.LoadTypes | packages.NeedSyntax | packages.NeedTypesInfo,
// packages.LoadSyntax | packages.NeedDeps,
Dir: s.revelContainer.AppPath,
}
config.Env = utils.ReducedEnv(false)
s.packageList, err = packages.Load(config, allPackages...)
s.log.Info("Loaded modules ", "len results", len(s.packageList), "error", err)
// Now process the files in the aap source folder s.revelContainer.ImportPath + "/...",
err = utils.Walk(s.revelContainer.BasePath, s.processPath)
s.log.Info("Loaded apps and modules ", "len results", len(s.packageList), "error", err)
return
}
// This callback is used to build the packages for the "app" package. This allows us to
// parse the source files without doing a full compile on them
// This callback only processes folders, so any files passed to this will return a nil.
func (s *SourceProcessor) processPath(path string, info os.FileInfo, err error) error {
if err != nil {
s.log.Error("Error scanning app source:", "error", err)
return nil
}
// Ignore files and folders not marked tmp (since those are generated)
if !info.IsDir() || info.Name() == "tmp" {
return nil
}
// Real work for processing the folder
pkgImportPath := s.revelContainer.ImportPath
appPath := s.revelContainer.BasePath
if appPath != path {
pkgImportPath = s.revelContainer.ImportPath + "/" + filepath.ToSlash(path[len(appPath)+1:])
}
s.log.Info("Processing source package folder", "package", pkgImportPath, "path", path)
// Parse files within the path.
var pkgMap map[string]*ast.Package
fset := token.NewFileSet()
pkgMap, err = parser.ParseDir(
fset,
path,
func(f os.FileInfo) bool {
return !f.IsDir() && !strings.HasPrefix(f.Name(), ".") && strings.HasSuffix(f.Name(), ".go")
},
0)
if err != nil {
var errList scanner.ErrorList
if errors.As(err, &errList) {
pos := errList[0].Pos
newError := &utils.SourceError{
SourceType: ".go source",
Title: "Go Compilation Error",
Path: pos.Filename,
Description: errList[0].Msg,
Line: pos.Line,
Column: pos.Column,
SourceLines: utils.MustReadLines(pos.Filename),
}
errorLink := s.revelContainer.Config.StringDefault("error.link", "")
if errorLink != "" {
newError.SetLink(errorLink)
}
return newError
}
// This is exception, err already checked above. Here just a print
ast.Print(nil, err)
s.log.Fatal("Failed to parse dir", "error", err)
}
// Skip "main" packages.
delete(pkgMap, "main")
// Ignore packages that end with _test
// These cannot be included in source code that is not generated specifically as a test
for i := range pkgMap {
if len(i) > 6 {
if i[len(i)-5:] == "_test" {
delete(pkgMap, i)
}
}
}
// If there is no code in this directory, skip it.
if len(pkgMap) == 0 {
return nil
}
// There should be only one package in this directory.
if len(pkgMap) > 1 {
for i := range pkgMap {
println("Found duplicate packages in single directory ", i)
}
utils.Logger.Fatal("Most unexpected! Multiple packages in a single directory:", "packages", pkgMap)
}
// At this point there is only one package in the pkgs map,
p := &packages.Package{}
p.PkgPath = pkgImportPath
p.Fset = fset
for _, pkg := range pkgMap {
p.Name = pkg.Name
s.log.Info("Found package", "pkg.Name", pkg.Name, "p.Name", p.PkgPath)
for filename, astFile := range pkg.Files {
p.Syntax = append(p.Syntax, astFile)
p.GoFiles = append(p.GoFiles, filename)
}
}
s.packageList = append(s.packageList, p)
return nil
}
// This function is used to populate a map so that we can lookup controller embedded types in order to determine
// if a Struct inherits from from revel.Controller.
func (s *SourceProcessor) addImportMap() (err error) {
s.importMap = map[string]string{}
s.packageMap = map[string]string{}
for _, p := range s.packageList {
if len(p.Errors) > 0 {
// Generate a compile error
for _, e := range p.Errors {
s.log.Info("While reading packages encountered import error ignoring ", "PkgPath", p.PkgPath, "error", e)
}
}
for _, tree := range p.Syntax {
s.importMap[tree.Name.Name] = p.PkgPath
}
}
return
}
func (s *SourceProcessor) addSourceInfo() (err error) {
for _, p := range s.packageList {
if sourceInfo := s.sourceInfoProcessor.processPackage(p); sourceInfo != nil {
if s.sourceInfo != nil {
s.sourceInfo.Merge(sourceInfo)
} else {
s.sourceInfo = sourceInfo
}
}
}
return
}

View File

@@ -9,5 +9,4 @@
// 2. Monitor the user source and restart the program when necessary. // 2. Monitor the user source and restart the program when necessary.
// //
// Source files are generated in the app/tmp directory. // Source files are generated in the app/tmp directory.
package proxy package proxy

View File

@@ -5,15 +5,14 @@
package main package main
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"fmt"
"github.com/revel/cmd/harness" "github.com/revel/cmd/harness"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"go/build"
) )
var cmdBuild = &Command{ var cmdBuild = &Command{
@@ -35,9 +34,15 @@ func init() {
cmdBuild.UpdateConfig = updateBuildConfig cmdBuild.UpdateConfig = updateBuildConfig
} }
// The update config updates the configuration command so that it can run // The update config updates the configuration command so that it can run.
func updateBuildConfig(c *model.CommandConfig, args []string) bool { func updateBuildConfig(c *model.CommandConfig, args []string) bool {
c.Index = model.BUILD c.Index = model.BUILD
if c.Build.TargetPath == "" {
c.Build.TargetPath = "target"
}
if len(args) == 0 && c.Build.ImportPath != "" {
return true
}
// If arguments were passed in then there must be two // If arguments were passed in then there must be two
if len(args) < 2 { if len(args) < 2 {
fmt.Fprintf(os.Stderr, "%s\n%s", cmdBuild.UsageLine, cmdBuild.Long) fmt.Fprintf(os.Stderr, "%s\n%s", cmdBuild.UsageLine, cmdBuild.Long)
@@ -52,7 +57,7 @@ func updateBuildConfig(c *model.CommandConfig, args []string) bool {
return true return true
} }
// The main entry point to build application from command line // The main entry point to build application from command line.
func buildApp(c *model.CommandConfig) (err error) { func buildApp(c *model.CommandConfig) (err error) {
appImportPath, destPath, mode := c.ImportPath, c.Build.TargetPath, DefaultRunMode appImportPath, destPath, mode := c.ImportPath, c.Build.TargetPath, DefaultRunMode
if len(c.Build.Mode) > 0 { if len(c.Build.Mode) > 0 {
@@ -62,16 +67,19 @@ func buildApp(c *model.CommandConfig) (err error) {
// Convert target to absolute path // Convert target to absolute path
c.Build.TargetPath, _ = filepath.Abs(destPath) c.Build.TargetPath, _ = filepath.Abs(destPath)
c.Build.Mode = mode c.Build.Mode = mode
c.Build.ImportPath = appImportPath
revel_paths, err := model.NewRevelPaths(mode, appImportPath, "", model.NewWrappedRevelCallback(nil, c.PackageResolver)) revelPaths, err := model.NewRevelPaths(mode, appImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
if err != nil { if err != nil {
return return
} }
buildSafetyCheck(destPath) if err = buildSafetyCheck(destPath); err != nil {
return
}
// Ensure the application can be built, this generates the main file // Ensure the application can be built, this generates the main file
app, err := harness.Build(c, revel_paths) app, err := harness.Build(c, revelPaths)
if err != nil { if err != nil {
return err return err
} }
@@ -82,11 +90,11 @@ func buildApp(c *model.CommandConfig) (err error) {
// - revel // - revel
// - app // - app
packageFolders, err := buildCopyFiles(c, app, revel_paths) packageFolders, err := buildCopyFiles(c, app, revelPaths)
if err != nil { if err != nil {
return return
} }
err = buildCopyModules(c, revel_paths, packageFolders) err = buildCopyModules(c, revelPaths, packageFolders, app)
if err != nil { if err != nil {
return return
} }
@@ -97,36 +105,36 @@ func buildApp(c *model.CommandConfig) (err error) {
return return
} }
// Copy the files to the target // Copy the files to the target.
func buildCopyFiles(c *model.CommandConfig, app *harness.App, revel_paths *model.RevelContainer) (packageFolders []string, err error) { func buildCopyFiles(c *model.CommandConfig, app *harness.App, revelPaths *model.RevelContainer) (packageFolders []string, err error) {
appImportPath, destPath := c.ImportPath, c.Build.TargetPath appImportPath, destPath := c.ImportPath, c.Build.TargetPath
// Revel and the app are in a directory structure mirroring import path // Revel and the app are in a directory structure mirroring import path
srcPath := filepath.Join(destPath, "src") srcPath := filepath.Join(destPath, "src")
destBinaryPath := filepath.Join(destPath, filepath.Base(app.BinaryPath)) destBinaryPath := filepath.Join(destPath, filepath.Base(app.BinaryPath))
tmpRevelPath := filepath.Join(srcPath, filepath.FromSlash(model.RevelImportPath)) tmpRevelPath := filepath.Join(srcPath, filepath.FromSlash(model.RevelImportPath))
if err = utils.CopyFile(destBinaryPath, app.BinaryPath); err != nil { if err = utils.CopyFile(destBinaryPath, filepath.Join(revelPaths.BasePath, app.BinaryPath)); err != nil {
return return
} }
utils.MustChmod(destBinaryPath, 0755) utils.MustChmod(destBinaryPath, 0755)
// Copy the templates from the revel // Copy the templates from the revel
if err = utils.CopyDir(filepath.Join(tmpRevelPath, "conf"), filepath.Join(revel_paths.RevelPath, "conf"), nil); err != nil { if err = utils.CopyDir(filepath.Join(tmpRevelPath, "conf"), filepath.Join(revelPaths.RevelPath, "conf"), nil); err != nil {
return return
} }
if err = utils.CopyDir(filepath.Join(tmpRevelPath, "templates"), filepath.Join(revel_paths.RevelPath, "templates"), nil); err != nil { if err = utils.CopyDir(filepath.Join(tmpRevelPath, "templates"), filepath.Join(revelPaths.RevelPath, "templates"), nil); err != nil {
return return
} }
// Get the folders to be packaged // Get the folders to be packaged
packageFolders = strings.Split(revel_paths.Config.StringDefault("package.folders", "conf,public,app/views"), ",") packageFolders = strings.Split(revelPaths.Config.StringDefault("package.folders", "conf,public,app/views"), ",")
for i, p := range packageFolders { for i, p := range packageFolders {
// Clean spaces, reformat slash to filesystem // Clean spaces, reformat slash to filesystem
packageFolders[i] = filepath.FromSlash(strings.TrimSpace(p)) packageFolders[i] = filepath.FromSlash(strings.TrimSpace(p))
} }
if c.Build.CopySource { if c.Build.CopySource {
err = utils.CopyDir(filepath.Join(srcPath, filepath.FromSlash(appImportPath)), revel_paths.BasePath, nil) err = utils.CopyDir(filepath.Join(srcPath, filepath.FromSlash(appImportPath)), revelPaths.BasePath, nil)
if err != nil { if err != nil {
return return
} }
@@ -134,7 +142,7 @@ func buildCopyFiles(c *model.CommandConfig, app *harness.App, revel_paths *model
for _, folder := range packageFolders { for _, folder := range packageFolders {
err = utils.CopyDir( err = utils.CopyDir(
filepath.Join(srcPath, filepath.FromSlash(appImportPath), folder), filepath.Join(srcPath, filepath.FromSlash(appImportPath), folder),
filepath.Join(revel_paths.BasePath, folder), filepath.Join(revelPaths.BasePath, folder),
nil) nil)
if err != nil { if err != nil {
return return
@@ -145,15 +153,15 @@ func buildCopyFiles(c *model.CommandConfig, app *harness.App, revel_paths *model
return return
} }
// Based on the section copy over the build modules // Based on the section copy over the build modules.
func buildCopyModules(c *model.CommandConfig, revel_paths *model.RevelContainer, packageFolders []string) (err error) { func buildCopyModules(c *model.CommandConfig, revelPaths *model.RevelContainer, packageFolders []string, app *harness.App) (err error) {
destPath := filepath.Join(c.Build.TargetPath, "src") destPath := filepath.Join(c.Build.TargetPath, "src")
// Find all the modules used and copy them over. // Find all the modules used and copy them over.
config := revel_paths.Config.Raw() config := revelPaths.Config.Raw()
modulePaths := make(map[string]string) // import path => filesystem path
// We should only copy over the section of options what the build is targeted for // We should only copy over the section of options what the build is targeted for
// We will default to prod // We will default to prod
moduleImportList := []string{}
for _, section := range config.Sections() { for _, section := range config.Sections() {
// If the runmode is defined we will only import modules defined for that run mode // If the runmode is defined we will only import modules defined for that run mode
if c.Build.Mode != "" && c.Build.Mode != section { if c.Build.Mode != "" && c.Build.Mode != section {
@@ -168,17 +176,13 @@ func buildCopyModules(c *model.CommandConfig, revel_paths *model.RevelContainer,
if moduleImportPath == "" { if moduleImportPath == "" {
continue continue
} }
moduleImportList = append(moduleImportList, moduleImportPath)
modPkg, err := build.Import(moduleImportPath, revel_paths.RevelPath, build.FindOnly)
if err != nil {
utils.Logger.Fatalf("Failed to load module %s (%s): %s", key[len("module."):], c.ImportPath, err)
}
modulePaths[moduleImportPath] = modPkg.Dir
} }
} }
// Copy the the paths for each of the modules // Copy the the paths for each of the modules
for importPath, fsPath := range modulePaths { for _, importPath := range moduleImportList {
fsPath := app.PackagePathMap[importPath]
utils.Logger.Info("Copy files ", "to", filepath.Join(destPath, importPath), "from", fsPath) utils.Logger.Info("Copy files ", "to", filepath.Join(destPath, importPath), "from", fsPath)
if c.Build.CopySource { if c.Build.CopySource {
err = utils.CopyDir(filepath.Join(destPath, importPath), fsPath, nil) err = utils.CopyDir(filepath.Join(destPath, importPath), fsPath, nil)
@@ -201,7 +205,7 @@ func buildCopyModules(c *model.CommandConfig, revel_paths *model.RevelContainer,
return return
} }
// Write the run scripts for the build // Write the run scripts for the build.
func buildWriteScripts(c *model.CommandConfig, app *harness.App) (err error) { func buildWriteScripts(c *model.CommandConfig, app *harness.App) (err error) {
tmplData := map[string]interface{}{ tmplData := map[string]interface{}{
"BinName": filepath.Base(app.BinaryPath), "BinName": filepath.Base(app.BinaryPath),
@@ -232,9 +236,8 @@ func buildWriteScripts(c *model.CommandConfig, app *harness.App) (err error) {
return return
} }
// Checks to see if the target folder exists and can be created // Checks to see if the target folder exists and can be created.
func buildSafetyCheck(destPath string) error { func buildSafetyCheck(destPath string) error {
// First, verify that it is either already empty or looks like a previous // First, verify that it is either already empty or looks like a previous
// build (to avoid clobbering anything) // build (to avoid clobbering anything)
if utils.Exists(destPath) && !utils.Empty(destPath) && !utils.Exists(filepath.Join(destPath, "run.sh")) { if utils.Exists(destPath) && !utils.Empty(destPath) && !utils.Exists(filepath.Join(destPath, "run.sh")) {
@@ -256,6 +259,7 @@ const PACKAGE_RUN_SH = `#!/bin/sh
SCRIPTPATH=$(cd "$(dirname "$0")"; pwd) SCRIPTPATH=$(cd "$(dirname "$0")"; pwd)
"$SCRIPTPATH/{{.BinName}}" -importPath {{.ImportPath}} -srcPath "$SCRIPTPATH/src" -runMode {{.Mode}} "$SCRIPTPATH/{{.BinName}}" -importPath {{.ImportPath}} -srcPath "$SCRIPTPATH/src" -runMode {{.Mode}}
` `
const PACKAGE_RUN_BAT = `@echo off const PACKAGE_RUN_BAT = `@echo off
{{.BinName}} -importPath {{.ImportPath}} -srcPath "%CD%\src" -runMode {{.Mode}} {{.BinName}} -importPath {{.ImportPath}} -srcPath "%CD%\src" -runMode {{.Mode}}

View File

@@ -1,24 +1,25 @@
package main_test package main_test
import ( import (
"github.com/revel/cmd/model"
"github.com/revel/cmd/revel"
"github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/revel/cmd/model"
main "github.com/revel/cmd/revel"
"github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert"
) )
// test the commands // test the commands.
func TestBuild(t *testing.T) { func TestBuild(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-build", a) gopath := setup("revel-test-build", a)
t.Run("Build", func(t *testing.T) { t.Run("Build", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("build-test", model.NEW, nil, a) c := newApp("build-test", model.NEW, nil, a)
main.Commands[model.NEW].RunWith(c) a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.BUILD c.Index = model.BUILD
c.Build.TargetPath = filepath.Join(gopath, "build-test", "target") c.Build.TargetPath = filepath.Join(gopath, "build-test", "target")
c.Build.ImportPath = c.ImportPath c.Build.ImportPath = c.ImportPath
@@ -26,6 +27,21 @@ func TestBuild(t *testing.T) {
a.True(utils.Exists(filepath.Join(gopath, "build-test", "target"))) a.True(utils.Exists(filepath.Join(gopath, "build-test", "target")))
}) })
t.Run("Build-withFlags", func(t *testing.T) {
a := assert.New(t)
c := newApp("build-test-WithFlags", model.NEW, nil, a)
c.BuildFlags = []string{
"build-test-WithFlags/app.AppVersion=SomeValue",
"build-test-WithFlags/app.SomeOtherValue=SomeValue",
}
a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.BUILD
c.Build.TargetPath = filepath.Join(gopath, "build-test", "target")
c.Build.ImportPath = c.ImportPath
a.Nil(main.Commands[model.BUILD].RunWith(c), "Failed to run build-test-withFlags")
a.True(utils.Exists(filepath.Join(gopath, "build-test", "target")))
})
if !t.Failed() { if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil { if err := os.RemoveAll(gopath); err != nil {
a.Fail("Failed to remove test path") a.Fail("Failed to remove test path")

View File

@@ -6,11 +6,11 @@ package main
import ( import (
"fmt" "fmt"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
"go/build"
"os" "os"
"path/filepath" "path/filepath"
"github.com/revel/cmd/model"
"github.com/revel/cmd/utils"
) )
var cmdClean = &Command{ var cmdClean = &Command{
@@ -34,9 +34,12 @@ func init() {
cmdClean.RunWith = cleanApp cmdClean.RunWith = cleanApp
} }
// Update the clean command configuration, using old method // Update the clean command configuration, using old method.
func updateCleanConfig(c *model.CommandConfig, args []string) bool { func updateCleanConfig(c *model.CommandConfig, args []string) bool {
c.Index = model.CLEAN c.Index = model.CLEAN
if len(args) == 0 && c.Clean.ImportPath != "" {
return true
}
if len(args) == 0 { if len(args) == 0 {
fmt.Fprintf(os.Stderr, cmdClean.Long) fmt.Fprintf(os.Stderr, cmdClean.Long)
return false return false
@@ -45,16 +48,11 @@ func updateCleanConfig(c *model.CommandConfig, args []string) bool {
return true return true
} }
// Clean the source directory of generated files // Clean the source directory of generated files.
func cleanApp(c *model.CommandConfig) (err error) { func cleanApp(c *model.CommandConfig) (err error) {
appPkg, err := build.Import(c.ImportPath, "", build.FindOnly)
if err != nil {
utils.Logger.Fatal("Abort: Failed to find import path:", "error", err)
}
purgeDirs := []string{ purgeDirs := []string{
filepath.Join(appPkg.Dir, "app", "tmp"), filepath.Join(c.AppPath, "app", "tmp"),
filepath.Join(appPkg.Dir, "app", "routes"), filepath.Join(c.AppPath, "app", "routes"),
} }
for _, dir := range purgeDirs { for _, dir := range purgeDirs {

View File

@@ -1,33 +1,36 @@
package main_test package main_test
import ( import (
"github.com/revel/cmd/model"
"github.com/revel/cmd/revel"
"github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/revel/cmd/model"
main "github.com/revel/cmd/revel"
"github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert"
) )
// test the commands // test the commands.
func TestClean(t *testing.T) { func TestClean(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-clean", a) gopath := setup("revel-test-clean", a)
t.Run("Clean", func(t *testing.T) { t.Run("Clean", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("clean-test", model.NEW, nil, a) c := newApp("clean-test", model.NEW, nil, a)
main.Commands[model.NEW].RunWith(c)
a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.TEST c.Index = model.TEST
main.Commands[model.TEST].RunWith(c) a.Nil(main.Commands[model.TEST].RunWith(c), "failed to run test")
a.True(utils.Exists(filepath.Join(gopath, "src", "clean-test", "app", "tmp", "main.go")),
"Missing main from path "+filepath.Join(gopath, "src", "clean-test", "app", "tmp", "main.go")) a.True(utils.Exists(filepath.Join(gopath, "clean-test", "app", "tmp", "main.go")),
"Missing main from path "+filepath.Join(gopath, "clean-test", "app", "tmp", "main.go"))
c.Clean.ImportPath = c.ImportPath c.Clean.ImportPath = c.ImportPath
a.Nil(main.Commands[model.CLEAN].RunWith(c), "Failed to run clean-test") a.Nil(main.Commands[model.CLEAN].RunWith(c), "Failed to run clean-test")
a.False(utils.Exists(filepath.Join(gopath, "src", "clean-test", "app", "tmp", "main.go")), a.False(utils.Exists(filepath.Join(gopath, "clean-test", "app", "tmp", "main.go")),
"Did not remove main from path "+filepath.Join(gopath, "src", "clean-test", "app", "tmp", "main.go")) "Did not remove main from path "+filepath.Join(gopath, "clean-test", "app", "tmp", "main.go"))
}) })
if !t.Failed() { if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil { if err := os.RemoveAll(gopath); err != nil {

View File

@@ -1,21 +1,24 @@
package main_test package main_test
import ( import (
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"github.com/revel/cmd/logger" "github.com/revel/cmd/logger"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"go/build"
"os"
"path/filepath"
) )
// Test that the event handler can be attached and it dispatches the event received // Test that the event handler can be attached and it dispatches the event received.
func setup(suffix string, a *assert.Assertions) (string) { func setup(suffix string, a *assert.Assertions) string {
temp := os.TempDir() temp := os.TempDir()
wd, _ := os.Getwd() wd, _ := os.Getwd()
utils.InitLogger(wd, logger.LvlInfo) utils.InitLogger(wd, logger.LvlInfo)
gopath := filepath.Join(temp, "revel-test",suffix) gopath := filepath.Join(temp, "revel-test", suffix)
if utils.Exists(gopath) { if utils.Exists(gopath) {
utils.Logger.Info("Removing test path", "path", gopath) utils.Logger.Info("Removing test path", "path", gopath)
if err := os.RemoveAll(gopath); err != nil { if err := os.RemoveAll(gopath); err != nil {
@@ -30,23 +33,41 @@ func setup(suffix string, a *assert.Assertions) (string) {
// But if you change into that directory and read the current folder it is // But if you change into that directory and read the current folder it is
// /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build // /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build
// So to make this work on darwin this code was added // So to make this work on darwin this code was added
os.Chdir(gopath) if err := os.Chdir(gopath); err != nil {
panic(err)
}
newwd, _ := os.Getwd() newwd, _ := os.Getwd()
gopath = newwd gopath = newwd
defaultBuild := build.Default defaultBuild := build.Default
defaultBuild.GOPATH = gopath defaultBuild.GOPATH = gopath
build.Default = defaultBuild build.Default = defaultBuild
utils.Logger.Info("Setup stats", "original wd", wd, "new wd", newwd, "gopath",gopath, "gopath exists", utils.DirExists(gopath), "wd exists", utils.DirExists(newwd)) utils.Logger.Info("Setup stats", "original wd", wd, "new wd", newwd, "gopath", gopath, "gopath exists", utils.DirExists(gopath), "wd exists", utils.DirExists(newwd))
return gopath return gopath
} }
// Create a new app for the name // Create a new app for the name.
func newApp(name string, command model.COMMAND, precall func(c *model.CommandConfig), a *assert.Assertions) *model.CommandConfig { func newApp(name string, command model.COMMAND, precall func(c *model.CommandConfig), a *assert.Assertions) *model.CommandConfig {
c := &model.CommandConfig{} c := &model.CommandConfig{Vendored: true}
switch command { switch command {
case model.NEW: case model.NEW:
c.New.ImportPath = name c.New.ImportPath = name
c.New.Callback = func() error {
// On callback we will invoke a specific branch of revel so that it works
goModCmd := exec.Command("go", "mod", "tidy")
utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
getOutput, _ := goModCmd.CombinedOutput()
fmt.Printf("Calling go mod tidy %s", string(getOutput))
goModCmd = exec.Command("go", "mod", "edit", "-replace=github.com/revel/revel=github.com/revel/revel@develop")
utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
getOutput, _ = goModCmd.CombinedOutput()
fmt.Printf("Calling go mod edit %v", string(getOutput))
return nil
}
case model.BUILD: case model.BUILD:
c.Build.ImportPath = name c.Build.ImportPath = name
case model.TEST: case model.TEST:
@@ -65,10 +86,10 @@ func newApp(name string, command model.COMMAND, precall func(c *model.CommandCon
if precall != nil { if precall != nil {
precall(c) precall(c)
} }
if !c.UpdateImportPath() { if c.UpdateImportPath() != nil {
a.Fail("Unable to update import path") a.Fail("Unable to update import path")
} }
c.InitGoPaths()
c.InitPackageResolver() c.InitPackageResolver()
return c return c
} }

View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"go/build" "go/build"
"math/rand" "math/rand"
"net/url"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -15,11 +16,12 @@ import (
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"net/url"
) )
const ErrNoSkeleton Error = "failed to find skeleton in filepath"
var cmdNew = &Command{ var cmdNew = &Command{
UsageLine: "new -i [path] -s [skeleton]", UsageLine: "new -i [path] -s [skeleton] -p [package name]",
Short: "create a skeleton Revel application", Short: "create a skeleton Revel application",
Long: ` Long: `
New creates a few files to get a new Revel application running quickly. New creates a few files to get a new Revel application running quickly.
@@ -43,101 +45,69 @@ func init() {
cmdNew.UpdateConfig = updateNewConfig cmdNew.UpdateConfig = updateNewConfig
} }
// Called when unable to parse the command line automatically and assumes an old launch // Called when unable to parse the command line automatically and assumes an old launch.
func updateNewConfig(c *model.CommandConfig, args []string) bool { func updateNewConfig(c *model.CommandConfig, args []string) bool {
c.Index = model.NEW c.Index = model.NEW
if len(c.New.Package) > 0 {
c.New.NotVendored = false
}
c.Vendored = !c.New.NotVendored
if len(args) == 0 { if len(args) == 0 {
fmt.Fprintf(os.Stderr, cmdNew.Long) if len(c.New.ImportPath) == 0 {
return false fmt.Fprintf(os.Stderr, cmdNew.Long)
return false
}
return true
} }
c.New.ImportPath = args[0] c.New.ImportPath = args[0]
if len(args) > 1 { if len(args) > 1 {
c.New.SkeletonPath = args[1] c.New.SkeletonPath = args[1]
} }
return true
return true
} }
// Call to create a new application // Call to create a new application.
func newApp(c *model.CommandConfig) (err error) { func newApp(c *model.CommandConfig) (err error) {
// Check for an existing folder so we don't clobber it // Check for an existing folder so we don't clobber it
_, err = build.Import(c.ImportPath, "", build.FindOnly) _, err = build.Import(c.ImportPath, "", build.FindOnly)
if err == nil || !utils.Empty(c.AppPath) { if err == nil || !utils.Empty(c.AppPath) {
return utils.NewBuildError("Abort: Import path already exists.", "path", c.ImportPath) return utils.NewBuildError("Abort: Import path already exists.", "path", c.ImportPath, "apppath", c.AppPath)
} }
// checking and setting skeleton
if err = setSkeletonPath(c); err != nil {
return
}
// Create application path
if err := os.MkdirAll(c.AppPath, os.ModePerm); err != nil { if err := os.MkdirAll(c.AppPath, os.ModePerm); err != nil {
return utils.NewBuildError("Abort: Unable to create app path.", "path", c.AppPath) return utils.NewBuildError("Abort: Unable to create app path.", "path", c.AppPath)
} }
if c.New.Vendored {
utils.Logger.Info("Creating a new vendor app")
vendorPath := filepath.Join(c.AppPath, "vendor")
if !utils.DirExists(vendorPath) {
if err := os.MkdirAll(vendorPath, os.ModePerm); err != nil {
return utils.NewBuildError("Failed to create "+vendorPath, "error", err)
}
}
// In order for dep to run there needs to be a source file in the folder
tempPath := filepath.Join(c.AppPath, "tmp")
utils.Logger.Info("Checking for temp folder for source code", "path", tempPath)
if !utils.DirExists(tempPath) {
if err := os.MkdirAll(tempPath, os.ModePerm); err != nil {
return utils.NewBuildIfError(err, "Failed to create "+vendorPath)
}
if err = utils.GenerateTemplate(filepath.Join(tempPath, "main.go"), NEW_MAIN_FILE, nil); err != nil {
return utils.NewBuildIfError(err, "Failed to create main file "+vendorPath)
}
}
// Create a package template file if it does not exist
packageFile := filepath.Join(c.AppPath, "Gopkg.toml")
utils.Logger.Info("Checking for Gopkg.toml", "path", packageFile)
if !utils.Exists(packageFile) {
utils.Logger.Info("Generating Gopkg.toml", "path", packageFile)
if err := utils.GenerateTemplate(packageFile, VENDOR_GOPKG, nil); err != nil {
return utils.NewBuildIfError(err, "Failed to generate template")
}
} else {
utils.Logger.Info("Package file exists in skeleto, skipping adding")
}
getCmd := exec.Command("dep", "ensure", "-v")
utils.CmdInit(getCmd, c.AppPath)
utils.Logger.Info("Exec:", "args", getCmd.Args)
getOutput, err := getCmd.CombinedOutput()
if err != nil {
return utils.NewBuildIfError(err, string(getOutput))
}
}
// checking and setting application // checking and setting application
if err = setApplicationPath(c); err != nil { if err = setApplicationPath(c); err != nil {
return err return err
} }
// checking and setting skeleton // This kicked off the download of the revel app, not needed for vendor
if err=setSkeletonPath(c);err!=nil { if !c.Vendored {
return // At this point the versions can be set
if err = c.SetVersions(); err != nil {
return
}
} }
// copy files to new app directory // copy files to new app directory
if err = copyNewAppFiles(c);err != nil { if err = copyNewAppFiles(c); err != nil {
return return
} }
// Rerun the dep tool if vendored // Run the vendor tool if needed
if c.New.Vendored { if c.Vendored {
getCmd := exec.Command("dep", "ensure", "-v") if err = createModVendor(c); err != nil {
utils.CmdInit(getCmd, c.AppPath) return
utils.Logger.Info("Exec:", "args", getCmd.Args)
getOutput, err := getCmd.CombinedOutput()
if err != nil {
utils.Logger.Fatal(string(getOutput))
} }
} }
@@ -145,41 +115,72 @@ func newApp(c *model.CommandConfig) (err error) {
fmt.Fprintln(os.Stdout, "Your application has been created in:\n ", c.AppPath) fmt.Fprintln(os.Stdout, "Your application has been created in:\n ", c.AppPath)
// Check to see if it should be run right off // Check to see if it should be run right off
if c.New.Run { if c.New.Run {
runApp(c) // Need to prep the run command
c.Run.ImportPath = c.ImportPath
updateRunConfig(c, nil)
if err = c.UpdateImportPath(); err != nil {
return
}
if err = runApp(c); err != nil {
return
}
} else { } else {
fmt.Fprintln(os.Stdout, "\nYou can run it with:\n revel run -a ", c.ImportPath) fmt.Fprintln(os.Stdout, "\nYou can run it with:\n revel run -a", c.ImportPath)
} }
return return
} }
// Used to generate a new secret key func createModVendor(c *model.CommandConfig) (err error) {
utils.Logger.Info("Creating a new mod app")
goModCmd := exec.Command("go", "mod", "init", filepath.Join(c.New.Package, c.AppName))
utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec:", "args", goModCmd.Args, "env", goModCmd.Env, "workingdir", goModCmd.Dir)
getOutput, err := goModCmd.CombinedOutput()
if c.New.Callback != nil {
err = c.New.Callback()
}
if err != nil {
return utils.NewBuildIfError(err, string(getOutput))
}
return
}
// Used to generate a new secret key.
const alphaNumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" const alphaNumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
// Generate a secret key // Generate a secret key.
func generateSecret() string { func generateSecret() string {
chars := make([]byte, 64) chars := make([]byte, 64)
for i := 0; i < 64; i++ { for i := 0; i < 64; i++ {
chars[i] = alphaNumeric[rand.Intn(len(alphaNumeric))] chars[i] = alphaNumeric[rand.Intn(len(alphaNumeric))]
} }
return string(chars) return string(chars)
} }
// Sets the applicaiton path // Sets the application path.
func setApplicationPath(c *model.CommandConfig) (err error) { func setApplicationPath(c *model.CommandConfig) (err error) {
// revel/revel#1014 validate relative path, we cannot use built-in functions // revel/revel#1014 validate relative path, we cannot use built-in functions
// since Go import path is valid relative path too. // since Go import path is valid relative path too.
// so check basic part of the path, which is "." // so check basic part of the path, which is "."
if filepath.IsAbs(c.ImportPath) || strings.HasPrefix(c.ImportPath, ".") {
utils.Logger.Fatalf("Abort: '%s' looks like a directory. Please provide a Go import path instead.",
c.ImportPath)
}
// If we are running a vendored version of Revel we do not need to check for it. // If we are running a vendored version of Revel we do not need to check for it.
if !c.New.Vendored { if !c.Vendored {
if filepath.IsAbs(c.ImportPath) || strings.HasPrefix(c.ImportPath, ".") {
utils.Logger.Fatalf("Abort: '%s' looks like a directory. Please provide a Go import path instead.",
c.ImportPath)
}
_, err = build.Import(model.RevelImportPath, "", build.FindOnly) _, err = build.Import(model.RevelImportPath, "", build.FindOnly)
if err != nil { if err != nil {
//// Go get the revel project // Go get the revel project
err = c.PackageResolver(model.RevelImportPath) err = c.PackageResolver(model.RevelImportPath)
if err != nil { if err != nil {
return utils.NewBuildIfError(err, "Failed to fetch revel "+model.RevelImportPath) return utils.NewBuildIfError(err, "Failed to fetch revel "+model.RevelImportPath)
@@ -189,43 +190,48 @@ func setApplicationPath(c *model.CommandConfig) (err error) {
c.AppName = filepath.Base(c.AppPath) c.AppName = filepath.Base(c.AppPath)
//if c.BasePath == "." {
// // we need to remove the a single '.' when
// // the app is in the $GOROOT/src directory
// c.BasePath = ""
//} else {
// // we need to append a '/' when the app is
// // is a subdirectory such as $GOROOT/src/path/to/revelapp
// c.BasePath += "/"
//}
return nil return nil
} }
// Set the skeleton path // Set the skeleton path.
func setSkeletonPath(c *model.CommandConfig) (err error) { func setSkeletonPath(c *model.CommandConfig) (err error) {
if len(c.New.SkeletonPath) == 0 { if len(c.New.SkeletonPath) == 0 {
c.New.SkeletonPath = "git://" + RevelSkeletonsImportPath + ":basic/bootstrap4" c.New.SkeletonPath = "https://" + RevelSkeletonsImportPath + ":basic/bootstrap4"
} }
// First check to see the protocol of the string // First check to see the protocol of the string
if sp, err := url.Parse(c.New.SkeletonPath); err == nil { sp, err := url.Parse(c.New.SkeletonPath)
if err == nil {
utils.Logger.Info("Detected skeleton path", "path", sp) utils.Logger.Info("Detected skeleton path", "path", sp)
switch strings.ToLower(sp.Scheme) { switch strings.ToLower(sp.Scheme) {
// TODO Add support for https, http, ftp, file // TODO Add support for ftp, sftp, scp ??
case "":
sp.Scheme = "file"
fallthrough
case "file":
fullpath := sp.String()[7:]
if !filepath.IsAbs(fullpath) {
fullpath, err = filepath.Abs(fullpath)
if err != nil {
return
}
}
c.New.SkeletonPath = fullpath
utils.Logger.Info("Set skeleton path to ", fullpath)
if !utils.DirExists(fullpath) {
return fmt.Errorf("%w %s %s", ErrNoSkeleton, fullpath, sp.String())
}
case "git": case "git":
fallthrough
case "http":
fallthrough
case "https":
if err := newLoadFromGit(c, sp); err != nil { if err := newLoadFromGit(c, sp); err != nil {
return err return err
} }
//case "":
//if err := newLoadFromGo(c, sp); err != nil {
// return err
//}
default: default:
utils.Logger.Fatal("Unsupported skeleton schema ", "path", c.New.SkeletonPath) utils.Logger.Fatal("Unsupported skeleton schema ", "path", c.New.SkeletonPath)
} }
// TODO check to see if the path needs to be extracted // TODO check to see if the path needs to be extracted
} else { } else {
@@ -234,7 +240,7 @@ func setSkeletonPath(c *model.CommandConfig) (err error) {
return return
} }
// Load skeleton from git // Load skeleton from git.
func newLoadFromGit(c *model.CommandConfig, sp *url.URL) (err error) { func newLoadFromGit(c *model.CommandConfig, sp *url.URL) (err error) {
// This method indicates we need to fetch from a repository using git // This method indicates we need to fetch from a repository using git
// Execute "git clone get <pkg>" // Execute "git clone get <pkg>"
@@ -245,7 +251,7 @@ func newLoadFromGit(c *model.CommandConfig, sp *url.URL) (err error) {
utils.Logger.Info("Exec:", "args", getCmd.Args) utils.Logger.Info("Exec:", "args", getCmd.Args)
getOutput, err := getCmd.CombinedOutput() getOutput, err := getCmd.CombinedOutput()
if err != nil { if err != nil {
utils.Logger.Fatalf("Abort: could not clone the Skeleton source code: \n%s\n%s\n", getOutput, c.New.SkeletonPath) utils.Logger.Fatal("Abort: could not clone the Skeleton source code: ", "output", string(getOutput), "path", c.New.SkeletonPath)
} }
outputPath := targetPath outputPath := targetPath
if len(pathpart) > 1 { if len(pathpart) > 1 {
@@ -280,68 +286,4 @@ func copyNewAppFiles(c *model.CommandConfig) (err error) {
// Dotfiles are skipped by mustCopyDir, so we have to explicitly copy the .gitignore. // Dotfiles are skipped by mustCopyDir, so we have to explicitly copy the .gitignore.
gitignore := ".gitignore" gitignore := ".gitignore"
return utils.CopyFile(filepath.Join(c.AppPath, gitignore), filepath.Join(c.New.SkeletonPath, gitignore)) return utils.CopyFile(filepath.Join(c.AppPath, gitignore), filepath.Join(c.New.SkeletonPath, gitignore))
} }
const (
VENDOR_GOPKG = `#
# Revel Gopkg.toml
#
# If you want to use a specific version of Revel change the branches below
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
required = ["github.com/revel/revel", "github.com/revel/modules"]
# Note to use a specific version changes this to
#
# [[override]]
# version = "0.20.1"
# name = "github.com/revel/modules"
[[override]]
branch = "master"
name = "github.com/revel/modules"
# Note to use a specific version changes this to
#
# [[override]]
# version = "0.20.0"
# name = "github.com/revel/revel"
[[override]]
branch = "master"
name = "github.com/revel/revel"
[[override]]
branch = "master"
name = "github.com/revel/log15"
[[override]]
branch = "master"
name = "github.com/revel/cron"
[[override]]
branch = "master"
name = "github.com/xeonx/timeago"
`
NEW_MAIN_FILE = `package main
`
)

View File

@@ -1,97 +1,51 @@
package main_test package main_test
import ( import (
"github.com/revel/cmd/model"
"github.com/revel/cmd/revel"
"github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/revel/cmd/model"
main "github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
) )
// test the commands // test the commands.
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-new", a) gopath := setup("revel-test-new", a)
t.Run("New", func(t *testing.T) {
a := assert.New(t)
c := newApp("new-test", model.NEW, nil, a)
a.Nil(main.Commands[model.NEW].RunWith(c), "New failed")
})
t.Run("Path", func(t *testing.T) {
a := assert.New(t)
c := newApp("new/test/a", model.NEW, nil, a)
a.Nil(main.Commands[model.NEW].RunWith(c), "New path failed")
})
t.Run("Path-Duplicate", func(t *testing.T) {
a := assert.New(t)
c := newApp("new/test/b", model.NEW, nil, a)
a.Nil(main.Commands[model.NEW].RunWith(c), "New path failed")
c = newApp("new/test/b", model.NEW, nil, a)
a.NotNil(main.Commands[model.NEW].RunWith(c), "Duplicate path Did Not failed")
})
t.Run("Skeleton-Git", func(t *testing.T) {
a := assert.New(t)
c := newApp("new/test/c/1", model.NEW, nil, a)
c.New.SkeletonPath = "git://github.com/revel/skeletons:basicnsadnsak"
a.NotNil(main.Commands[model.NEW].RunWith(c), "Expected Failed to run with new")
// We need to pick a different path
c = newApp("new/test/c/2", model.NEW, nil, a)
c.New.SkeletonPath = "git://github.com/revel/skeletons:basic/bootstrap4"
a.Nil(main.Commands[model.NEW].RunWith(c), "Failed to run with new skeleton git")
})
if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil {
a.Fail("Failed to remove test path")
}
}
}
// test the commands
func TestNewVendor(t *testing.T) {
a := assert.New(t)
gopath := setup("revel-test-new-vendor", a)
precall := func(c *model.CommandConfig) {
c.New.Vendored = true
}
t.Run("New", func(t *testing.T) { t.Run("New", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("onlyone/v/a", model.NEW, precall, a) c := newApp("new-test", model.NEW, nil, a)
c.New.Vendored = true
a.Nil(main.Commands[model.NEW].RunWith(c), "New failed") a.Nil(main.Commands[model.NEW].RunWith(c), "New failed")
}) })
t.Run("Test", func(t *testing.T) { t.Run("New-NotVendoredmode", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("onlyone/v/a", model.TEST, nil, a) c := newApp("new-notvendored", model.NEW, nil, a)
a.Nil(main.Commands[model.TEST].RunWith(c), "Test failed") c.New.NotVendored = true
a.Nil(main.Commands[model.NEW].RunWith(c), "New failed")
}) })
t.Run("Build", func(t *testing.T) { t.Run("Path", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("onlyone/v/a", model.BUILD, nil, a) c := newApp("new/test/a", model.NEW, nil, a)
c.Index = model.BUILD a.Nil(main.Commands[model.NEW].RunWith(c), "New path failed")
c.Build.TargetPath = filepath.Join(gopath, "src/onlyone/v/a", "target")
a.Nil(main.Commands[model.BUILD].RunWith(c), " Build failed")
a.True(utils.DirExists(c.Build.TargetPath), "Target folder not made", c.Build.TargetPath)
}) })
t.Run("Package", func(t *testing.T) { t.Run("Path-Duplicate", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("onlyone/v/a", model.PACKAGE, nil, a) c := newApp("new/test/b", model.NEW, nil, a)
c.Package.TargetPath = filepath.Join(gopath, "src/onlyone/v/a", "target.tar.gz") a.Nil(main.Commands[model.NEW].RunWith(c), "New path failed")
a.Nil(main.Commands[model.PACKAGE].RunWith(c), "Package Failed") c = newApp("new/test/b", model.NEW, nil, a)
a.True(utils.Exists(c.Package.TargetPath), "Target package not made", c.Package.TargetPath) a.NotNil(main.Commands[model.NEW].RunWith(c), "Duplicate path Did Not failed")
}) })
t.Run("TestVendorDir", func(t *testing.T) { t.Run("Skeleton-Git", func(t *testing.T) {
// Check to see that no additional packages were downloaded outside the vendor folder a := assert.New(t)
files, err := ioutil.ReadDir(gopath) c := newApp("new/test/c/1", model.NEW, nil, a)
a.Nil(err, "Failed to read gopath folder") c.New.SkeletonPath = "git://github.com/revel/skeletons:basicnsadnsak"
// bin/ onlyone/ pkg/ src/ a.NotNil(main.Commands[model.NEW].RunWith(c), "Expected Failed to run with new")
a.Equal(3, len(files), "Expected single file in "+gopath) // We need to pick a different path
files, err = ioutil.ReadDir(filepath.Join(gopath, "src")) c = newApp("new/test/c/2", model.NEW, nil, a)
a.Nil(err, "Failed to read src folder") c.New.SkeletonPath = "git://github.com/revel/skeletons:basic/bootstrap4"
a.Equal(1, len(files), "Expected single file in source folder", filepath.Join(gopath, "src")) a.Nil(main.Commands[model.NEW].RunWith(c), "Failed to run with new skeleton git")
}) })
if !t.Failed() { if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil { if err := os.RemoveAll(gopath); err != nil {

View File

@@ -37,34 +37,32 @@ func init() {
cmdPackage.UpdateConfig = updatePackageConfig cmdPackage.UpdateConfig = updatePackageConfig
} }
// Called when unable to parse the command line automatically and assumes an old launch // Called when unable to parse the command line automatically and assumes an old launch.
func updatePackageConfig(c *model.CommandConfig, args []string) bool { func updatePackageConfig(c *model.CommandConfig, args []string) bool {
c.Index = model.PACKAGE c.Index = model.PACKAGE
if len(args) == 0 && c.Package.ImportPath != "" {
return true
}
c.Package.ImportPath = args[0] c.Package.ImportPath = args[0]
if len(args) > 1 { if len(args) > 1 {
c.Package.Mode = args[1] c.Package.Mode = args[1]
} }
return true return true
} }
// Called to package the app // Called to package the app.
func packageApp(c *model.CommandConfig) (err error) { func packageApp(c *model.CommandConfig) (err error) {
// Determine the run mode. // Determine the run mode.
mode := DefaultRunMode mode := c.Package.Mode
if len(c.Package.Mode) >= 0 {
mode = c.Package.Mode
}
appImportPath := c.ImportPath appImportPath := c.ImportPath
revel_paths, err := model.NewRevelPaths(mode, appImportPath, "", model.NewWrappedRevelCallback(nil, c.PackageResolver)) revelPaths, err := model.NewRevelPaths(mode, appImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
if err != nil { if err != nil {
return return
} }
// Remove the archive if it already exists. // Remove the archive if it already exists.
destFile := filepath.Join(c.AppPath, filepath.Base(revel_paths.BasePath)+".tar.gz") destFile := filepath.Join(c.AppPath, filepath.Base(revelPaths.BasePath)+".tar.gz")
if c.Package.TargetPath != "" { if c.Package.TargetPath != "" {
if filepath.IsAbs(c.Package.TargetPath) { if filepath.IsAbs(c.Package.TargetPath) {
destFile = c.Package.TargetPath destFile = c.Package.TargetPath
@@ -77,16 +75,16 @@ func packageApp(c *model.CommandConfig) (err error) {
} }
// Collect stuff in a temp directory. // Collect stuff in a temp directory.
tmpDir, err := ioutil.TempDir("", filepath.Base(revel_paths.BasePath)) tmpDir, err := ioutil.TempDir("", filepath.Base(revelPaths.BasePath))
utils.PanicOnError(err, "Failed to get temp dir") utils.PanicOnError(err, "Failed to get temp dir")
// Build expects the command the build to contain the proper data // Build expects the command the build to contain the proper data
if len(c.Package.Mode) >= 0 { c.Build.Mode = c.Package.Mode
c.Build.Mode = c.Package.Mode
}
c.Build.TargetPath = tmpDir c.Build.TargetPath = tmpDir
c.Build.CopySource = c.Package.CopySource c.Build.CopySource = c.Package.CopySource
buildApp(c) if err = buildApp(c); err != nil {
return
}
// Create the zip file. // Create the zip file.

View File

@@ -1,22 +1,23 @@
package main_test package main_test
import ( import (
"github.com/revel/cmd/model"
"github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
"os" "os"
"testing" "testing"
"github.com/revel/cmd/model"
main "github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
) )
// test the commands // test the commands.
func TestPackage(t *testing.T) { func TestPackage(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-package", a) gopath := setup("revel-test-package", a)
t.Run("Package", func(t *testing.T) { t.Run("Package", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
c := newApp("package-test", model.NEW, nil, a) c := newApp("package-test", model.NEW, nil, a)
main.Commands[model.NEW].RunWith(c) a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.PACKAGE c.Index = model.PACKAGE
c.Package.ImportPath = c.ImportPath c.Package.ImportPath = c.ImportPath
a.Nil(main.Commands[model.PACKAGE].RunWith(c), "Failed to run package-test") a.Nil(main.Commands[model.PACKAGE].RunWith(c), "Failed to run package-test")

View File

@@ -6,6 +6,7 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"math/rand" "math/rand"
@@ -14,22 +15,28 @@ import (
"strings" "strings"
"time" "time"
"github.com/jessevdk/go-flags"
"github.com/agtorre/gocolorize" "github.com/agtorre/gocolorize"
"github.com/jessevdk/go-flags"
"github.com/revel/cmd/logger" "github.com/revel/cmd/logger"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
) )
const ( // Error is used for constant errors.
// RevelCmdImportPath Revel framework cmd tool import path type Error string
RevelCmdImportPath = "github.com/revel/cmd"
// RevelCmdImportPath Revel framework cmd tool import path // Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
const ErrInvalidCommandLine Error = "invalid command line arguments"
const (
// RevelCmdImportPath Revel framework cmd tool import path.
RevelSkeletonsImportPath = "github.com/revel/skeletons" RevelSkeletonsImportPath = "github.com/revel/skeletons"
// DefaultRunMode for revel's application // DefaultRunMode for revel's application.
DefaultRunMode = "dev" DefaultRunMode = "dev"
) )
@@ -40,7 +47,7 @@ type Command struct {
UsageLine, Short, Long string UsageLine, Short, Long string
} }
// Name returns command name from usage line // Name returns command name from usage line.
func (cmd *Command) Name() string { func (cmd *Command) Name() string {
name := cmd.UsageLine name := cmd.UsageLine
i := strings.Index(name, " ") i := strings.Index(name, " ")
@@ -50,7 +57,7 @@ func (cmd *Command) Name() string {
return name return name
} }
// The commands // Commands defines the available commands.
var Commands = []*Command{ var Commands = []*Command{
nil, // Safety net, prevent missing index from running nil, // Safety net, prevent missing index from running
cmdNew, cmdNew,
@@ -70,43 +77,45 @@ func main() {
wd, _ := os.Getwd() wd, _ := os.Getwd()
utils.InitLogger(wd, logger.LvlError) utils.InitLogger(wd, logger.LvlError)
parser := flags.NewParser(c, flags.HelpFlag|flags.PassDoubleDash) parser := flags.NewParser(c, flags.HelpFlag|flags.PassDoubleDash)
if err := ParseArgs(c, parser, os.Args[1:]); err != nil { if len(os.Args) < 2 {
fmt.Fprint(os.Stderr, err.Error())
parser.WriteHelp(os.Stdout) parser.WriteHelp(os.Stdout)
os.Exit(1) os.Exit(1)
} }
if err := ParseArgs(c, parser, os.Args[1:]); err != nil {
fmt.Fprint(os.Stderr, err.Error()+"\n")
os.Exit(1)
}
// Switch based on the verbose flag // Switch based on the verbose flag
if len(c.Verbose)>1 { if len(c.Verbose) > 1 {
utils.InitLogger(wd, logger.LvlDebug) utils.InitLogger(wd, logger.LvlDebug)
} else if len(c.Verbose)>0 { } else if len(c.Verbose) > 0 {
utils.InitLogger(wd, logger.LvlInfo) utils.InitLogger(wd, logger.LvlInfo)
} else { } else {
utils.InitLogger(wd, logger.LvlWarn) utils.InitLogger(wd, logger.LvlWarn)
} }
if !c.UpdateImportPath() { // Setup package resolver
utils.Logger.Fatal("Unable to determine application path") c.InitPackageResolver()
if err := c.UpdateImportPath(); err != nil {
utils.Logger.Error(err.Error())
parser.WriteHelp(os.Stdout)
os.Exit(1)
} }
command := Commands[c.Index] command := Commands[c.Index]
println("Revel executing:", command.Short) println("Revel executing:", command.Short)
// Setting go paths
c.InitGoPaths()
// Setup package resolver
c.InitPackageResolver()
if err := command.RunWith(c); err != nil { if err := command.RunWith(c); err != nil {
utils.Logger.Error("Unable to execute","error",err) utils.Logger.Error("Unable to execute", "error", err)
os.Exit(1) os.Exit(1)
} }
} }
// Parse the arguments passed into the model.CommandConfig // Parse the arguments passed into the model.CommandConfig.
func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err error) { func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err error) {
var extraArgs []string var extraArgs []string
if ini := flag.String("ini", "none", ""); *ini != "none" { if ini := flag.String("ini", "none", ""); *ini != "none" {
@@ -116,33 +125,30 @@ func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err
} else { } else {
if extraArgs, err = parser.ParseArgs(args); err != nil { if extraArgs, err = parser.ParseArgs(args); err != nil {
return return
} else { }
switch parser.Active.Name {
case "new": switch parser.Active.Name {
c.Index = model.NEW case "new":
case "run": c.Index = model.NEW
c.Index = model.RUN case "run":
case "build": c.Index = model.RUN
c.Index = model.BUILD case "build":
case "package": c.Index = model.BUILD
c.Index = model.PACKAGE case "package":
case "clean": c.Index = model.PACKAGE
c.Index = model.CLEAN case "clean":
case "test": c.Index = model.CLEAN
c.Index = model.TEST case "test":
case "version": c.Index = model.TEST
c.Index = model.VERSION case "version":
} c.Index = model.VERSION
} }
} }
if c.Index == 0 { if !Commands[c.Index].UpdateConfig(c, extraArgs) {
err = fmt.Errorf("Unknown command %v", extraArgs) buffer := &bytes.Buffer{}
} else if len(extraArgs) > 0 { parser.WriteHelp(buffer)
utils.Logger.Info("Found additional arguements, setting them") err = fmt.Errorf("%w %v\n%s", ErrInvalidCommandLine, extraArgs, buffer.String())
if !Commands[c.Index].UpdateConfig(c, extraArgs) {
err = fmt.Errorf("Invalid command line arguements %v", extraArgs)
}
} }
return return

View File

@@ -5,13 +5,14 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"os"
"strconv" "strconv"
"fmt"
"github.com/revel/cmd/harness" "github.com/revel/cmd/harness"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"go/build"
) )
var cmdRun = &Command{ var cmdRun = &Command{
@@ -34,13 +35,6 @@ You can set a port as well. For example:
revel run -m prod -p 8080 github.com/revel/examples/chat `, 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() { func init() {
cmdRun.RunWith = runApp cmdRun.RunWith = runApp
cmdRun.UpdateConfig = updateRunConfig cmdRun.UpdateConfig = updateRunConfig
@@ -71,7 +65,7 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool {
// 3. revel run [run-mode] [port] // 3. revel run [run-mode] [port]
// Check to see if the import path evaluates out to something that may be on a gopath // Check to see if the import path evaluates out to something that may be on a gopath
if _, err := build.Import(args[0], "", build.FindOnly); err == nil { if runIsImportPath(args[0]) {
// 1st arg is the import path // 1st arg is the import path
c.Run.ImportPath = args[0] c.Run.ImportPath = args[0]
@@ -92,12 +86,7 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool {
// 1. revel run [import-path] // 1. revel run [import-path]
// 2. revel run [port] // 2. revel run [port]
// 3. revel run [run-mode] // 3. revel run [run-mode]
_, err := build.Import(args[0], "", build.FindOnly) if runIsImportPath(args[0]) {
if err != nil {
utils.Logger.Warn("Unable to run using an import path, assuming import path is working directory %s %s", "Argument", args[0], "error", err.Error())
}
utils.Logger.Info("Trying to build with", args[0], err)
if err == nil {
// 1st arg is the import path // 1st arg is the import path
c.Run.ImportPath = args[0] c.Run.ImportPath = args[0]
} else if _, err := strconv.Atoi(args[0]); err == nil { } else if _, err := strconv.Atoi(args[0]); err == nil {
@@ -108,53 +97,66 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool {
c.Run.Mode = args[0] c.Run.Mode = args[0]
} }
case 0: case 0:
return false // Attempt to set the import path to the current working director.
if c.Run.ImportPath == "" {
c.Run.ImportPath, _ = os.Getwd()
}
} }
c.Index = model.RUN c.Index = model.RUN
return true return true
} }
// Called to run the app // 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) { func runApp(c *model.CommandConfig) (err error) {
if c.Run.Mode == "" { if c.Run.Mode == "" {
c.Run.Mode = "dev" c.Run.Mode = "dev"
} }
revel_path, err := model.NewRevelPaths(c.Run.Mode, c.ImportPath, "", model.NewWrappedRevelCallback(nil, c.PackageResolver)) revelPath, err := model.NewRevelPaths(c.Run.Mode, c.ImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
if err != nil { if err != nil {
return utils.NewBuildIfError(err, "Revel paths") return utils.NewBuildIfError(err, "Revel paths")
} }
if c.Run.Port > -1 { if c.Run.Port > -1 {
revel_path.HTTPPort = c.Run.Port revelPath.HTTPPort = c.Run.Port
} else { } else {
c.Run.Port = revel_path.HTTPPort c.Run.Port = revelPath.HTTPPort
} }
utils.Logger.Infof("Running %s (%s) in %s mode\n", revel_path.AppName, revel_path.ImportPath, revel_path.RunMode) utils.Logger.Infof("Running %s (%s) in %s mode\n", revelPath.AppName, revelPath.ImportPath, revelPath.RunMode)
utils.Logger.Debug("Base path:", "path", revel_path.BasePath) utils.Logger.Debug("Base path:", "path", revelPath.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_path.Config.BoolDefault("watch", true) && revel_path.Config.BoolDefault("watch.code", true) { if revelPath.Config.BoolDefault("watch", true) && revelPath.Config.BoolDefault("watch.code", true) {
utils.Logger.Info("Running in watched mode.") utils.Logger.Info("Running in watched mode.")
runMode := fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v}`, revel_path.RunMode, c.Verbose)
runMode := fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v}`, revelPath.RunMode, c.GetVerbose())
if c.HistoricMode { if c.HistoricMode {
runMode = revel_path.RunMode runMode = revelPath.RunMode
} }
// **** Never returns. // **** Never returns.
harness.NewHarness(c, revel_path, runMode, c.Run.NoProxy).Run() harness.NewHarness(c, revelPath, runMode, c.Run.NoProxy).Run()
} }
// Else, just build and run the app. // Else, just build and run the app.
utils.Logger.Debug("Running in live build mode.") utils.Logger.Debug("Running in live build mode.")
app, err := harness.Build(c, revel_path) app, err := harness.Build(c, revelPath)
if err != nil { if err != nil {
utils.Logger.Errorf("Failed to build app: %s", err) utils.Logger.Errorf("Failed to build app: %s", err)
} }
app.Port = revel_path.HTTPPort app.Port = revelPath.HTTPPort
runMode := fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v}`, app.Paths.RunMode, c.Verbose) var paths []byte
if c.HistoricMode { if len(app.PackagePathMap) > 0 {
runMode = revel_path.RunMode paths, _ = json.Marshal(app.PackagePathMap)
} }
app.Cmd(runMode).Run() runMode := fmt.Sprintf(`{"mode":"%s", "specialUseFlag":%v,"packagePathMap":%s}`, app.Paths.RunMode, c.GetVerbose(), string(paths))
if c.HistoricMode {
runMode = revelPath.RunMode
}
app.Cmd(runMode).Run(c)
return return
} }

View File

@@ -1,15 +1,16 @@
package main_test package main_test
import ( import (
"github.com/stretchr/testify/assert"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
// test the commands // test the commands.
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-run", a) gopath := setup("revel-test-run", a)
// TODO Testing run // TODO Testing run

View File

@@ -52,9 +52,13 @@ func init() {
cmdTest.UpdateConfig = updateTestConfig cmdTest.UpdateConfig = updateTestConfig
} }
// Called to update the config command with from the older stype // Called to update the config command with from the older stype.
func updateTestConfig(c *model.CommandConfig, args []string) bool { func updateTestConfig(c *model.CommandConfig, args []string) bool {
c.Index = model.TEST c.Index = model.TEST
if len(args) == 0 && c.Test.ImportPath != "" {
return true
}
// The full test runs // The full test runs
// revel test <import path> (run mode) (suite(.function)) // revel test <import path> (run mode) (suite(.function))
if len(args) < 1 { if len(args) < 1 {
@@ -70,7 +74,7 @@ func updateTestConfig(c *model.CommandConfig, args []string) bool {
return true return true
} }
// Called to test the application // Called to test the application.
func testApp(c *model.CommandConfig) (err error) { func testApp(c *model.CommandConfig) (err error) {
mode := DefaultRunMode mode := DefaultRunMode
if c.Test.Mode != "" { if c.Test.Mode != "" {
@@ -78,7 +82,7 @@ func testApp(c *model.CommandConfig) (err error) {
} }
// Find and parse app.conf // Find and parse app.conf
revel_path, err := model.NewRevelPaths(mode, c.ImportPath, "", model.NewWrappedRevelCallback(nil, c.PackageResolver)) revelPath, err := model.NewRevelPaths(mode, c.ImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
if err != nil { if err != nil {
return return
} }
@@ -86,7 +90,7 @@ func testApp(c *model.CommandConfig) (err error) {
// todo Ensure that the testrunner is loaded in this mode. // todo Ensure that the testrunner is loaded in this mode.
// Create a directory to hold the test result files. // Create a directory to hold the test result files.
resultPath := filepath.Join(revel_path.BasePath, "test-results") resultPath := filepath.Join(revelPath.BasePath, "test-results")
if err = os.RemoveAll(resultPath); err != nil { if err = os.RemoveAll(resultPath); err != nil {
return utils.NewBuildError("Failed to remove test result directory ", "path", resultPath, "error", err) return utils.NewBuildError("Failed to remove test result directory ", "path", resultPath, "error", err)
} }
@@ -100,15 +104,20 @@ func testApp(c *model.CommandConfig) (err error) {
return utils.NewBuildError("Failed to create test result log file: ", "error", err) return utils.NewBuildError("Failed to create test result log file: ", "error", err)
} }
app, reverr := harness.Build(c, revel_path) app, reverr := harness.Build(c, revelPath)
if reverr != nil { if reverr != nil {
return utils.NewBuildIfError(reverr, "Error building: ") return utils.NewBuildIfError(reverr, "Error building: ")
} }
runMode := fmt.Sprintf(`{"mode":"%s","testModeFlag":true, "specialUseFlag":%v}`, app.Paths.RunMode, c.Verbose) 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.GetVerbose(), string(paths))
if c.HistoricMode { if c.HistoricMode {
runMode = app.Paths.RunMode runMode = app.Paths.RunMode
} }
cmd := app.Cmd(runMode) cmd := app.Cmd(runMode)
cmd.Dir = c.AppPath
cmd.Stderr = io.MultiWriter(cmd.Stderr, file) cmd.Stderr = io.MultiWriter(cmd.Stderr, file)
cmd.Stdout = io.MultiWriter(cmd.Stderr, file) cmd.Stdout = io.MultiWriter(cmd.Stderr, file)
@@ -119,20 +128,20 @@ func testApp(c *model.CommandConfig) (err error) {
} }
defer cmd.Kill() defer cmd.Kill()
var httpAddr = revel_path.HTTPAddr httpAddr := revelPath.HTTPAddr
if httpAddr == "" { if httpAddr == "" {
httpAddr = "localhost" httpAddr = "localhost"
} }
var httpProto = "http" httpProto := "http"
if revel_path.HTTPSsl { if revelPath.HTTPSsl {
httpProto = "https" httpProto = "https"
} }
// Get a list of tests // Get a list of tests
var baseURL = fmt.Sprintf("%s://%s:%d", httpProto, httpAddr, revel_path.HTTPPort) baseURL := fmt.Sprintf("%s://%s:%d", httpProto, httpAddr, revelPath.HTTPPort)
utils.Logger.Infof("Testing %s (%s) in %s mode URL %s \n", revel_path.AppName, revel_path.ImportPath, mode, baseURL) utils.Logger.Infof("Testing %s (%s) in %s mode URL %s \n", revelPath.AppName, revelPath.ImportPath, mode, baseURL)
testSuites, _ := getTestsList(baseURL) testSuites, _ := getTestsList(baseURL)
// If a specific TestSuite[.Method] is specified, only run that suite/test // If a specific TestSuite[.Method] is specified, only run that suite/test
@@ -145,7 +154,7 @@ func testApp(c *model.CommandConfig) (err error) {
fmt.Println() fmt.Println()
// Run each suite. // Run each suite.
failedResults, overallSuccess := runTestSuites(revel_path, baseURL, resultPath, testSuites) failedResults, overallSuccess := runTestSuites(revelPath, baseURL, resultPath, testSuites)
fmt.Println() fmt.Println()
if overallSuccess { if overallSuccess {
@@ -168,14 +177,14 @@ func testApp(c *model.CommandConfig) (err error) {
return return
} }
// Outputs the results to a file // Outputs the results to a file.
func writeResultFile(resultPath, name, content string) { func writeResultFile(resultPath, name, content string) {
if err := ioutil.WriteFile(filepath.Join(resultPath, name), []byte(content), 0666); err != nil { if err := ioutil.WriteFile(filepath.Join(resultPath, name), []byte(content), 0666); err != nil {
utils.Logger.Errorf("Failed to write result file %s: %s", filepath.Join(resultPath, name), err) utils.Logger.Errorf("Failed to write result file %s: %s", filepath.Join(resultPath, name), err)
} }
} }
// Determines if response should be plural // Determines if response should be plural.
func pluralize(num int, singular, plural string) string { func pluralize(num int, singular, plural string) string {
if num == 1 { if num == 1 {
return singular return singular
@@ -184,7 +193,7 @@ func pluralize(num int, singular, plural string) string {
} }
// Filters test suites and individual tests to match // Filters test suites and individual tests to match
// the parsed command line parameter // the parsed command line parameter.
func filterTestSuites(suites *[]tests.TestSuiteDesc, suiteArgument string) *[]tests.TestSuiteDesc { func filterTestSuites(suites *[]tests.TestSuiteDesc, suiteArgument string) *[]tests.TestSuiteDesc {
var suiteName, testName string var suiteName, testName string
argArray := strings.Split(suiteArgument, ".") argArray := strings.Split(suiteArgument, ".")
@@ -254,11 +263,10 @@ func getTestsList(baseURL string) (*[]tests.TestSuiteDesc, error) {
return &testSuites, err return &testSuites, err
} }
// Run the testsuites using the container // Run the testsuites using the container.
func runTestSuites(paths *model.RevelContainer, baseURL, resultPath string, testSuites *[]tests.TestSuiteDesc) (*[]tests.TestSuiteResult, bool) { func runTestSuites(paths *model.RevelContainer, baseURL, resultPath string, testSuites *[]tests.TestSuiteDesc) (*[]tests.TestSuiteResult, bool) {
// We can determine the testsuite location by finding the test module and extracting the data from it // We can determine the testsuite location by finding the test module and extracting the data from it
resultFilePath := filepath.Join(paths.ModulePathMap["testrunner"], "app", "views", "TestRunner/SuiteResult.html") resultFilePath := filepath.Join(paths.ModulePathMap["testrunner"].Path, "app", "views", "TestRunner/SuiteResult.html")
var ( var (
overallSuccess = true overallSuccess = true

View File

@@ -1,18 +1,18 @@
package main_test package main_test
import ( import (
"github.com/revel/cmd/model"
"github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
"os" "os"
"testing" "testing"
"github.com/revel/cmd/model"
main "github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
) )
// test the commands.
// test the commands
func TestRevelTest(t *testing.T) { func TestRevelTest(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-test", a) gopath := setup("revel-test-test", a)
t.Run("Test", func(t *testing.T) { t.Run("Test", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)

View File

@@ -9,20 +9,31 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"runtime" "go/ast"
"go/parser"
"go/token"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/revel/cmd" "github.com/revel/cmd"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"go/ast" )
"go/build"
"go/parser" type (
"go/token" // The version container.
"io/ioutil" VersionCommand struct {
"os" Command *model.CommandConfig // The command
"os/exec" revelVersion *model.Version // The Revel framework version
"path/filepath" modulesVersion *model.Version // The Revel modules version
cmdVersion *model.Version // The tool version
}
) )
var cmdVersion = &Command{ var cmdVersion = &Command{
@@ -38,83 +49,189 @@ For example:
} }
func init() { func init() {
cmdVersion.RunWith = versionApp v := &VersionCommand{}
cmdVersion.UpdateConfig = updateVersionConfig cmdVersion.UpdateConfig = v.UpdateConfig
cmdVersion.RunWith = v.RunWith
} }
// Update the version // Update the version.
func updateVersionConfig(c *model.CommandConfig, args []string) bool { func (v *VersionCommand) UpdateConfig(c *model.CommandConfig, args []string) bool {
if len(args) > 0 { if len(args) > 0 {
c.Version.ImportPath = args[0] c.Version.ImportPath = args[0]
} }
return true return true
} }
// Displays the version of go and Revel // Displays the version of go and Revel.
func versionApp(c *model.CommandConfig) (err error) { func (v *VersionCommand) RunWith(c *model.CommandConfig) (err error) {
utils.Logger.Info("Requesting version information", "config", c)
v.Command = c
var revelPath, appPath string // Update the versions with the local values
v.updateLocalVersions()
needsUpdates := true
appPath, revelPath, err = utils.FindSrcPaths(c.ImportPath, model.RevelImportPath, c.PackageResolver) versionInfo := ""
if err != nil { for x := 0; x < 2 && needsUpdates; x++ {
return utils.NewBuildError("Failed to import "+c.ImportPath+" with error:", "error", err) versionInfo, needsUpdates = v.doRepoCheck(x == 0)
} }
revelPath = revelPath + model.RevelImportPath
fmt.Println("\nRevel Framework",revelPath, appPath ) fmt.Printf("%s\n\nGo Location:%s\n\n", versionInfo, c.GoCmd)
if err != nil { cmd := exec.Command(c.GoCmd, "version")
utils.Logger.Info("Failed to find Revel in GOPATH with error:", "error", err, "gopath", build.Default.GOPATH) cmd.Stdout = os.Stdout
fmt.Println("Information not available (not on GOPATH)") if e := cmd.Start(); e != nil {
fmt.Println("Go command error ", e)
} else { } else {
utils.Logger.Info("Fullpath to revel", "dir", revelPath) if err = cmd.Wait(); err != nil {
fset := token.NewFileSet() // positions are relative to fset return
version, err := ioutil.ReadFile(filepath.Join(revelPath, "version.go"))
if err != nil {
utils.Logger.Error("Failed to find Revel version:", "error", err, "path", revelPath)
} }
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, "", version, parser.ParseComments)
if err != nil {
return utils.NewBuildError("Failed to parse Revel version error:", "error", err)
}
// Print the imports from the file's AST.
for _, s := range f.Decls {
genDecl, ok := s.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok != token.CONST {
continue
}
for _, a := range genDecl.Specs {
spec := a.(*ast.ValueSpec)
r := spec.Values[0].(*ast.BasicLit)
fmt.Printf("Revel %s = %s\n", spec.Names[0].Name, r.Value)
}
}
}
fmt.Println("\nRevel Command Utility Tool")
fmt.Println("Version", cmd.Version)
fmt.Println("Build Date", cmd.BuildDate)
fmt.Println("Minimum Go Version", cmd.MinimumGoVersion)
fmt.Printf("Compiled By %s %s/%s\n\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
// Extract the goversion detected
if len(c.GoCmd) > 0 {
cmd := exec.Command(c.GoCmd, "version")
cmd.Stdout = os.Stdout
if e := cmd.Start(); e != nil {
fmt.Println("Go command error ", e)
} else {
cmd.Wait()
}
} else {
fmt.Println("Go command not found ")
} }
return return
} }
// Checks the Revel repos for the latest version.
func (v *VersionCommand) doRepoCheck(updateLibs bool) (versionInfo string, needsUpdate bool) {
var (
title string
localVersion *model.Version
)
for _, repo := range []string{"revel", "cmd", "modules"} {
versonFromRepo, err := v.versionFromRepo(repo, "", "version.go")
if err != nil {
utils.Logger.Info("Failed to get version from repo", "repo", repo, "error", err)
}
switch repo {
case "revel":
title, repo, localVersion = "Revel Framework", "github.com/revel/revel", v.revelVersion
case "cmd":
title, repo, localVersion = "Revel Cmd", "github.com/revel/cmd/revel", v.cmdVersion
case "modules":
title, repo, localVersion = "Revel Modules", "github.com/revel/modules", v.modulesVersion
}
// Only do an update on the first loop, and if specified to update
versionInfo += v.outputVersion(title, repo, localVersion, versonFromRepo)
}
return
}
// Prints out the local and remote versions, calls update if needed.
func (v *VersionCommand) outputVersion(title, repo string, local, remote *model.Version) (output string) {
buffer := &bytes.Buffer{}
remoteVersion := "Unknown"
if remote != nil {
remoteVersion = remote.VersionString()
}
localVersion := "Unknown"
if local != nil {
localVersion = local.VersionString()
}
fmt.Fprintf(buffer, "%s\t:\t%s\t(%s remote master branch)\n", title, localVersion, remoteVersion)
return buffer.String()
}
// Returns the version from the repository.
func (v *VersionCommand) versionFromRepo(repoName, branchName, fileName string) (version *model.Version, err error) {
if branchName == "" {
branchName = "master"
}
// Try to download the version of file from the repo, just use an http connection to retrieve the source
// Assuming that the repo is github
fullurl := "https://raw.githubusercontent.com/revel/" + repoName + "/" + branchName + "/" + fileName
resp, err := http.Get(fullurl)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
utils.Logger.Info("Got version file", "from", fullurl, "content", string(body))
return v.versionFromBytes(body)
}
func (v *VersionCommand) versionFromFilepath(sourcePath string) (version *model.Version, err error) {
utils.Logger.Info("Fullpath to revel", "dir", sourcePath)
sourceStream, err := ioutil.ReadFile(filepath.Join(sourcePath, "version.go"))
if err != nil {
return
}
return v.versionFromBytes(sourceStream)
}
// Returns version information from a file called version on the gopath.
func (v *VersionCommand) versionFromBytes(sourceStream []byte) (version *model.Version, err error) {
fset := token.NewFileSet() // positions are relative to fset
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, "", sourceStream, parser.ParseComments)
if err != nil {
err = utils.NewBuildError("Failed to parse Revel version error:", "error", err)
return
}
version = &model.Version{}
// Print the imports from the file's AST.
for _, s := range f.Decls {
genDecl, ok := s.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok != token.CONST {
continue
}
for _, a := range genDecl.Specs {
spec := a.(*ast.ValueSpec)
r := spec.Values[0].(*ast.BasicLit)
switch spec.Names[0].Name {
case "Version":
if err = version.ParseVersion(strings.ReplaceAll(r.Value, `"`, "")); err != nil {
return
}
case "BuildDate":
version.BuildDate = r.Value
case "MinimumGoVersion":
version.MinGoVersion = r.Value
}
}
}
return
}
// Fetch the local version of revel from the file system.
func (v *VersionCommand) updateLocalVersions() {
v.cmdVersion = &model.Version{}
if err := v.cmdVersion.ParseVersion(cmd.Version); err != nil {
utils.Logger.Warn("Error parsing version", "error", err, "version", cmd.Version)
return
}
v.cmdVersion.BuildDate = cmd.BuildDate
v.cmdVersion.MinGoVersion = cmd.MinimumGoVersion
if v.Command.Version.ImportPath == "" {
return
}
pathMap, err := utils.FindSrcPaths(v.Command.AppPath, []string{model.RevelImportPath, model.RevelModulesImportPath}, v.Command.PackageResolver)
if err != nil {
utils.Logger.Warn("Unable to extract version information from Revel library", "path", pathMap[model.RevelImportPath], "error", err)
return
}
utils.Logger.Info("Fullpath to revel modules", "dir", pathMap[model.RevelImportPath])
v.revelVersion, err = v.versionFromFilepath(pathMap[model.RevelImportPath])
if err != nil {
utils.Logger.Warn("Unable to extract version information from Revel", "error,err")
}
v.modulesVersion, err = v.versionFromFilepath(pathMap[model.RevelModulesImportPath])
if err != nil {
utils.Logger.Warn("Unable to extract version information from Revel Modules", "path", pathMap[model.RevelModulesImportPath], "error", err)
}
}

View File

@@ -1,18 +1,20 @@
package main_test package main_test
import ( import (
"github.com/revel/cmd/model" "errors"
"github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/revel/cmd/model"
main "github.com/revel/cmd/revel"
"github.com/stretchr/testify/assert"
) )
// test the commands // test the commands.
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
a := assert.New(t) a := assert.New(t)
gopath := setup("revel-test-version", a) gopath := setup("revel-test-version", a)
t.Run("Version", func(t *testing.T) { t.Run("Version", func(t *testing.T) {
a := assert.New(t) a := assert.New(t)
@@ -33,9 +35,10 @@ func TestVersion(t *testing.T) {
c.Version.ImportPath = c.ImportPath c.Version.ImportPath = c.ImportPath
a.Nil(main.Commands[model.VERSION].RunWith(c), "Failed to run version-test") a.Nil(main.Commands[model.VERSION].RunWith(c), "Failed to run version-test")
}) })
if !t.Failed() { if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil { if err := os.RemoveAll(gopath); err != nil && !errors.Is(err, os.ErrNotExist) {
a.Fail("Failed to remove test path") a.Fail("Failed to remove test path", err.Error())
} }
} }
} }

View File

@@ -5,12 +5,8 @@
package tests package tests
import ( import (
"fmt"
"html/template" "html/template"
"reflect" "reflect"
"strings"
"github.com/revel/cmd/utils"
) )
// TestSuiteDesc is used for storing information about a single test suite. // TestSuiteDesc is used for storing information about a single test suite.
@@ -46,111 +42,3 @@ type TestResult struct {
ErrorHTML template.HTML ErrorHTML template.HTML
ErrorSummary string ErrorSummary string
} }
var (
testSuites []TestSuiteDesc // A list of all available tests.
none = []reflect.Value{} // It is used as input for reflect call in a few places.
// registeredTests simplifies the search of test suites by their name.
// "TestSuite.TestName" is used as a key. Value represents index in testSuites.
registeredTests map[string]int
)
/*
Below are helper functions.
*/
// describeSuite expects testsuite interface as input parameter
// and returns its description in a form of TestSuiteDesc structure.
func describeSuite(testSuite interface{}) TestSuiteDesc {
t := reflect.TypeOf(testSuite)
// Get a list of methods of the embedded test type.
// It will be used to make sure the same tests are not included in multiple test suites.
super := t.Elem().Field(0).Type
superMethods := map[string]bool{}
for i := 0; i < super.NumMethod(); i++ {
// Save the current method's name.
superMethods[super.Method(i).Name] = true
}
// Get a list of methods on the test suite that take no parameters, return
// no results, and were not part of the embedded type's method set.
var tests []TestDesc
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
mt := m.Type
// Make sure the test method meets the criterias:
// - method of testSuite without input parameters;
// - nothing is returned;
// - has "Test" prefix;
// - doesn't belong to the embedded structure.
methodWithoutParams := (mt.NumIn() == 1 && mt.In(0) == t)
nothingReturned := (mt.NumOut() == 0)
hasTestPrefix := (strings.HasPrefix(m.Name, "Test"))
if methodWithoutParams && nothingReturned && hasTestPrefix && !superMethods[m.Name] {
// Register the test suite's index so we can quickly find it by test's name later.
registeredTests[t.Elem().Name()+"."+m.Name] = len(testSuites)
// Add test to the list of tests.
tests = append(tests, TestDesc{m.Name})
}
}
return TestSuiteDesc{
Name: t.Elem().Name(),
Tests: tests,
Elem: t.Elem(),
}
}
// errorSummary gets an error and returns its summary in human readable format.
func errorSummary(err *utils.Error) (message string) {
expectedPrefix := "(expected)"
actualPrefix := "(actual)"
errDesc := err.Description
//strip the actual/expected stuff to provide more condensed display.
if strings.Index(errDesc, expectedPrefix) == 0 {
errDesc = errDesc[len(expectedPrefix):]
}
if strings.LastIndex(errDesc, actualPrefix) > 0 {
errDesc = errDesc[0 : len(errDesc)-len(actualPrefix)]
}
errFile := err.Path
slashIdx := strings.LastIndex(errFile, "/")
if slashIdx > 0 {
errFile = errFile[slashIdx+1:]
}
message = fmt.Sprintf("%s %s#%d", errDesc, errFile, err.Line)
/*
// If line of error isn't known return the message as is.
if err.Line == 0 {
return
}
// Otherwise, include info about the line number and the relevant
// source code lines.
message += fmt.Sprintf(" (around line %d): ", err.Line)
for _, line := range err.ContextSource() {
if line.IsError {
message += line.Source
}
}
*/
return
}
//sortbySuiteName sorts the testsuites by name.
type sortBySuiteName []interface{}
func (a sortBySuiteName) Len() int { return len(a) }
func (a sortBySuiteName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortBySuiteName) Less(i, j int) bool {
return reflect.TypeOf(a[i]).Elem().Name() < reflect.TypeOf(a[j]).Elem().Name()
}

View File

@@ -1,7 +1,11 @@
package utils package utils
import ( import (
"errors"
"fmt" "fmt"
"regexp"
"strconv"
"github.com/revel/cmd/logger" "github.com/revel/cmd/logger"
) )
@@ -13,7 +17,7 @@ type (
} }
) )
// Returns a new builed error // Returns a new builed error.
func NewBuildError(message string, args ...interface{}) (b *BuildError) { func NewBuildError(message string, args ...interface{}) (b *BuildError) {
Logger.Info(message, args...) Logger.Info(message, args...)
b = &BuildError{} b = &BuildError{}
@@ -24,22 +28,80 @@ func NewBuildError(message string, args ...interface{}) (b *BuildError) {
return b return b
} }
// Returns a new BuildError if err is not nil // Returns a new BuildError if err is not nil.
func NewBuildIfError(err error, message string, args ...interface{}) (b error) { func NewBuildIfError(err error, message string, args ...interface{}) (b error) {
if err != nil { if err != nil {
if berr, ok := err.(*BuildError); ok { var berr *BuildError
if errors.As(err, &berr) {
// This is already a build error so just append the args // This is already a build error so just append the args
berr.Args = append(berr.Args, args...) berr.Args = append(berr.Args, args...)
return berr return berr
} else {
args = append(args, "error", err.Error())
b = NewBuildError(message, args...)
} }
args = append(args, "error", err.Error())
b = NewBuildError(message, args...)
} }
return return
} }
// BuildError implements Error() string // BuildError implements Error() string.
func (b *BuildError) Error() string { func (b *BuildError) Error() string {
return fmt.Sprint(b.Message, b.Args) return fmt.Sprint(b.Message, b.Args)
} }
// Parse the output of the "go build" command.
// Return a detailed Error.
func NewCompileError(importPath, errorLink string, err error) *SourceError {
// Get the stack from the error
errorMatch := regexp.MustCompile(`(?m)^([^:#]+):(\d+):(\d+:)? (.*)$`).
FindSubmatch([]byte(err.Error()))
if errorMatch == nil {
errorMatch = regexp.MustCompile(`(?m)^(.*?):(\d+):\s(.*?)$`).FindSubmatch([]byte(err.Error()))
if errorMatch == nil {
Logger.Error("Failed to parse build errors", "error", err)
return &SourceError{
SourceType: "Go code",
Title: "Go Compilation Error",
Description: "See console for build error.",
}
}
errorMatch = append(errorMatch, errorMatch[3])
Logger.Error("Build errors", "errors", err)
}
// Read the source for the offending file.
var (
relFilename = string(errorMatch[1]) // e.g. "src/revel/sample/app/controllers/app.go"
absFilename = relFilename
line, _ = strconv.Atoi(string(errorMatch[2]))
description = string(errorMatch[4])
compileError = &SourceError{
SourceType: "Go code",
Title: "Go Compilation Error",
Path: relFilename,
Description: description,
Line: line,
}
)
// errorLink := paths.Config.StringDefault("error.link", "")
if errorLink != "" {
compileError.SetLink(errorLink)
}
fileStr, err := ReadLines(absFilename)
if err != nil {
compileError.MetaError = absFilename + ": " + err.Error()
Logger.Info("Unable to readlines "+compileError.MetaError, "error", err)
return compileError
}
compileError.SourceLines = fileStr
return compileError
}

View File

@@ -1,34 +1,49 @@
package utils package utils
import ( import (
"bytes"
"go/build" "go/build"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
) )
// Initialize the command based on the GO environment // Initialize the command based on the GO environment.
func CmdInit(c *exec.Cmd, basePath string) { func CmdInit(c *exec.Cmd, addGoPath bool, basePath string) {
c.Dir = basePath c.Dir = basePath
// Go 1.8 fails if we do not include the GOROOT // Dep does not like paths that are not real, convert all paths in go to real paths
c.Env = []string{"GOPATH=" + build.Default.GOPATH, "PATH=" + GetEnv("PATH"), "GOROOT="+ GetEnv("GOROOT")}
// Fetch the rest of the env variables // Fetch the rest of the env variables
for _, e := range os.Environ() { c.Env = ReducedEnv(addGoPath)
pair := strings.Split(e, "=")
if pair[0]=="GOPATH" {
continue
}
c.Env = append(c.Env,e)
}
} }
// Returns an environment variable // ReducedEnv returns a list of environment vairables by using os.Env
func GetEnv(name string) string { // it will remove the GOPATH, GOROOT if addGoPath is true
for _, v := range os.Environ() { func ReducedEnv(addGoPath bool) []string {
split := strings.Split(v, "=") realPath := &bytes.Buffer{}
if split[0] == name { env := []string{}
return strings.Join(split[1:], "") if addGoPath {
for _, p := range filepath.SplitList(build.Default.GOPATH) {
rp, _ := filepath.EvalSymlinks(p)
if realPath.Len() > 0 {
realPath.WriteString(string(filepath.ListSeparator))
}
realPath.WriteString(rp)
} }
// Go 1.8 fails if we do not include the GOROOT
env = []string{"GOPATH=" + realPath.String(), "GOROOT=" + os.Getenv("GOROOT")}
} }
return ""
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
// Always exclude gomodcache
if pair[0] == "GOMODCACHE" {
continue
} else if !addGoPath && (pair[0] == "GOPATH" || pair[0] == "GOROOT") {
continue
}
env = append(env, e)
}
return env
} }

View File

@@ -6,9 +6,9 @@ import (
"strings" "strings"
) )
// The error is a wrapper for the // The error is a wrapper for the.
type ( type (
Error struct { SourceError struct {
SourceType string // The type of source that failed to build. SourceType string // The type of source that failed to build.
Title, Path, Description string // Description of the error, as presented to the user. Title, Path, Description string // Description of the error, as presented to the user.
Line, Column int // Where the error was encountered. Line, Column int // Where the error was encountered.
@@ -24,18 +24,28 @@ type (
} }
) )
// Creates a link based on the configuration setting "errors.link" // Return a new error object.
func (e *Error) SetLink(errorLink string) { func NewError(source, title, path, description string) *SourceError {
errorLink = strings.Replace(errorLink, "{{Path}}", e.Path, -1) return &SourceError{
errorLink = strings.Replace(errorLink, "{{Line}}", strconv.Itoa(e.Line), -1) SourceType: source,
Title: title,
Path: path,
Description: description,
}
}
// Creates a link based on the configuration setting "errors.link".
func (e *SourceError) SetLink(errorLink string) {
errorLink = strings.ReplaceAll(errorLink, "{{Path}}", e.Path)
errorLink = strings.ReplaceAll(errorLink, "{{Line}}", strconv.Itoa(e.Line))
e.Link = "<a href=" + errorLink + ">" + e.Path + ":" + strconv.Itoa(e.Line) + "</a>" e.Link = "<a href=" + errorLink + ">" + e.Path + ":" + strconv.Itoa(e.Line) + "</a>"
} }
// Error method constructs a plaintext version of the error, taking // Error method constructs a plaintext version of the error, taking
// account that fields are optionally set. Returns e.g. Compilation Error // account that fields are optionally set. Returns e.g. Compilation Error
// (in views/header.html:51): expected right delim in end; got "}" // (in views/header.html:51): expected right delim in end; got "}".
func (e *Error) Error() string { func (e *SourceError) Error() string {
if e == nil { if e == nil {
panic("opps") panic("opps")
} }
@@ -60,7 +70,7 @@ func (e *Error) Error() string {
// ContextSource method returns a snippet of the source around // ContextSource method returns a snippet of the source around
// where the error occurred. // where the error occurred.
func (e *Error) ContextSource() []SourceLine { func (e *SourceError) ContextSource() []SourceLine {
if e.SourceLines == nil { if e.SourceLines == nil {
return nil return nil
} }

View File

@@ -6,13 +6,14 @@ import (
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt" "fmt"
"go/build"
"html/template" "html/template"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"golang.org/x/tools/go/packages"
) )
// DirExists returns true if the given path exists and is a directory. // DirExists returns true if the given path exists and is a directory.
@@ -39,9 +40,8 @@ func ReadLines(filename string) ([]string, error) {
return strings.Split(string(dataBytes), "\n"), nil return strings.Split(string(dataBytes), "\n"), nil
} }
// Copy file returns error // Copy file returns error.
func CopyFile(destFilename, srcFilename string) (err error) { func CopyFile(destFilename, srcFilename string) (err error) {
destFile, err := os.Create(destFilename) destFile, err := os.Create(destFilename)
if err != nil { if err != nil {
return NewBuildIfError(err, "Failed to create file", "file", destFilename) return NewBuildIfError(err, "Failed to create file", "file", destFilename)
@@ -105,7 +105,7 @@ func GenerateTemplate(filename, templateSource string, args map[string]interface
return return
} }
// Given the target path and source path and data. A template // Given the target path and source path and data. A template.
func RenderTemplate(destPath, srcPath string, data interface{}) (err error) { func RenderTemplate(destPath, srcPath string, data interface{}) (err error) {
tmpl, err := template.ParseFiles(srcPath) tmpl, err := template.ParseFiles(srcPath)
if err != nil { if err != nil {
@@ -129,7 +129,7 @@ func RenderTemplate(destPath, srcPath string, data interface{}) (err error) {
return return
} }
// Given the target path and source path and data. A template // Given the target path and source path and data. A template.
func RenderTemplateToStream(output io.Writer, srcPath []string, data interface{}) (err error) { func RenderTemplateToStream(output io.Writer, srcPath []string, data interface{}) (err error) {
tmpl, err := template.ParseFiles(srcPath...) tmpl, err := template.ParseFiles(srcPath...)
if err != nil { if err != nil {
@@ -148,10 +148,11 @@ func MustChmod(filename string, mode os.FileMode) {
PanicOnError(err, fmt.Sprintf("Failed to chmod %d %q", mode, filename)) PanicOnError(err, fmt.Sprintf("Failed to chmod %d %q", mode, filename))
} }
// Called if panic // Called if panic.
func PanicOnError(err error, msg string) { func PanicOnError(err error, msg string) {
if revErr, ok := err.(*Error); (ok && revErr != nil) || (!ok && err != nil) { var serr *SourceError
Logger.Panicf("Abort: %s: %s %s\n", msg, revErr, err) if (errors.As(err, &serr) && serr != nil) || err != nil {
Logger.Panicf("Abort: %s: %s %s", msg, serr, err)
} }
} }
@@ -160,6 +161,9 @@ func PanicOnError(err error, msg string) {
// Additionally, the trailing ".template" is stripped from the file name. // Additionally, the trailing ".template" is stripped from the file name.
// Also, dot files and dot directories are skipped. // Also, dot files and dot directories are skipped.
func CopyDir(destDir, srcDir string, data map[string]interface{}) error { func CopyDir(destDir, srcDir string, data map[string]interface{}) error {
if !DirExists(srcDir) {
return nil
}
return fsWalk(srcDir, srcDir, func(srcPath string, info os.FileInfo, err error) error { return fsWalk(srcDir, srcDir, func(srcPath string, info os.FileInfo, err error) error {
// Get the relative path from the source base, and the corresponding path in // Get the relative path from the source base, and the corresponding path in
// the dest directory. // the dest directory.
@@ -185,7 +189,6 @@ func CopyDir(destDir, srcDir string, data map[string]interface{}) error {
// If this file ends in ".template", render it as a template. // If this file ends in ".template", render it as a template.
if strings.HasSuffix(relSrcPath, ".template") { if strings.HasSuffix(relSrcPath, ".template") {
return RenderTemplate(destPath[:len(destPath)-len(".template")], srcPath, data) return RenderTemplate(destPath[:len(destPath)-len(".template")], srcPath, data)
} }
@@ -195,12 +198,13 @@ func CopyDir(destDir, srcDir string, data map[string]interface{}) error {
}) })
} }
// Shortcut to fsWalk // Shortcut to fsWalk.
func Walk(root string, walkFn filepath.WalkFunc) error { func Walk(root string, walkFn filepath.WalkFunc) error {
return fsWalk(root, root, walkFn) return fsWalk(root, root, walkFn)
} }
// Walk the tree using the function // Walk the path tree using the function
// Every file found will call the function.
func fsWalk(fname string, linkName string, walkFn filepath.WalkFunc) error { func fsWalk(fname string, linkName string, walkFn filepath.WalkFunc) error {
fsWalkFunc := func(path string, info os.FileInfo, err error) error { fsWalkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@@ -240,7 +244,7 @@ func fsWalk(fname string, linkName string, walkFn filepath.WalkFunc) error {
return err return err
} }
// Tar gz the folder // Tar gz the folder.
func TarGzDir(destFilename, srcDir string) (name string, err error) { func TarGzDir(destFilename, srcDir string) (name string, err error) {
zipFile, err := os.Create(destFilename) zipFile, err := os.Create(destFilename)
if err != nil { if err != nil {
@@ -262,6 +266,10 @@ func TarGzDir(destFilename, srcDir string) (name string, err error) {
}() }()
err = fsWalk(srcDir, srcDir, func(srcPath string, info os.FileInfo, err error) error { err = fsWalk(srcDir, srcDir, func(srcPath string, info os.FileInfo, err error) error {
if err != nil {
Logger.Debugf("error in walkFn: %s", err)
}
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
@@ -296,7 +304,7 @@ func TarGzDir(destFilename, srcDir string) (name string, err error) {
return zipFile.Name(), err return zipFile.Name(), err
} }
// Return true if the file exists // Return true if the file exists.
func Exists(filename string) bool { func Exists(filename string) bool {
_, err := os.Stat(filename) _, err := os.Stat(filename)
return err == nil return err == nil
@@ -305,9 +313,13 @@ func Exists(filename string) bool {
// empty returns true if the given directory is empty. // empty returns true if the given directory is empty.
// the directory must exist. // the directory must exist.
func Empty(dirname string) bool { func Empty(dirname string) bool {
if !DirExists(dirname) {
return true
}
dir, err := os.Open(dirname) dir, err := os.Open(dirname)
if err != nil { if err != nil {
Logger.Infof("error opening directory: %s", err) Logger.Infof("error opening directory: %s", err)
return false
} }
defer func() { defer func() {
_ = dir.Close() _ = dir.Close()
@@ -316,50 +328,82 @@ func Empty(dirname string) bool {
return len(results) == 0 return len(results) == 0
} }
// Find the full source dir for the import path, uses the build.Default.GOPATH to search for the directory // Find the full source dir for the import path, uses the build.Default.GOPATH to search for the directory.
func FindSrcPaths(appImportPath, revelImportPath string, packageResolver func(pkgName string) error) (appSourcePath, revelSourcePath string, err error) { func FindSrcPaths(appPath string, packageList []string, packageResolver func(pkgName string) error) (sourcePathsmap map[string]string, err error) {
var ( sourcePathsmap, missingList, err := findSrcPaths(appPath, packageList)
gopaths = filepath.SplitList(build.Default.GOPATH) if err != nil && packageResolver != nil || len(missingList) > 0 {
goroot = build.Default.GOROOT Logger.Info("Failed to find package, attempting to call resolver for missing packages", "missing packages", missingList)
) for _, item := range missingList {
if err = packageResolver(item); err != nil {
if len(gopaths) == 0 { return
err = errors.New("GOPATH environment variable is not set. " + }
"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
return
}
if ContainsString(gopaths, goroot) {
err = fmt.Errorf("GOPATH (%s) must not include your GOROOT (%s). "+
"Please refer to http://golang.org/doc/code.html to configure your Go environment. ",
build.Default.GOPATH, goroot)
return
}
appPkgDir := ""
appPkgSrcDir := ""
if len(appImportPath)>0 {
Logger.Info("Seeking app package","app",appImportPath)
appPkg, err := build.Import(appImportPath, "", build.FindOnly)
if err != nil {
err = fmt.Errorf("Failed to import " + appImportPath + " with error %s", err.Error())
return "","",err
} }
appPkgDir,appPkgSrcDir =appPkg.Dir, appPkg.SrcRoot sourcePathsmap, missingList, err = findSrcPaths(appPath, packageList)
} }
Logger.Info("Seeking remote package","using",appImportPath, "remote",revelImportPath) if err != nil && len(missingList) > 0 {
revelPkg, err := build.Default.Import(revelImportPath, appPkgDir, build.FindOnly) for _, missing := range missingList {
if err != nil { Logger.Error("Unable to import this package", "package", missing)
Logger.Info("Resolved called Seeking remote package","using",appImportPath, "remote",revelImportPath) }
packageResolver(revelImportPath) }
revelPkg, err = build.Import(revelImportPath, appPkgDir, build.FindOnly)
if err != nil { return
err = fmt.Errorf("Failed to find Revel with error: %s", err.Error()) }
return
// Error is used for constant errors.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
var (
ErrNoApp Error = "no app found"
ErrNoRevel Error = "no revel found"
)
// Find the full source dir for the import path, uses the build.Default.GOPATH to search for the directory.
func findSrcPaths(appPath string, packagesList []string) (sourcePathsmap map[string]string, missingList []string, err error) {
// Use packages to fetch
// by not specifying env, we will use the default env
config := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedDeps,
Dir: appPath,
}
config.Env = ReducedEnv(false)
sourcePathsmap = map[string]string{}
Logger.Infof("Environment path %s root %s config env %s", os.Getenv("GOPATH"), os.Getenv("GOROOT"), config.Env)
pkgs, err := packages.Load(config, packagesList...)
Logger.Infof("Environment path %s root %s config env %s", os.Getenv("GOPATH"), os.Getenv("GOROOT"), config.Env)
Logger.Info("Loaded packages ", "len results", len(pkgs), "error", err, "basedir", appPath)
for _, packageName := range packagesList {
found := false
log := Logger.New("seeking", packageName)
for _, pck := range pkgs {
log.Info("Found package", "package", pck.ID)
if pck.ID == packageName {
if pck.Errors != nil && len(pck.Errors) > 0 {
log.Error("Error ", "count", len(pck.Errors), "App Import Path", pck.ID, "filesystem path", pck.PkgPath, "errors", pck.Errors)
// continue
}
// a,_ := pck.MarshalJSON()
log.Info("Found ", "count", len(pck.GoFiles), "App Import Path", pck.ID, "apppath", appPath)
if len(pck.GoFiles) > 0 {
sourcePathsmap[packageName] = filepath.Dir(pck.GoFiles[0])
found = true
}
}
}
if !found {
if packageName == "github.com/revel/revel" {
err = ErrNoRevel
} else {
err = ErrNoApp
}
missingList = append(missingList, packageName)
} }
} }
revelSourcePath, appSourcePath = revelPkg.Dir[:len(revelPkg.Dir)-len(revelImportPath)], appPkgSrcDir
return return
} }

View File

@@ -2,10 +2,11 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/revel/cmd/logger"
"github.com/revel/config"
"os" "os"
"strings" "strings"
"github.com/revel/cmd/logger"
"github.com/revel/config"
) )
var Logger = logger.New() var Logger = logger.New()
@@ -31,8 +32,8 @@ func InitLogger(basePath string, logLevel logger.LogLevel) {
} }
// This function is to throw a panic that may be caught by the packger so it can perform the needed // This function is to throw a panic that may be caught by the packger so it can perform the needed
// imports // imports.
func Retry(format string, args ...interface{}) { func Retryf(format string, args ...interface{}) {
// Ensure the user's command prompt starts on the next line. // Ensure the user's command prompt starts on the next line.
if !strings.HasSuffix(format, "\n") { if !strings.HasSuffix(format, "\n") {
format += "\n" format += "\n"

View File

@@ -1,6 +1,6 @@
package utils package utils
// Return true if the target string is in the list // Return true if the target string is in the list.
func ContainsString(list []string, target string) bool { func ContainsString(list []string, target string) bool {
for _, el := range list { for _, el := range list {
if el == target { if el == target {

View File

@@ -1,16 +1,16 @@
// Copyright (c) 2012-2017 The Revel Framework Authors, All rights reserved. // Copyright (c) 2012-2018 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style // Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package cmd package cmd
const ( const (
// Version current Revel Command version // Version current Revel version
Version = "0.20.2" Version = "1.2.0-dev"
// BuildDate latest commit/release date // BuildDate latest commit/release date
BuildDate = "2018-10-02" BuildDate = "2022-04-11"
// MinimumGoVersion minimum required Go version for Revel // MinimumGoVersion minimum required Go version for Revel
MinimumGoVersion = ">= go1.8" MinimumGoVersion = ">= go1.17"
) )

View File

@@ -9,18 +9,18 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"gopkg.in/fsnotify/fsnotify.v1"
"time"
) )
// Listener is an interface for receivers of filesystem events. // Listener is an interface for receivers of filesystem events.
type Listener interface { type Listener interface {
// Refresh is invoked by the watcher on relevant filesystem events. // Refresh is invoked by the watcher on relevant filesystem events.
// If the listener returns an error, it is served to the user on the current request. // If the listener returns an error, it is served to the user on the current request.
Refresh() *utils.Error Refresh() *utils.SourceError
} }
// DiscerningListener allows the receiver to selectively watch files. // DiscerningListener allows the receiver to selectively watch files.
@@ -44,24 +44,24 @@ type Watcher struct {
paths *model.RevelContainer paths *model.RevelContainer
refreshTimer *time.Timer // The timer to countdown the next refresh refreshTimer *time.Timer // The timer to countdown the next refresh
timerMutex *sync.Mutex // A mutex to prevent concurrent updates timerMutex *sync.Mutex // A mutex to prevent concurrent updates
refreshChannel chan *utils.Error refreshChannel chan *utils.SourceError
refreshChannelCount int refreshChannelCount int
refreshTimerMS time.Duration // The number of milliseconds between refreshing builds refreshInterval time.Duration // The interval between refreshing builds
} }
// Creates a new watched based on the container // Creates a new watched based on the container.
func NewWatcher(paths *model.RevelContainer, eagerRefresh bool) *Watcher { func NewWatcher(paths *model.RevelContainer, eagerRefresh bool) *Watcher {
return &Watcher{ return &Watcher{
forceRefresh: true, forceRefresh: true,
lastError: -1, lastError: -1,
paths: paths, paths: paths,
refreshTimerMS: time.Duration(paths.Config.IntDefault("watch.rebuild.delay", 10)), refreshInterval: time.Duration(paths.Config.IntDefault("watch.rebuild.delay", 1000)) * time.Millisecond,
eagerRefresh: eagerRefresh || eagerRefresh: eagerRefresh ||
paths.DevMode && paths.DevMode &&
paths.Config.BoolDefault("watch", true) && paths.Config.BoolDefault("watch", true) &&
paths.Config.StringDefault("watch.mode", "normal") == "eager", paths.Config.StringDefault("watch.mode", "normal") == "eager",
timerMutex: &sync.Mutex{}, timerMutex: &sync.Mutex{},
refreshChannel: make(chan *utils.Error, 10), refreshChannel: make(chan *utils.SourceError, 10),
refreshChannelCount: 0, refreshChannelCount: 0,
} }
} }
@@ -109,9 +109,7 @@ func (w *Watcher) Listen(listener Listener, roots ...string) {
continue continue
} }
var watcherWalker func(path string, info os.FileInfo, err error) error watcherWalker := func(path string, info os.FileInfo, err error) error {
watcherWalker = func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
utils.Logger.Fatal("Watcher: Error walking path:", "error", err) utils.Logger.Fatal("Watcher: Error walking path:", "error", err)
return nil return nil
@@ -150,7 +148,6 @@ func (w *Watcher) Listen(listener Listener, roots ...string) {
// NotifyWhenUpdated notifies the watcher when a file event is received. // NotifyWhenUpdated notifies the watcher when a file event is received.
func (w *Watcher) NotifyWhenUpdated(listener Listener, watcher *fsnotify.Watcher) { func (w *Watcher) NotifyWhenUpdated(listener Listener, watcher *fsnotify.Watcher) {
for { for {
select { select {
case ev := <-watcher.Events: case ev := <-watcher.Events:
@@ -166,7 +163,10 @@ func (w *Watcher) NotifyWhenUpdated(listener Listener, watcher *fsnotify.Watcher
} else { } else {
// Run refresh in parallel // Run refresh in parallel
go func() { go func() {
w.notifyInProcess(listener) if err := w.notifyInProcess(listener); err != nil {
utils.Logger.Error("failed to notify",
"error", err)
}
}() }()
} }
} }
@@ -178,7 +178,7 @@ func (w *Watcher) NotifyWhenUpdated(listener Listener, watcher *fsnotify.Watcher
// Notify causes the watcher to forward any change events to listeners. // Notify causes the watcher to forward any change events to listeners.
// It returns the first (if any) error returned. // It returns the first (if any) error returned.
func (w *Watcher) Notify() *utils.Error { func (w *Watcher) Notify() *utils.SourceError {
if w.serial { if w.serial {
// Serialize Notify() calls. // Serialize Notify() calls.
w.notifyMutex.Lock() w.notifyMutex.Lock()
@@ -205,9 +205,10 @@ func (w *Watcher) Notify() *utils.Error {
break break
} }
utils.Logger.Info("Watcher:Notify refresh state", "Current Index", i, " last error index", w.lastError) utils.Logger.Info("Watcher:Notify refresh state", "Current Index", i, " last error index", w.lastError,
"force", w.forceRefresh, "refresh", refresh, "lastError", w.lastError == i)
if w.forceRefresh || refresh || w.lastError == i { if w.forceRefresh || refresh || w.lastError == i {
var err *utils.Error var err *utils.SourceError
if w.serial { if w.serial {
err = listener.Refresh() err = listener.Refresh()
} else { } else {
@@ -217,10 +218,10 @@ func (w *Watcher) Notify() *utils.Error {
w.lastError = i w.lastError = i
w.forceRefresh = true w.forceRefresh = true
return err return err
} else {
w.lastError = -1
w.forceRefresh = false
} }
w.lastError = -1
w.forceRefresh = false
} }
} }
@@ -228,8 +229,8 @@ func (w *Watcher) Notify() *utils.Error {
} }
// Build a queue for refresh notifications // Build a queue for refresh notifications
// this will not return until one of the queue completes // this will not return until one of the queue completes.
func (w *Watcher) notifyInProcess(listener Listener) (err *utils.Error) { func (w *Watcher) notifyInProcess(listener Listener) (err *utils.SourceError) {
shouldReturn := false shouldReturn := false
// This code block ensures that either a timer is created // This code block ensures that either a timer is created
// or that a process would be added the the h.refreshChannel // or that a process would be added the the h.refreshChannel
@@ -240,11 +241,11 @@ func (w *Watcher) notifyInProcess(listener Listener) (err *utils.Error) {
w.forceRefresh = true w.forceRefresh = true
if w.refreshTimer != nil { if w.refreshTimer != nil {
utils.Logger.Info("Found existing timer running, resetting") utils.Logger.Info("Found existing timer running, resetting")
w.refreshTimer.Reset(time.Millisecond * w.refreshTimerMS) w.refreshTimer.Reset(w.refreshInterval)
shouldReturn = true shouldReturn = true
w.refreshChannelCount++ w.refreshChannelCount++
} else { } else {
w.refreshTimer = time.NewTimer(time.Millisecond * w.refreshTimerMS) w.refreshTimer = time.NewTimer(w.refreshInterval)
} }
}() }()
@@ -291,16 +292,3 @@ func (w *Watcher) rebuildRequired(ev fsnotify.Event, listener Listener) bool {
} }
return true return true
} }
/*
var WatchFilter = func(c *Controller, fc []Filter) {
if MainWatcher != nil {
err := MainWatcher.Notify()
if err != nil {
c.Result = c.RenderError(err)
return
}
}
fc[0](c, fc[1:])
}
*/