Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari committed Sep 6, 2021
1 parent 89b1643 commit 818213a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# tenv
tenv is analyzer that detects environment variable not using t.Setenv
tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17

[![test_and_lint](https://github.com/sivchari/tenv/actions/workflows/workflows.yml/badge.svg?branch=main)](https://github.com/sivchari/tenv/actions/workflows/workflows.yml)

Expand Down Expand Up @@ -94,7 +94,7 @@ go vet -vettool=`which tenv` -tenv.f main.go
```yaml
- run:
name: Install tenv
command: go get github.com/sivchari/tenv
command: go install github.com/sivchari/tenv

- run:
name: Run tenv
Expand All @@ -105,7 +105,7 @@ go vet -vettool=`which tenv` -tenv.f main.go

```yaml
- name: Install tenv
run: go get github.com/sivchari/tenv
run: go install github.com/sivchari/tenv

- name: Run tenv
run: go vet -vettool=`which tenv` ./...
Expand Down
60 changes: 40 additions & 20 deletions tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package tenv

import (
"go/ast"
"io/ioutil"
"log"
"runtime"
"strconv"
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const doc = "tenv is analyzer that detects environment variable not using t.Setenv"
const doc = "tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17"

// Analyzer is tenv analyzer
var Analyzer = &analysis.Analyzer{
Expand All @@ -24,12 +25,13 @@ var Analyzer = &analysis.Analyzer{
},
}

const F = "f"

var fopt bool
var (
F = "force"
fflag bool
)

func init() {
Analyzer.Flags.BoolVar(&fopt, F, false, "the force option will also run against code prior to Go1.17")
Analyzer.Flags.BoolVar(&fflag, F, false, "the force option will also run against code prior to Go1.17")
}

func run(pass *analysis.Pass) (interface{}, error) {
Expand Down Expand Up @@ -92,8 +94,10 @@ func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, n *ast.FuncDecl) boo
}
funName := x.Name + "." + fun.Sel.Name
if funName == "os.Setenv" {
foldV := checkVersion()
if foldV >= 1.17 || isForceExec() {
if checkVersion() {
pass.Reportf(stmt.Pos(), "func %s is not using t.Setenv", n.Name.Name)
}
if isForceExec() {
pass.Reportf(stmt.Pos(), "func %s is not using t.Setenv", n.Name.Name)
}
}
Expand All @@ -119,8 +123,10 @@ func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, n *ast.FuncDecl) bool {
}
funName := x.Name + "." + fun.Sel.Name
if funName == "os.Setenv" {
foldV := checkVersion()
if foldV >= 1.17 || isForceExec() {
if checkVersion() {
pass.Reportf(stmt.Pos(), "func %s is not using t.Setenv", n.Name.Name)
}
if isForceExec() {
pass.Reportf(stmt.Pos(), "func %s is not using t.Setenv", n.Name.Name)
}
}
Expand All @@ -142,8 +148,10 @@ func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, n *ast.FuncDecl)
}
funName := x.Name + "." + fun.Sel.Name
if funName == "os.Setenv" {
foldV := checkVersion()
if foldV >= 1.17 || isForceExec() {
if checkVersion() {
pass.Reportf(stmt.Pos(), "func %s is not using t.Setenv", n.Name.Name)
}
if isForceExec() {
pass.Reportf(stmt.Pos(), "func %s is not using t.Setenv", n.Name.Name)
}
}
Expand Down Expand Up @@ -176,23 +184,35 @@ func checkGenDecl(pass *analysis.Pass, decl *ast.GenDecl) {

funName := x.Name + "." + selectorExpr.Sel.Name
if funName == "os.Setenv" {
foldV := checkVersion()
if foldV >= 1.17 || isForceExec() {
if checkVersion() {
pass.Reportf(valueSpec.Pos(), "variable %s is not using t.Setenv", variable)
}
if isForceExec() {
pass.Reportf(valueSpec.Pos(), "variable %s is not using t.Setenv", variable)
}
}
}
}

func checkVersion() float64 {
version := strings.Trim(runtime.Version(), "go")
foldV, err := strconv.ParseFloat(version[0:4], 64)
func checkVersion() bool {
data, err := ioutil.ReadFile("go.mod")
if err != nil {
log.Printf("read go.mod error: %v", err)
return false
}
mod, err := modfile.Parse("", data, nil)
if err != nil {
log.Println(err)
log.Printf("parse go.mod error: %v", err)
return false
}
floatVersion, err := strconv.ParseFloat(mod.Go.Version, 64)
if err != nil {
log.Printf("parse go.mod version error: %v", err)
return false
}
return foldV
return floatVersion >= 1.17
}

func isForceExec() bool {
return fopt
return fflag
}

0 comments on commit 818213a

Please sign in to comment.