Lint fixes

This commit is contained in:
Paul Tötterman
2020-10-19 13:40:52 +03:00
parent 3cec19ee62
commit 3d924a016b
65 changed files with 884 additions and 1083 deletions

View File

@@ -4,14 +4,15 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
)
@@ -39,9 +40,8 @@ func ReadLines(filename string) ([]string, error) {
return strings.Split(string(dataBytes), "\n"), nil
}
// Copy file returns error
// Copy file returns error.
func CopyFile(destFilename, srcFilename string) (err error) {
destFile, err := os.Create(destFilename)
if err != nil {
return NewBuildIfError(err, "Failed to create file", "file", destFilename)
@@ -105,11 +105,11 @@ func GenerateTemplate(filename, templateSource string, args map[string]interface
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) {
tmpl, err := template.ParseFiles(srcPath)
if err != nil {
return NewBuildIfError(err, "Failed to parse template " + srcPath)
return NewBuildIfError(err, "Failed to parse template "+srcPath)
}
f, err := os.Create(destPath)
@@ -119,26 +119,26 @@ func RenderTemplate(destPath, srcPath string, data interface{}) (err error) {
err = tmpl.Execute(f, data)
if err != nil {
return NewBuildIfError(err, "Failed to Render template " + srcPath)
return NewBuildIfError(err, "Failed to Render template "+srcPath)
}
err = f.Close()
if err != nil {
return NewBuildIfError(err, "Failed to close file stream " + destPath)
return NewBuildIfError(err, "Failed to close file stream "+destPath)
}
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) {
tmpl, err := template.ParseFiles(srcPath...)
if err != nil {
return NewBuildIfError(err, "Failed to parse template " + srcPath[0])
return NewBuildIfError(err, "Failed to parse template "+srcPath[0])
}
err = tmpl.Execute(output, data)
if err != nil {
return NewBuildIfError(err, "Failed to render template " + srcPath[0])
return NewBuildIfError(err, "Failed to render template "+srcPath[0])
}
return
}
@@ -148,7 +148,7 @@ func MustChmod(filename string, mode os.FileMode) {
PanicOnError(err, fmt.Sprintf("Failed to chmod %d %q", mode, filename))
}
// Called if panic
// Called if panic.
func PanicOnError(err error, msg string) {
if revErr, ok := err.(*SourceError); (ok && revErr != nil) || (!ok && err != nil) {
Logger.Panicf("Abort: %s: %s %s", msg, revErr, err)
@@ -181,15 +181,14 @@ func CopyDir(destDir, srcDir string, data map[string]interface{}) error {
if info.IsDir() {
err := os.MkdirAll(filepath.Join(destDir, relSrcPath), 0777)
if !os.IsExist(err) {
return NewBuildIfError(err, "Failed to create directory", "path", destDir + "/" + relSrcPath)
return NewBuildIfError(err, "Failed to create directory", "path", destDir+"/"+relSrcPath)
}
return nil
}
// If this file ends in ".template", render it as a template.
if strings.HasSuffix(relSrcPath, ".template") {
return RenderTemplate(destPath[:len(destPath) - len(".template")], srcPath, data)
return RenderTemplate(destPath[:len(destPath)-len(".template")], srcPath, data)
}
// Else, just copy it over.
@@ -198,13 +197,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 {
return fsWalk(root, root, walkFn)
}
// Walk the path tree using the function
// Every file found will call the function
// Every file found will call the function.
func fsWalk(fname string, linkName string, walkFn filepath.WalkFunc) error {
fsWalkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
@@ -219,7 +218,7 @@ func fsWalk(fname string, linkName string, walkFn filepath.WalkFunc) error {
path = filepath.Join(linkName, name)
if err == nil && info.Mode() & os.ModeSymlink == os.ModeSymlink {
if err == nil && info.Mode()&os.ModeSymlink == os.ModeSymlink {
var symlinkPath string
symlinkPath, err = filepath.EvalSymlinks(path)
if err != nil {
@@ -244,7 +243,7 @@ func fsWalk(fname string, linkName string, walkFn filepath.WalkFunc) error {
return err
}
// Tar gz the folder
// Tar gz the folder.
func TarGzDir(destFilename, srcDir string) (name string, err error) {
zipFile, err := os.Create(destFilename)
if err != nil {
@@ -266,6 +265,10 @@ func TarGzDir(destFilename, srcDir string) (name string, err 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() {
return nil
}
@@ -300,7 +303,7 @@ func TarGzDir(destFilename, srcDir string) (name string, err error) {
return zipFile.Name(), err
}
// Return true if the file exists
// Return true if the file exists.
func Exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
@@ -324,7 +327,7 @@ func Empty(dirname string) bool {
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(appPath string, packageList []string, packageResolver func(pkgName string) error) (sourcePathsmap map[string]string, err error) {
sourcePathsmap, missingList, err := findSrcPaths(appPath, packageList)
if err != nil && packageResolver != nil || len(missingList) > 0 {
@@ -345,16 +348,18 @@ func FindSrcPaths(appPath string, packageList []string, packageResolver func(pkg
return
}
var NO_APP_FOUND = errors.New("No app found")
var NO_REVEL_FOUND = errors.New("No revel found")
var (
NO_APP_FOUND = errors.New("no app found")
NO_REVEL_FOUND = errors.New("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) {
// 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,
Dir:appPath,
Dir: appPath,
}
sourcePathsmap = map[string]string{}
Logger.Infof("Environment path %s root %s config env %s", os.Getenv("GOPATH"), os.Getenv("GOROOT"), config.Env)
@@ -371,9 +376,8 @@ func findSrcPaths(appPath string, packagesList []string) (sourcePathsmap map[str
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()
// 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])