Updated formating

Ran through testing individually for vendored Revel applications
This commit is contained in:
notzippy@gmail.com
2020-04-26 22:24:00 -07:00
parent 07d67846c1
commit 86736d6e43
22 changed files with 239 additions and 291 deletions

View File

@@ -1,9 +1,9 @@
{ {
"GOLANG": { "GOLANG": {
"ABC":[25, 35, 50, 70], "ABC":[33, 38, 50, 70],
"ARITY":[5,6,7,8], "ARITY":[5,6,7,8],
"BLOCK_NESTING":[7, 9, 11, 13], "BLOCK_NESTING":[9, 10, 12, 13],
"CYCLO":[20, 30, 45, 60], "CYCLO":[30, 35, 45, 60],
"TOO_MANY_IVARS": [20, 25, 40, 45], "TOO_MANY_IVARS": [20, 25, 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],

View File

@@ -65,7 +65,7 @@ func (cmd AppCmd) Start(c *model.CommandConfig) error {
listeningWriter := &startupListeningWriter{os.Stdout, make(chan bool), c, &bytes.Buffer{}} listeningWriter := &startupListeningWriter{os.Stdout, make(chan bool), c, &bytes.Buffer{}}
cmd.Stdout = listeningWriter cmd.Stdout = listeningWriter
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) utils.CmdInit(cmd.Cmd, !c.Vendored, 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)
} }

View File

@@ -29,9 +29,15 @@ var importErrorPattern = regexp.MustCompile("cannot find package \"([^\"]+)\"")
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.
@@ -116,8 +122,8 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
} }
} }
// Binary path is a combination of BasePath/target directory, app's import path and its name. // Binary path is a combination of BasePath/target/app directory, app's import path and its name.
binName := filepath.Join(paths.BasePath, "target", paths.ImportPath, filepath.Base(paths.BasePath)) binName := filepath.Join(paths.BasePath, "target", "app", paths.ImportPath, filepath.Base(paths.BasePath))
// Change binary path for Windows build // Change binary path for Windows build
goos := runtime.GOOS goos := runtime.GOOS
@@ -139,14 +145,10 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
} }
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
@@ -161,13 +163,9 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
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") {
ldflags := "-ldflags= " + versionLinkerFlags flags = append(flags, "-ldflags", versionLinkerFlags)
// Add in build flags
for i := range c.BuildFlags {
ldflags += "-X '" + c.BuildFlags[i] + "'"
}
flags = append(flags, ldflags)
} }
if !contains(flags, "-tags") { if !contains(flags, "-tags") {
flags = append(flags, "-tags", buildTags) flags = append(flags, "-tags", buildTags)
@@ -177,20 +175,26 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
} }
} }
// Add in build flags
flags = append(flags, c.BuildFlags...)
// Note: It's not applicable for filepath.* usage
flags = append(flags, path.Join(paths.ImportPath, "app", "tmp"))
buildCmd := exec.Command(goPath, flags...)
if !c.Vendored {
// This is Go main path // This is Go main path
gopath := c.GoPath gopath := c.GoPath
for _, o := range paths.ModulePathMap { for _, o := range paths.ModulePathMap {
gopath += string(filepath.ListSeparator) + o.Path gopath += string(filepath.ListSeparator) + o.Path
} }
// Note: It's not applicable for filepath.* usage
flags = append(flags, path.Join(paths.ImportPath, "app", "tmp"))
buildCmd := exec.Command(goPath, flags...)
buildCmd.Env = append(os.Environ(), buildCmd.Env = append(os.Environ(),
"GOPATH=" + gopath, "GOPATH=" + gopath,
) )
utils.CmdInit(buildCmd, c.AppPath) }
utils.CmdInit(buildCmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec:", "args", buildCmd.Args, "working dir", buildCmd.Dir) utils.Logger.Info("Exec:", "args", buildCmd.Args, "working dir", buildCmd.Dir)
output, err := buildCmd.CombinedOutput() output, err := buildCmd.CombinedOutput()
@@ -202,6 +206,7 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
// 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)
@@ -418,6 +423,7 @@ func newCompileError(paths *model.RevelContainer, output []byte) *utils.SourceEr
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"

View File

@@ -1,4 +1,6 @@
package command package command
type ( type (
New struct { New struct {
ImportCommand ImportCommand
@@ -6,6 +8,7 @@ type (
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"` 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 `short:"V" long:"vendor" description:"True if project should not be configured with a go.mod"` NotVendored bool `short:"V" long:"vendor" description:"True if project should not be configured with a go.mod"`
Run bool `short:"r" long:"run" description:"True if you want to run the application right away"` Run bool `short:"r" long:"run" description:"True if you want to run the application right away"`
Callback func() error
} }
) )

View File

@@ -177,7 +177,7 @@ func (c *CommandConfig) initAppFolder() (err error) {
appFolder = filepath.Join(wd,appFolder) appFolder = filepath.Join(wd,appFolder)
} }
utils.Logger.Info("Determined app folder to be", "folder",appFolder, "working",wd) 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 // Use app folder to read the go.mod if it exists and extract the package information
goModFile := filepath.Join(appFolder,"go.mod") goModFile := filepath.Join(appFolder,"go.mod")
@@ -214,6 +214,7 @@ func (c *CommandConfig) initAppFolder() (err error) {
workingDir, _ := os.Getwd() workingDir, _ := os.Getwd()
goPathList := filepath.SplitList(c.GoPath) goPathList := filepath.SplitList(c.GoPath)
bestpath := "" bestpath := ""
if !c.Vendored {
for _, path := range goPathList { for _, path := range goPathList {
if c.Index == NEW { if c.Index == NEW {
// If the GOPATH is part of the working dir this is the most likely target // If the GOPATH is part of the working dir this is the most likely target
@@ -227,12 +228,16 @@ func (c *CommandConfig) initAppFolder() (err error) {
} }
} }
} }
utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath, "bestpath",bestpath)
if len(c.SrcRoot) == 0 && len(bestpath) > 0 { if len(c.SrcRoot) == 0 && len(bestpath) > 0 {
c.SrcRoot = bestpath c.SrcRoot = bestpath
} }
} else {
c.SrcRoot = appFolder
}
utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath, "bestpath",bestpath)
// If source root is empty and this isn't a version then skip it // If source root is empty and this isn't a version then skip it
if len(c.SrcRoot) == 0 { if len(c.SrcRoot) == 0 {
if c.Index == NEW { if c.Index == NEW {
@@ -270,32 +275,11 @@ func (c *CommandConfig) InitPackageResolver() {
utils.Logger.Info("Request for package ", "package", pkgName, "use vendor", c.Vendored) utils.Logger.Info("Request for package ", "package", pkgName, "use vendor", c.Vendored)
if c.Vendored { if c.Vendored {
goModCmd := exec.Command("go", "mod", "tidy") goModCmd := exec.Command("go", "mod", "tidy")
utils.CmdInit(goModCmd, c.AppPath) utils.CmdInit(goModCmd,!c.Vendored, c.AppPath)
goModCmd.Run()
return nil return nil
} }
//utils.Logger.Info("Using dependency manager to import package", "package", pkgName)
//
//// Check to see if the package exists locally
//_, err := build.Import(pkgName, c.AppPath, build.FindOnly)
//if err != nil {
// getCmd = exec.Command(depPath, "ensure", "-add", pkgName)
//} else {
// getCmd = exec.Command(depPath, "ensure", "-update", pkgName)
//}
//
//
//} else {
// utils.Logger.Info("No vendor folder detected, not using dependency manager to import package", "package", pkgName)
// getCmd = exec.Command(c.GoCmd, "get", "-u", pkgName)
//}
//
//utils.CmdInit(getCmd, c.AppPath)
//utils.Logger.Info("Go get command ", "exec", getCmd.Path, "dir", getCmd.Dir, "args", getCmd.Args, "env", getCmd.Env, "package", pkgName)
//output, err := getCmd.CombinedOutput()
//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))
//}
return nil return nil
} }
} }

View File

@@ -15,11 +15,11 @@ type (
sourceProcessor *SourceProcessor sourceProcessor *SourceProcessor
} }
) )
func NewSourceInfoProcessor(sourceProcessor *SourceProcessor) *SourceInfoProcessor { func NewSourceInfoProcessor(sourceProcessor *SourceProcessor) *SourceInfoProcessor {
return &SourceInfoProcessor{sourceProcessor:sourceProcessor} return &SourceInfoProcessor{sourceProcessor:sourceProcessor}
} }
func (s *SourceInfoProcessor) processPackage(p *packages.Package) (sourceInfo *model.SourceInfo) { func (s *SourceInfoProcessor) processPackage(p *packages.Package) (sourceInfo *model.SourceInfo) {
sourceInfo = &model.SourceInfo{ sourceInfo = &model.SourceInfo{
ValidationKeys: map[string]map[int]string{}, ValidationKeys: map[string]map[int]string{},
@@ -52,7 +52,8 @@ func (s *SourceInfoProcessor) processPackage(p *packages.Package) (sourceInfo *m
if isController && if isController &&
funcDecl.Recv != nil && // Must have a receiver funcDecl.Recv != nil && // Must have a receiver
funcDecl.Name.IsExported() && // be public funcDecl.Name.IsExported() && // be public
funcDecl.Type.Results != nil && len(funcDecl.Type.Results.List) == 1 { // return one result funcDecl.Type.Results != nil && len(funcDecl.Type.Results.List) == 1 {
// return one result
if m, receiver := s.getControllerFunc(funcDecl, p); m != nil { if m, receiver := s.getControllerFunc(funcDecl, p); m != nil {
methodMap[receiver] = append(methodMap[receiver], m) methodMap[receiver] = append(methodMap[receiver], m)
s.sourceProcessor.log.Info("Added method map to ", "receiver", receiver, "method", m.Name) s.sourceProcessor.log.Info("Added method map to ", "receiver", receiver, "method", m.Name)

View File

@@ -90,8 +90,11 @@ func newApp(c *model.CommandConfig) (err error) {
return err return err
} }
// This kicked off the download of the revel app, not needed for vendor
if !c.Vendored {
// At this point the versions can be set // At this point the versions can be set
c.SetVersions() c.SetVersions()
}
// copy files to new app directory // copy files to new app directory
if err = copyNewAppFiles(c); err != nil { if err = copyNewAppFiles(c); err != nil {
@@ -99,7 +102,6 @@ func newApp(c *model.CommandConfig) (err error) {
} }
// Run the vendor tool if needed // Run the vendor tool if needed
println("********** here",c.Vendored)
if c.Vendored { if c.Vendored {
if err = createModVendor(c); err != nil { if err = createModVendor(c); err != nil {
return return
@@ -122,11 +124,13 @@ func createModVendor(c *model.CommandConfig) (err error) {
utils.Logger.Info("Creating a new mod app") utils.Logger.Info("Creating a new mod app")
goModCmd := exec.Command("go", "mod", "init", filepath.Join(c.New.Package, c.AppName)) goModCmd := exec.Command("go", "mod", "init", filepath.Join(c.New.Package, c.AppName))
utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
utils.CmdInit(goModCmd, c.AppPath)
utils.Logger.Info("Exec:", "args", goModCmd.Args, "env", goModCmd.Env, "workingdir", goModCmd.Dir) utils.Logger.Info("Exec:", "args", goModCmd.Args, "env", goModCmd.Env, "workingdir", goModCmd.Dir)
getOutput, err := goModCmd.CombinedOutput() getOutput, err := goModCmd.CombinedOutput()
if c.New.Callback != nil {
err = c.New.Callback()
}
if err != nil { if err != nil {
return utils.NewBuildIfError(err, string(getOutput)) return utils.NewBuildIfError(err, string(getOutput))
} }
@@ -171,7 +175,7 @@ func createDepVendor(c *model.CommandConfig) (err error) {
} }
getCmd := exec.Command("dep", "ensure", "-v") getCmd := exec.Command("dep", "ensure", "-v")
utils.CmdInit(getCmd, c.AppPath) utils.CmdInit(getCmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec:", "args", getCmd.Args, "env", getCmd.Env, "workingdir", getCmd.Dir) utils.Logger.Info("Exec:", "args", getCmd.Args, "env", getCmd.Env, "workingdir", getCmd.Dir)
getOutput, err := getCmd.CombinedOutput() getOutput, err := getCmd.CombinedOutput()

View File

@@ -3,11 +3,8 @@ package main_test
import ( import (
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"github.com/revel/cmd/revel" "github.com/revel/cmd/revel"
"github.com/revel/cmd/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
) )
@@ -50,52 +47,3 @@ func TestNew(t *testing.T) {
} }
} }
// 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.DepVendored = true
}
t.Run("New", func(t *testing.T) {
a := assert.New(t)
c := newApp("onlyone/v/a", model.NEW, precall, a)
c.New.DepVendored = true
a.Nil(main.Commands[model.NEW].RunWith(c), "New failed")
})
t.Run("Test", func(t *testing.T) {
a := assert.New(t)
c := newApp("onlyone/v/a", model.TEST, nil, a)
a.Nil(main.Commands[model.TEST].RunWith(c), "Test failed")
})
t.Run("Build", func(t *testing.T) {
a := assert.New(t)
c := newApp("onlyone/v/a", model.BUILD, nil, a)
c.Index = model.BUILD
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) {
a := assert.New(t)
c := newApp("onlyone/v/a", model.PACKAGE, nil, a)
c.Package.TargetPath = filepath.Join(gopath, "src/onlyone/v/a", "target.tar.gz")
a.Nil(main.Commands[model.PACKAGE].RunWith(c), "Package Failed")
a.True(utils.Exists(c.Package.TargetPath), "Target package not made", c.Package.TargetPath)
})
t.Run("TestVendorDir", func(t *testing.T) {
// Check to see that no additional packages were downloaded outside the vendor folder
files, err := ioutil.ReadDir(gopath)
a.Nil(err, "Failed to read gopath folder")
// bin/ onlyone/ pkg/ src/
a.Equal(3, len(files), "Expected single file in "+gopath)
files, err = ioutil.ReadDir(filepath.Join(gopath, "src"))
a.Nil(err, "Failed to read src folder")
a.Equal(1, len(files), "Expected single file in source folder", filepath.Join(gopath, "src"))
})
if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil {
a.Fail("Failed to remove test path")
}
}
}

View File

@@ -10,10 +10,11 @@ import (
) )
// 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
// Dep does not like paths that are not real, convert all paths in go to real paths // Dep does not like paths that are not real, convert all paths in go to real paths
realPath := &bytes.Buffer{} realPath := &bytes.Buffer{}
if addGoPath {
for _, p := range filepath.SplitList(build.Default.GOPATH) { for _, p := range filepath.SplitList(build.Default.GOPATH) {
rp, _ := filepath.EvalSymlinks(p) rp, _ := filepath.EvalSymlinks(p)
if realPath.Len() > 0 { if realPath.Len() > 0 {
@@ -23,6 +24,7 @@ func CmdInit(c *exec.Cmd, basePath string) {
} }
// Go 1.8 fails if we do not include the GOROOT // Go 1.8 fails if we do not include the GOROOT
c.Env = []string{"GOPATH=" + realPath.String(), "GOROOT=" + os.Getenv("GOROOT")} c.Env = []string{"GOPATH=" + realPath.String(), "GOROOT=" + os.Getenv("GOROOT")}
}
// Fetch the rest of the env variables // Fetch the rest of the env variables
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.Split(e, "=") pair := strings.Split(e, "=")

View File

@@ -338,6 +338,7 @@ func FindSrcPaths(appPath string, packageList []string, packageResolver func(pkg
return return
} }
var NO_APP_FOUND = errors.New("No app found") var NO_APP_FOUND = errors.New("No app found")
var NO_REVEL_FOUND = errors.New("No revel found") var NO_REVEL_FOUND = errors.New("No revel found")
@@ -361,7 +362,6 @@ func findSrcPaths(appPath string, packagesList []string) (sourcePathsmap map[str
if pck.ID == packageName { if pck.ID == packageName {
if pck.Errors != nil && len(pck.Errors) > 0 { if pck.Errors != nil && len(pck.Errors) > 0 {
log.Info("Error ", "count", len(pck.Errors), "App Import Path", pck.ID, "errors", pck.Errors) log.Info("Error ", "count", len(pck.Errors), "App Import Path", pck.ID, "errors", pck.Errors)
} }
//a,_ := pck.MarshalJSON() //a,_ := pck.MarshalJSON()
log.Info("Found ", "count", len(pck.GoFiles), "App Import Path", pck.ID, "apppath", appPath) log.Info("Found ", "count", len(pck.GoFiles), "App Import Path", pck.ID, "apppath", appPath)