More linting

This commit is contained in:
Paul Tötterman
2021-02-10 16:34:20 +02:00
parent b562bd2dc5
commit ddec572d5d
29 changed files with 275 additions and 107 deletions

View File

@@ -2,7 +2,6 @@
package model
import (
"errors"
"fmt"
"path/filepath"
"sort"
@@ -13,6 +12,24 @@ import (
"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 (
// The container object for describing all Revels variables.
RevelContainer struct {
@@ -123,10 +140,10 @@ func NewRevelPaths(mode, importPath, appSrcPath string, callback RevelCallback)
// Sanity check , ensure app and conf paths exist
if !utils.DirExists(rp.AppPath) {
return rp, fmt.Errorf("no application found at path %s", rp.AppPath)
return rp, fmt.Errorf("%w: %s", ErrNoApp, rp.AppPath)
}
if !utils.DirExists(filepath.Join(rp.BasePath, "conf")) {
return rp, fmt.Errorf("no configuration found at path %s", filepath.Join(rp.BasePath, "conf"))
return rp, fmt.Errorf("%w: %s", ErrNoConfig, filepath.Join(rp.BasePath, "conf"))
}
rp.ViewsPath = filepath.Join(rp.AppPath, "views")
@@ -150,7 +167,7 @@ func NewRevelPaths(mode, importPath, appSrcPath string, callback RevelCallback)
rp.Config, err = config.LoadContext("app.conf", rp.ConfPaths)
if err != nil {
return rp, fmt.Errorf("unable to load configuration file %s", err)
return rp, fmt.Errorf("unable to load configuration file %w", err)
}
// Ensure that the selected runmode appears in app.conf.
@@ -159,7 +176,7 @@ func NewRevelPaths(mode, importPath, appSrcPath string, callback RevelCallback)
mode = config.DefaultSection
}
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)
@@ -172,13 +189,14 @@ func NewRevelPaths(mode, importPath, appSrcPath string, callback RevelCallback)
rp.HTTPSslKey = rp.Config.StringDefault("http.sslkey", "")
if rp.HTTPSsl {
if rp.HTTPSslCert == "" {
return rp, errors.New("no http.sslcert provided")
return rp, ErrMissingCert
}
if rp.HTTPSslKey == "" {
return rp, errors.New("no http.sslkey provided")
return rp, ErrMissingKey
}
}
//
rp.AppName = rp.Config.StringDefault("app.name", "(not set)")
rp.AppRoot = rp.Config.StringDefault("app.root", "")
rp.CookiePrefix = rp.Config.StringDefault("cookie.prefix", "REVEL")
@@ -201,7 +219,7 @@ func NewRevelPaths(mode, importPath, appSrcPath string, callback RevelCallback)
func (rp *RevelContainer) LoadMimeConfig() (err error) {
rp.MimeConfig, err = config.LoadContext("mime-types.conf", rp.ConfPaths)
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
}
@@ -226,10 +244,14 @@ func (rp *RevelContainer) loadModules(callback RevelCallback) (err error) {
modulePath, err := rp.ResolveImportPath(moduleImportPath)
if err != nil {
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)
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>
@@ -281,7 +303,7 @@ func (rp *RevelContainer) ResolveImportPath(importPath string) (string, error) {
pkgs, err := packages.Load(config, importPath)
if len(pkgs) == 0 {
return "", errors.New("No packages found for import " + importPath + " using app path " + rp.AppPath)
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 {
@@ -290,5 +312,5 @@ func (rp *RevelContainer) ResolveImportPath(importPath string) (string, error) {
if len(pkgs[0].GoFiles) > 0 {
return filepath.Dir(pkgs[0].GoFiles[0]), nil
}
return pkgs[0].PkgPath, errors.New("No files found in import path " + importPath)
return pkgs[0].PkgPath, fmt.Errorf("%w: %s", ErrNoFiles, importPath)
}