Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite sqlvet using the analyzer framework. #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 33 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type SQLVet struct {
QueryCnt int32
ErrCnt int32

Cfg config.Config
ProjectRoot string
Schema *schema.Db
Cfg config.Config
Paths []string
Schema *schema.Db
}

func (s *SQLVet) reportError(format string, a ...interface{}) {
Expand All @@ -39,33 +39,19 @@ func (s *SQLVet) reportError(format string, a ...interface{}) {

// Vet performs static analysis
func (s *SQLVet) Vet() {
queries, err := vet.CheckDir(
vet.VetContext{
Schema: s.Schema,
},
s.ProjectRoot,
s.Cfg.SqlFuncMatchers,
)
if err != nil {
cli.Exit(err)
}

for _, q := range queries {
handleResult := func(q *vet.QuerySite) {
atomic.AddInt32(&s.QueryCnt, 1)

if q.Err == nil {
if cli.Verbose {
cli.Show("query detected at %s", q.Position)
}
continue
return
}

// an error in the query is detected
if flagErrFormat {
relFilePath, err := filepath.Rel(s.ProjectRoot, q.Position.Filename)
if err != nil {
relFilePath = s.ProjectRoot
}
relFilePath := q.Position.Filename
// format ref: https://github.com/reviewdog/reviewdog#errorformat
cli.Show(
"%s:%d:%d: %v",
Expand All @@ -84,6 +70,18 @@ func (s *SQLVet) Vet() {
cli.Show("")
}
}

err := vet.CheckPackages(
vet.VetContext{
Schema: s.Schema,
},
s.Paths,
s.Cfg.SqlFuncMatchers,
handleResult,
)
if err != nil {
cli.Exit(err)
}
}

// PrintSummary dumps analysis stats into stdout
Expand All @@ -97,15 +95,19 @@ func (s *SQLVet) PrintSummary() {
}

// NewSQLVet creates SQLVet for a given project dir
func NewSQLVet(projectRoot string) (*SQLVet, error) {
cfg, err := config.Load(projectRoot)
func NewSQLVet(configPath string, paths []string) (*SQLVet, error) {
cfg, err := config.Load(configPath)
if err != nil {
return nil, err
}

var dbSchema *schema.Db
if cfg.SchemaPath != "" {
dbSchema, err = schema.NewDbSchema(filepath.Join(projectRoot, cfg.SchemaPath))
schemaPath := cfg.SchemaPath
if !filepath.IsAbs(cfg.SchemaPath) {
schemaPath = filepath.Join(filepath.Dir(configPath), schemaPath)
}
dbSchema, err = schema.NewDbSchema(schemaPath)
if err != nil {
return nil, err
}
Expand All @@ -122,26 +124,26 @@ func NewSQLVet(projectRoot string) (*SQLVet, error) {
}

return &SQLVet{
Cfg: cfg,
ProjectRoot: projectRoot,
Schema: dbSchema,
Cfg: cfg,
Paths: paths,
Schema: dbSchema,
}, nil
}

func main() {
var configPath string
var rootCmd = &cobra.Command{
Use: "sqlvet PATH",
Short: "Go fearless SQL",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
Version: fmt.Sprintf("%s (%s)", version, gitCommit),
PreRun: func(cmd *cobra.Command, args []string) {
if cli.Verbose {
log.SetLevel(log.DebugLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
projectRoot := args[0]
s, err := NewSQLVet(projectRoot)
s, err := NewSQLVet(configPath, args)
if err != nil {
cli.Exit(err)
}
Expand All @@ -157,9 +159,8 @@ func main() {

},
}

rootCmd.PersistentFlags().BoolVarP(
&cli.Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringVarP(
&configPath, "config", "f", "./sqlvet.toml", "Path of the config file.")
rootCmd.PersistentFlags().BoolVarP(
&flagErrFormat, "errorformat", "e", false,
"output error in errorformat fromat for easier integration")
Expand Down
5 changes: 1 addition & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"io/ioutil"
"os"
"path/filepath"

"github.com/pelletier/go-toml"

Expand All @@ -18,9 +17,7 @@ type Config struct {
}

// Load sqlvet config from project root
func Load(searchPath string) (conf Config, err error) {
configPath := filepath.Join(searchPath, "sqlvet.toml")

func Load(configPath string) (conf Config, err error) {
if _, e := os.Stat(configPath); os.IsNotExist(e) {
conf.DbEngine = "postgres"
// return default config if not found
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *ConfigTests) SubTestMultipleMatchers(t *testing.T, fixtures struct {
`), 0644)
assert.NoError(t, err)

cfg, err := config.Load(fixtures.TmpDir)
cfg, err := config.Load(configPath)
assert.NoError(t, err)

assert.Equal(t, 2, len(cfg.SqlFuncMatchers))
Expand All @@ -73,7 +73,7 @@ func (s *ConfigTests) SubTestNoConfigFile(t *testing.T, fixtures struct {
_, e := os.Stat(configPath)
assert.True(t, os.IsNotExist(e))

cfg, err := config.Load(fixtures.TmpDir)
cfg, err := config.Load(configPath)
assert.NoError(t, err)
assert.Equal(t, config.Config{DbEngine: "postgres"}, cfg)
}
Expand Down
Loading