mirror of
https://github.com/hanzoai/team.git
synced 2026-11-01 07:55:31 +00:00
* inline mattermost-govet * fix style issues * simplify the openApiSync spec test * README.md tweaks * fix missing licenses * simplify README.md * trigger server-ci on tools/mattermost-govet/** * Apply https://github.com/mattermost/mattermost-govet/commit/470cf78253f0aa85a4f4b5b50002682bf823e729 --------- Co-authored-by: Mattermost Build <[email protected]>
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package rawSql
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/token"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost/tools/mattermost-govet/util"
|
|
"golang.org/x/tools/go/analysis"
|
|
)
|
|
|
|
const (
|
|
sqlstorePackagePath = util.ServerModulePath + "/channels/store/sqlstore"
|
|
)
|
|
|
|
var Analyzer = &analysis.Analyzer{
|
|
Name: "rawSql",
|
|
Doc: "check invalid usage of raw SQL queries instead of using the squirrel lib",
|
|
Run: run,
|
|
}
|
|
|
|
func run(pass *analysis.Pass) (interface{}, error) {
|
|
if !strings.HasPrefix(pass.Pkg.Path(), sqlstorePackagePath) {
|
|
return nil, nil
|
|
}
|
|
|
|
for _, file := range pass.Files {
|
|
ast.Inspect(file, func(node ast.Node) bool {
|
|
switch n := node.(type) {
|
|
case *ast.BasicLit:
|
|
if n.Kind != token.STRING {
|
|
return false
|
|
}
|
|
|
|
unquoted, err := strconv.Unquote(n.Value)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
words := strings.Fields(strings.TrimSpace(unquoted))
|
|
if len(words) == 0 {
|
|
return false
|
|
}
|
|
|
|
switch lead := strings.ToLower(words[0]); lead {
|
|
case "select", "insert", "update":
|
|
pass.Reportf(n.Pos(), "Found leading %q in a string. Creating raw SQL queries is not allowed, please use the squirrel builder instead.", lead)
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
return nil, nil
|
|
}
|