Skip to content

Commit

Permalink
error out when GOMOD env value is /dev/null
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancurrah committed Aug 3, 2021
1 parent 59c8d3f commit 0decae5
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions gomodguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gomodguard
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"go/parser"
"go/token"
Expand All @@ -18,8 +19,8 @@ import (

const (
goModFilename = "go.mod"
errReadingGoModFile = "unable to read go mod file %s: %w"
errParsingGoModFile = "unable to parsing go mod file %s: %w"
errReadingGoModFile = "unable to read module file %s: %w"
errParsingGoModFile = "unable to parse module file %s: %w"
)

var (
Expand Down Expand Up @@ -64,15 +65,15 @@ func (r *BlockedVersion) Message(lintedModuleVersion string) string {
var sb strings.Builder

// Add version contraint to message.
fmt.Fprintf(&sb, "version `%s` is blocked because it does not meet the version constraint `%s`.",
_, _ = fmt.Fprintf(&sb, "version `%s` is blocked because it does not meet the version constraint `%s`.",
lintedModuleVersion, r.Version)

if r.Reason == "" {
return sb.String()
}

// Add reason to message.
fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
_, _ = fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))

return sb.String()
}
Expand Down Expand Up @@ -110,13 +111,13 @@ func (r *BlockedModule) Message() string {
for i := range r.Recommendations {
switch {
case len(r.Recommendations) == 1:
fmt.Fprintf(&sb, "`%s` is a recommended module.", r.Recommendations[i])
_, _ = fmt.Fprintf(&sb, "`%s` is a recommended module.", r.Recommendations[i])
case (i+1) != len(r.Recommendations) && (i+1) == (len(r.Recommendations)-1):
fmt.Fprintf(&sb, "`%s` ", r.Recommendations[i])
_, _ = fmt.Fprintf(&sb, "`%s` ", r.Recommendations[i])
case (i + 1) != len(r.Recommendations):
fmt.Fprintf(&sb, "`%s`, ", r.Recommendations[i])
_, _ = fmt.Fprintf(&sb, "`%s`, ", r.Recommendations[i])
default:
fmt.Fprintf(&sb, "and `%s` are recommended modules.", r.Recommendations[i])
_, _ = fmt.Fprintf(&sb, "and `%s` are recommended modules.", r.Recommendations[i])
}
}

Expand All @@ -126,9 +127,9 @@ func (r *BlockedModule) Message() string {

// Add reason to message
if sb.Len() == 0 {
fmt.Fprintf(&sb, "%s.", strings.TrimRight(r.Reason, "."))
_, _ = fmt.Fprintf(&sb, "%s.", strings.TrimRight(r.Reason, "."))
} else {
fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
_, _ = fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
}

return sb.String()
Expand Down Expand Up @@ -473,9 +474,13 @@ func loadGoModFile() ([]byte, error) {
return ioutil.ReadFile(goModFilename)
}

if _, err := os.Stat(goEnv["GOMOD"]); os.IsNotExist(err) {
if _, err = os.Stat(goEnv["GOMOD"]); os.IsNotExist(err) {
return ioutil.ReadFile(goModFilename)
}

if goEnv["GOMOD"] == "/dev/null" {
return nil, errors.New("current working directory must have a go.mod file")
}

return ioutil.ReadFile(goEnv["GOMOD"])
}

0 comments on commit 0decae5

Please sign in to comment.