Skip to content

Commit

Permalink
Merge pull request #83 from sashamelentyev:feat/syslog
Browse files Browse the repository at this point in the history
Add syslog.Priority
  • Loading branch information
sashamelentyev authored Oct 19, 2022
2 parents b13f455 + 04e2fd1 commit 0056db5
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
SQLIsolationLevelFlag = "sql-isolation-level"
TLSSignatureSchemeFlag = "tls-signature-scheme"
ConstantKindFlag = "constant-kind"
SyslogPriorityFlag = "syslog-priority"
)

// New returns new usestdlibvars analyzer.
Expand All @@ -51,6 +52,7 @@ func flags() flag.FlagSet {
flags.Bool(SQLIsolationLevelFlag, false, "suggest the use of sql.LevelXX.String()")
flags.Bool(TLSSignatureSchemeFlag, false, "suggest the use of tls.SignatureScheme.String()")
flags.Bool(ConstantKindFlag, false, "suggest the use of constant.Kind.String()")
flags.Bool(SyslogPriorityFlag, false, "suggest the use of syslog.Priority")
return *flags
}

Expand Down Expand Up @@ -238,6 +240,33 @@ func funArgs(pass *analysis.Pass, x *ast.Ident, fun *ast.SelectorExpr, args []as
checkHTTPMethod(pass, basicLit)
}
}
case "syslog":
switch fun.Sel.Name {
case "New":
if !lookupFlag(pass, SyslogPriorityFlag) {
return
}

if basicLit := getBasicLitFromArgs(args, 2, 0, token.INT); basicLit != nil {
checkSyslogPriority(pass, basicLit)
}
case "Dial":
if !lookupFlag(pass, SyslogPriorityFlag) {
return
}

if basicLit := getBasicLitFromArgs(args, 4, 2, token.INT); basicLit != nil {
checkSyslogPriority(pass, basicLit)
}
case "NewLogger":
if !lookupFlag(pass, SyslogPriorityFlag) {
return
}

if basicLit := getBasicLitFromArgs(args, 2, 0, token.INT); basicLit != nil {
checkSyslogPriority(pass, basicLit)
}
}
default:
// w.WriteHeader(http.StatusOk)
if fun.Sel.Name == "WriteHeader" {
Expand Down Expand Up @@ -470,6 +499,14 @@ func checkConstantKind(pass *analysis.Pass, basicLit *ast.BasicLit) {
}
}

func checkSyslogPriority(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)

if newVal, ok := mapping.SyslogPriority[currentVal]; ok {
report(pass, basicLit.Pos(), currentVal, newVal)
}
}

// getBasicLitFromArgs gets the *ast.BasicLit of a function argument.
//
// Arguments:
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestUseStdlibVars(t *testing.T) {
analyzer.SQLIsolationLevelFlag,
analyzer.TLSSignatureSchemeFlag,
analyzer.ConstantKindFlag,
analyzer.SyslogPriorityFlag,
} {
if err := a.Flags.Set(flag, "true"); err != nil {
t.Fatal(err)
Expand All @@ -36,6 +37,7 @@ func TestUseStdlibVars(t *testing.T) {
"a/sql",
"a/tls",
"a/constant",
"a/syslog",
}

analysistest.Run(t, analysistest.TestData(), a, pkgs...)
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ func main() {
templateName: "test-template.go.tmpl",
fileName: "pkg/analyzer/testdata/src/a/constant/kind.go",
},
{
mapping: mapping.SyslogPriority,
packageName: "syslog_test",
templateName: "test-syslog.go.tmpl",
fileName: "pkg/analyzer/testdata/src/a/syslog/priority.go",
},
}

for _, operation := range operations {
Expand Down
33 changes: 33 additions & 0 deletions pkg/analyzer/internal/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"database/sql"
"go/constant"
"log/syslog"
"net/http"
"net/rpc"
"os"
Expand Down Expand Up @@ -202,3 +203,35 @@ var ConstantKind = map[string]string{
constant.Float.String(): "constant.Float.String()",
constant.Complex.String(): "constant.Complex.String()",
}

var SyslogPriority = map[string]string{
// strconv.Itoa(int(syslog.LOG_EMERG)): "syslog.LOG_EMERG",
strconv.Itoa(int(syslog.LOG_ALERT)): "syslog.LOG_ALERT",
strconv.Itoa(int(syslog.LOG_CRIT)): "syslog.LOG_CRIT",
strconv.Itoa(int(syslog.LOG_ERR)): "syslog.LOG_ERR",
strconv.Itoa(int(syslog.LOG_WARNING)): "syslog.LOG_WARNING",
strconv.Itoa(int(syslog.LOG_NOTICE)): "syslog.LOG_NOTICE",
strconv.Itoa(int(syslog.LOG_INFO)): "syslog.LOG_INFO",
strconv.Itoa(int(syslog.LOG_DEBUG)): "syslog.LOG_DEBUG",

// strconv.Itoa(int(syslog.LOG_KERN)): "syslog.LOG_KERN",
strconv.Itoa(int(syslog.LOG_USER)): "syslog.LOG_USER",
strconv.Itoa(int(syslog.LOG_MAIL)): "syslog.LOG_MAIL",
strconv.Itoa(int(syslog.LOG_DAEMON)): "syslog.LOG_DAEMON",
strconv.Itoa(int(syslog.LOG_AUTH)): "syslog.LOG_AUTH",
strconv.Itoa(int(syslog.LOG_SYSLOG)): "syslog.LOG_SYSLOG",
strconv.Itoa(int(syslog.LOG_LPR)): "syslog.LOG_LPR",
strconv.Itoa(int(syslog.LOG_NEWS)): "syslog.LOG_NEWS",
strconv.Itoa(int(syslog.LOG_UUCP)): "syslog.LOG_UUCP",
strconv.Itoa(int(syslog.LOG_CRON)): "syslog.LOG_CRON",
strconv.Itoa(int(syslog.LOG_AUTHPRIV)): "syslog.LOG_AUTHPRIV",
strconv.Itoa(int(syslog.LOG_FTP)): "syslog.LOG_FTP",
strconv.Itoa(int(syslog.LOG_LOCAL0)): "syslog.LOG_LOCAL0",
strconv.Itoa(int(syslog.LOG_LOCAL1)): "syslog.LOG_LOCAL1",
strconv.Itoa(int(syslog.LOG_LOCAL2)): "syslog.LOG_LOCAL2",
strconv.Itoa(int(syslog.LOG_LOCAL3)): "syslog.LOG_LOCAL3",
strconv.Itoa(int(syslog.LOG_LOCAL4)): "syslog.LOG_LOCAL4",
strconv.Itoa(int(syslog.LOG_LOCAL5)): "syslog.LOG_LOCAL5",
strconv.Itoa(int(syslog.LOG_LOCAL6)): "syslog.LOG_LOCAL6",
strconv.Itoa(int(syslog.LOG_LOCAL7)): "syslog.LOG_LOCAL7",
}
29 changes: 29 additions & 0 deletions pkg/analyzer/internal/template/test-syslog.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Code generated by usestdlibvars, DO NOT EDIT.

package {{ .PackageName }}

import "log/syslog"

var (
{{- range $key, $value := .Mapping }}
_ = {{ $key }}
{{- end }}
)

var (
{{- range $key, $value := .Mapping }}
_, _ = syslog.New({{ $key }}, "") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
)

var (
{{- range $key, $value := .Mapping }}
_, _ = syslog.Dial("", "", {{ $key }}, "") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
)

var (
{{- range $key, $value := .Mapping }}
_, _ = syslog.NewLogger({{ $key }}, 0) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
)
121 changes: 121 additions & 0 deletions pkg/analyzer/testdata/src/a/syslog/priority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0056db5

Please sign in to comment.