Skip to content

Commit

Permalink
Fix CLI flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jjti committed Jan 9, 2024
1 parent 6b201ef commit 54ed04d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
24 changes: 23 additions & 1 deletion cmd/spancheck/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package main

import (
"flag"
"fmt"
"strings"

"golang.org/x/tools/go/analysis/singlechecker"

"github.com/jjti/go-spancheck"
)

func main() {
singlechecker.Main(spancheck.NewAnalyzer())
// Set the list of checks to enable.
checkOptions := []string{}
for check := range spancheck.Checks {
checkOptions = append(checkOptions, check)
}

checkStrings := ""
flag.StringVar(&checkStrings, "checks", "end", fmt.Sprintf("comma-separated list of checks to enable (options: %v)", strings.Join(checkOptions, ", ")))

// Set the list of function signatures to ignore checks for.
ignoreCheckSignatures := ""
flag.StringVar(&ignoreCheckSignatures, "ignore-check-signatures", "", "comma-separated list of regex for function signatures that disable checks on errors")
flag.Parse()

cfg := spancheck.NewDefaultConfig()
cfg.EnabledChecks = strings.Split(checkStrings, ",")
cfg.IgnoreChecksSignaturesSlice = strings.Split(ignoreCheckSignatures, ",")

singlechecker.Main(spancheck.NewAnalyzerWithConfig(cfg))
}
43 changes: 4 additions & 39 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,6 @@ type Config struct {
ignoreChecksSignatures *regexp.Regexp
}

// NewConfigFromFlags returns a new Config with default values and flags for CLI usage.
func NewConfigFromFlags() *Config {
cfg := NewDefaultConfig()

cfg.fs = flag.FlagSet{}

// Set the list of checks to enable.
checkOptions := []string{}
for check := range Checks {
checkOptions = append(checkOptions, check)
}
checkStrings := cfg.fs.String("checks", "end", fmt.Sprintf("comma-separated list of checks to enable (options: %v)", strings.Join(checkOptions, ", ")))
cfg.EnabledChecks = strings.Split(*checkStrings, ",")

// Set the list of function signatures to ignore checks for.
ignoreCheckSignatures := flag.String("ignore-check-signatures", "", "comma-separated list of regex for function signatures that disable checks on errors")
cfg.ignoreChecksSignatures = parseSignatures(*ignoreCheckSignatures)

return cfg
}

// NewDefaultConfig returns a new Config with default values.
func NewDefaultConfig() *Config {
return &Config{
Expand All @@ -105,6 +84,10 @@ func (c *Config) finalize() {
// parseSignatures sets the Ignore*CheckSignatures regex from the string slices.
func (c *Config) parseSignatures() {
if c.ignoreChecksSignatures == nil && len(c.IgnoreChecksSignaturesSlice) > 0 {
if len(c.IgnoreChecksSignaturesSlice) == 1 && c.IgnoreChecksSignaturesSlice[0] == "" {
return
}

c.ignoreChecksSignatures = createRegex(c.IgnoreChecksSignaturesSlice)
}
}
Expand Down Expand Up @@ -132,24 +115,6 @@ func parseChecks(checksSlice []string) []Check {
return checks
}

func parseSignatures(sigFlag string) *regexp.Regexp {
if sigFlag == "" {
return nil
}

sigs := []string{}
for _, sig := range strings.Split(sigFlag, ",") {
sig = strings.TrimSpace(sig)
if sig == "" {
continue
}

sigs = append(sigs, sig)
}

return createRegex(sigs)
}

func createRegex(sigs []string) *regexp.Regexp {
if len(sigs) == 0 {
return nil
Expand Down
6 changes: 0 additions & 6 deletions spancheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ var (
errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
)

// NewAnalyzer returns a new analyzer that checks for mistakes with OTEL trace spans.
// Its config is sourced from flags.
func NewAnalyzer() *analysis.Analyzer {
return newAnalyzer(NewConfigFromFlags())
}

// NewAnalyzerWithConfig returns a new analyzer configured with the Config passed in.
// Its config can be set for testing.
func NewAnalyzerWithConfig(config *Config) *analysis.Analyzer {
Expand Down
2 changes: 1 addition & 1 deletion spancheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test(t *testing.T) {
t.Parallel()

for dir, config := range map[string]*spancheck.Config{
"base": spancheck.NewConfigFromFlags(),
"base": spancheck.NewDefaultConfig(),
"disableerrorchecks": {
EnabledChecks: []string{
spancheck.EndCheck.String(),
Expand Down

0 comments on commit 54ed04d

Please sign in to comment.