fix(lint): Finish to apply linter recommandation
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE 2022-08-03 13:36:13 +00:00
parent 660a4ef162
commit 7938ce2dfa
Signed by: mderasse
GPG Key ID: 55141C777B16A705
14 changed files with 37 additions and 35 deletions

View File

@ -93,6 +93,7 @@ linters:
- nolintlint # reports ill-formed or insufficient nolint directives - nolintlint # reports ill-formed or insufficient nolint directives
- nonamedreturns - nonamedreturns
- prealloc # finds slice declarations that could potentially be preallocated - prealloc # finds slice declarations that could potentially be preallocated
- revive
- sqlclosecheck - sqlclosecheck
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks - staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- structcheck # finds unused struct fields - structcheck # finds unused struct fields
@ -103,8 +104,6 @@ linters:
- unused # checks Go code for unused constants, variables, functions and types - unused # checks Go code for unused constants, variables, functions and types
- varcheck # Finds unused global variables and constants - varcheck # Finds unused global variables and constants
- wastedassign # wastedassign finds wasted assignment statements. - wastedassign # wastedassign finds wasted assignment statements.
- whitespace
- revive
# all available settings of specific linters # all available settings of specific linters
linters-settings: linters-settings:
@ -129,7 +128,7 @@ linters-settings:
dupl: dupl:
# tokens count to trigger issue, 150 by default # tokens count to trigger issue, 150 by default
threshold: 100 threshold: 150
gomoddirectives: gomoddirectives:
# Allow local `replace` directives. Default is false. # Allow local `replace` directives. Default is false.

View File

@ -1,3 +1,7 @@
# Gouick # Gouick
[![Build Status](https://drone.home.m-and-m.ovh/api/badges/mderasse/gouick/status.svg?ref=refs/heads/main)](https://drone.home.m-and-m.ovh/mderasse/gouick) [![Build Status](https://drone.home.m-and-m.ovh/api/badges/mderasse/gouick/status.svg?ref=refs/heads/main)](https://drone.home.m-and-m.ovh/mderasse/gouick)
## Improvement idea
* Refactoring of dependencies to use same architecture as APIType to use base function and avoid dups

View File

@ -2,7 +2,7 @@ package gin_gonic
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base" import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// APIType // APIType represent an empty struct respecting the APITypeInterface.
type APIType struct { type APIType struct {
base.APIType base.APIType
} }

View File

@ -2,7 +2,7 @@ package go_swagger
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base" import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// APIType // APIType represent an empty struct respecting the APITypeInterface.
type APIType struct { type APIType struct {
base.APIType base.APIType
} }

View File

@ -12,7 +12,6 @@ import (
// CheckInitialize will do preliminary check before initializing a new project. // CheckInitialize will do preliminary check before initializing a new project.
func (a APIType) CheckInitialize() error { func (a APIType) CheckInitialize() error {
log.Debugf("Starting %s check initialize", string(a.GetName())) log.Debugf("Starting %s check initialize", string(a.GetName()))
// Get current path // Get current path

View File

@ -13,7 +13,6 @@ import (
// GetInitializeUserInput will ask user to provide information that allow initialization of a project. // GetInitializeUserInput will ask user to provide information that allow initialization of a project.
func (a APIType) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) { func (a APIType) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) {
log.Debugf("Starting %s user input", a.GetName()) log.Debugf("Starting %s user input", a.GetName())
// Get current path // Get current path
@ -29,8 +28,8 @@ func (a APIType) GetInitializeUserInput(params *models.UserInputParams) (*models
log.Debug("Checking if we are in GoPath") log.Debug("Checking if we are in GoPath")
isInGoPath := helpers.IsInGoPath(currentPath) isInGoPath := helpers.IsInGoPath(currentPath)
if !isInGoPath {
if !isInGoPath {
log.Debug("We are not in GoPath, ask extra info") log.Debug("We are not in GoPath, ask extra info")
goModulePath := helpers.StringInput() goModulePath := helpers.StringInput()
log.Info("Go Module name:") log.Info("Go Module name:")

View File

@ -2,7 +2,7 @@ package api_types
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models" import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
// APITypeInterface // APITypeInterface is the interface that need to be respected by an APIType.
type APITypeInterface interface { type APITypeInterface interface {
CheckInitialize() error CheckInitialize() error
GetName() models.APITypeName GetName() models.APITypeName

View File

@ -2,7 +2,7 @@ package mojolicious
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base" import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// APIType // APIType represent an empty struct respecting the APITypeInterface.
type APIType struct { type APIType struct {
base.APIType base.APIType
} }

View File

@ -254,6 +254,7 @@ func (g Golang) PostInstall(path string) error {
if len(lineBashRc) > 1 { if len(lineBashRc) > 1 {
log.Debug("Adding env variable to .bashrc") log.Debug("Adding env variable to .bashrc")
//nolint:gosec // we did compute the file path
fh, err := os.OpenFile( fh, err := os.OpenFile(
filepath.Join(homeDir, ".bashrc"), filepath.Join(homeDir, ".bashrc"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.O_APPEND|os.O_CREATE|os.O_WRONLY,

View File

@ -162,6 +162,7 @@ func (s Swagger) Install(path string) error {
return errors.Trace(err) return errors.Trace(err)
} }
//nolint:gosec // we did compute the file path
fh, err := os.OpenFile( fh, err := os.OpenFile(
filepath.Join(path, "swagger"), filepath.Join(path, "swagger"),
os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
@ -228,6 +229,7 @@ func (s Swagger) PostInstall(path string) error {
if len(lineBashRc) > 1 { if len(lineBashRc) > 1 {
log.Debug("Adding env variable to .bashrc") log.Debug("Adding env variable to .bashrc")
//nolint:gosec // we did compute the file path
fh, err := os.OpenFile( fh, err := os.OpenFile(
filepath.Join(homeDir, ".bashrc"), filepath.Join(homeDir, ".bashrc"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.O_APPEND|os.O_CREATE|os.O_WRONLY,

View File

@ -21,6 +21,7 @@ import (
// content will probably be untar, ungzip, .... // content will probably be untar, ungzip, ....
func downloadFile(url string) (io.Reader, error) { func downloadFile(url string) (io.Reader, error) {
// Get the data // Get the data
//nolint:gosec,noctx // we trust the url
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return nil, errors.Trace(err) return nil, errors.Trace(err)
@ -93,7 +94,7 @@ func unTar(reader io.Reader, subdir string, dest string) error {
log.Debugf("Extacting %s", target) log.Debugf("Extacting %s", target)
switch header.Typeflag { switch header.Typeflag {
// create directory if doesn't exit // create directory if doesn't exit.
case tar.TypeDir: case tar.TypeDir:
if _, err := os.Stat(target); err != nil { if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0750); err != nil { if err := os.MkdirAll(target, 0750); err != nil {
@ -102,6 +103,7 @@ func unTar(reader io.Reader, subdir string, dest string) error {
} }
// create file // create file
case tar.TypeReg: case tar.TypeReg:
//nolint:gosec // we did compute the file path.
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil { if err != nil {
return errors.Trace(err) return errors.Trace(err)
@ -113,7 +115,8 @@ func unTar(reader io.Reader, subdir string, dest string) error {
} }
}() }()
// copy contents to file // copy contents to file.
//nolint:gosec // TODO: do a decompression that protect from decompression bomb.
if _, err := io.Copy(f, tr); err != nil { if _, err := io.Copy(f, tr); err != nil {
return errors.Trace(err) return errors.Trace(err)
} }
@ -166,6 +169,7 @@ func unZip(reader io.Reader, subdir string, dest string) error {
} }
} }
} else { } else {
//nolint:gosec // we did compute the file path.
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, file.Mode()) f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, file.Mode())
if err != nil { if err != nil {
return errors.Trace(err) return errors.Trace(err)
@ -189,6 +193,7 @@ func unZip(reader io.Reader, subdir string, dest string) error {
}() }()
// copy contents to file // copy contents to file
//nolint:gosec // TODO: do a decompression that protect from decompression bomb.
if _, err := io.Copy(f, fileInArchive); err != nil { if _, err := io.Copy(f, fileInArchive); err != nil {
return errors.Trace(err) return errors.Trace(err)
} }

View File

@ -10,8 +10,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// IsGouickDirectory will check if we are in the same directory as the Gouick binary.
func IsGouickDirectory(path string) (bool, error) { func IsGouickDirectory(path string) (bool, error) {
binPath, err := os.Executable() binPath, err := os.Executable()
if err != nil { if err != nil {
return false, errors.Trace(err) return false, errors.Trace(err)
@ -26,8 +26,8 @@ func IsGouickDirectory(path string) (bool, error) {
return true, nil return true, nil
} }
// IsGouickProject will check if a gouick config file exist and readeable.
func IsGouickProject(path string) (bool, error) { func IsGouickProject(path string) (bool, error) {
exist, err := fileExists(filepath.Join(path, configFile)) exist, err := fileExists(filepath.Join(path, configFile))
if err != nil { if err != nil {
return false, errors.Trace(err) return false, errors.Trace(err)
@ -40,11 +40,10 @@ func IsGouickProject(path string) (bool, error) {
return true, nil return true, nil
} }
// fileExists will check if a file exist and is readeable.
func fileExists(path string) (bool, error) { func fileExists(path string) (bool, error) {
fileInfo, err := os.Stat(path) fileInfo, err := os.Stat(path)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrPermission) { if errors.Is(err, fs.ErrPermission) {
return false, errors.NewForbidden(err, "file is not readeable") return false, errors.NewForbidden(err, "file is not readeable")
} else if errors.Is(err, fs.ErrNotExist) { } else if errors.Is(err, fs.ErrNotExist) {
@ -60,12 +59,10 @@ func fileExists(path string) (bool, error) {
return true, nil return true, nil
} }
// isDirectoryWritable // isDirectoryWritable will check if a directory path is writable.
func isDirectoryWritable(path string) (bool, error) { func isDirectoryWritable(path string) (bool, error) {
dirInfo, err := os.Stat(path) dirInfo, err := os.Stat(path)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrPermission) { if errors.Is(err, fs.ErrPermission) {
return false, errors.NewForbidden(err, "directory is not readeable") return false, errors.NewForbidden(err, "directory is not readeable")
} else if errors.Is(err, fs.ErrNotExist) { } else if errors.Is(err, fs.ErrNotExist) {
@ -86,11 +83,10 @@ func isDirectoryWritable(path string) (bool, error) {
return true, nil return true, nil
} }
// createDirectory // createDirectory will create a new directory along with necessessary parents.
func createDirectory(path string) error { func createDirectory(path string) error {
// no need to check if path exist.
// no need to check if path exist // MkdirAll will do it for us.
// MkdirAll will do it for us
err := os.MkdirAll(path, os.ModePerm) err := os.MkdirAll(path, os.ModePerm)
if err != nil { if err != nil {
return errors.Trace(err) return errors.Trace(err)
@ -100,9 +96,8 @@ func createDirectory(path string) error {
} }
// CheckAndCreateDir will check if path is writable and create directory if needed // CheckAndCreateDir will check if path is writable and create directory if needed
// return a bool true if directory created, false if not // return a bool true if directory created, false if not.
func CheckAndCreateDir(path string) (bool, error) { func CheckAndCreateDir(path string) (bool, error) {
dirWritable, err := isDirectoryWritable(path) dirWritable, err := isDirectoryWritable(path)
if dirWritable { if dirWritable {
return false, nil return false, nil
@ -123,8 +118,8 @@ func CheckAndCreateDir(path string) (bool, error) {
return true, nil return true, nil
} }
// RemoveDirectoryContent will delete the content of a provided directory path.
func RemoveDirectoryContent(path string) error { func RemoveDirectoryContent(path string) error {
dir, err := os.ReadDir(path) dir, err := os.ReadDir(path)
if err != nil { if err != nil {
return errors.Trace(err) return errors.Trace(err)

View File

@ -8,9 +8,8 @@ import (
"github.com/juju/errors" "github.com/juju/errors"
) )
// IsGoProject // IsGoProject check if we are in a golang project (have go.mod).
func IsGoProject(path string) (bool, error) { func IsGoProject(path string) (bool, error) {
exist, err := fileExists(filepath.Join(path, "go.mod")) exist, err := fileExists(filepath.Join(path, "go.mod"))
if err != nil { if err != nil {
return false, errors.Trace(err) return false, errors.Trace(err)
@ -32,7 +31,7 @@ func IsGoProject(path string) (bool, error) {
return false, nil return false, nil
} }
// IsInGoPath // IsInGoPath check if the given path is in the gopath directory.
func IsInGoPath(path string) bool { func IsInGoPath(path string) bool {
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")

View File

@ -11,12 +11,11 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// YesOrNoInput // YesOrNoInput ask user for a yes or no reply and try until we get a possible answer.
func YesOrNoInput() bool { func YesOrNoInput() bool {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
for { for {
scanner.Scan() scanner.Scan()
userInput := scanner.Text() userInput := scanner.Text()
@ -37,7 +36,7 @@ func YesOrNoInput() bool {
} }
} }
// AlphanumericalInput // AlphanumericalInput ask user for a alphanumerical string and try until we get a possible answer.
func AlphanumericalInput() string { func AlphanumericalInput() string {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
@ -61,7 +60,7 @@ func AlphanumericalInput() string {
} }
} }
// StringInput // StringInput ask user for a string and try until we get a possible answer.
func StringInput() string { func StringInput() string {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
@ -79,7 +78,7 @@ func StringInput() string {
} }
} }
// PathInput // PathInput ask user for a path string and try until we get a possible answer.
func PathInput() string { func PathInput() string {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
@ -103,7 +102,7 @@ func PathInput() string {
} }
} }
// APITypeNameInput // APITypeNameInput ask user for a string that is a valid API Type name and try until we get a possible answer.
func APITypeNameInput() models.APITypeName { func APITypeNameInput() models.APITypeName {
possibleAPITypes := make([]string, 0) possibleAPITypes := make([]string, 0)