Skip to content

Commit

Permalink
Merge pull request #287 from compose-spec/add-nakedret-linter
Browse files Browse the repository at this point in the history
Add "nakedret" to the linters to put dotenv in conformity
  • Loading branch information
ulyssessouza committed Jul 29, 2022
2 parents cc56ae5 + ca8f7fd commit 422bb5c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
23 changes: 11 additions & 12 deletions dotenv/godotenv.go
Expand Up @@ -62,7 +62,7 @@ func ParseWithLookup(r io.Reader, lookupFn LookupFn) (map[string]string, error)
// godotenv.Load("fileone", "filetwo")
//
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
func Load(filenames ...string) (err error) {
func Load(filenames ...string) error {
return load(false, filenames...)
}

Expand All @@ -77,20 +77,19 @@ func Load(filenames ...string) (err error) {
// godotenv.Overload("fileone", "filetwo")
//
// It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars.
func Overload(filenames ...string) (err error) {
func Overload(filenames ...string) error {
return load(true, filenames...)
}

func load(overload bool, filenames ...string) (err error) {
func load(overload bool, filenames ...string) error {
filenames = filenamesOrDefault(filenames)

for _, filename := range filenames {
err = loadFile(filename, overload)
err := loadFile(filename, overload)
if err != nil {
return // return early on a spazout
return err
}
}
return
return nil
}

var startsWithDigitRegex = regexp.MustCompile(`^\s*\d.*`) // Keys starting with numbers are ignored
Expand Down Expand Up @@ -121,12 +120,12 @@ func ReadWithLookup(lookupFn LookupFn, filenames ...string) (map[string]string,

// Read all env (with same file loading semantics as Load) but return values as
// a map rather than automatically writing values into env
func Read(filenames ...string) (envMap map[string]string, err error) {
func Read(filenames ...string) (map[string]string, error) {
return ReadWithLookup(nil, filenames...)
}

// Unmarshal reads an env file from a string, returning a map of keys and values.
func Unmarshal(str string) (envMap map[string]string, err error) {
func Unmarshal(str string) (map[string]string, error) {
return UnmarshalBytes([]byte(str))
}

Expand Down Expand Up @@ -223,10 +222,10 @@ func loadFile(filename string, overload bool) error {
return nil
}

func readFile(filename string, lookupFn LookupFn) (envMap map[string]string, err error) {
func readFile(filename string, lookupFn LookupFn) (map[string]string, error) {
file, err := os.Open(filename)
if err != nil {
return
return nil, err
}
defer file.Close()

Expand All @@ -235,7 +234,7 @@ func readFile(filename string, lookupFn LookupFn) (envMap map[string]string, err

var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)

func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
func parseLine(line string, envMap map[string]string) (string, string, error) {
return parseLineWithLookup(line, envMap, nil)
}
func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupFn) (string, string, error) {
Expand Down
12 changes: 7 additions & 5 deletions dotenv/parser.go
Expand Up @@ -81,7 +81,9 @@ func getStatementStart(src []byte) []byte {
}

// locateKeyName locates and parses key name and returns rest of slice
func locateKeyName(src []byte) (key string, cutset []byte, inherited bool, err error) {
func locateKeyName(src []byte) (string, []byte, bool, error) {
var key string
var inherited bool
// trim "export" and space at beginning
src = bytes.TrimLeftFunc(bytes.TrimPrefix(src, []byte(exportPrefix)), isSpace)

Expand Down Expand Up @@ -120,7 +122,7 @@ loop:

// trim whitespace
key = strings.TrimRightFunc(key, unicode.IsSpace)
cutset = bytes.TrimLeftFunc(src[offset:], isSpace)
cutset := bytes.TrimLeftFunc(src[offset:], isSpace)
return key, cutset, inherited, nil
}

Expand Down Expand Up @@ -208,14 +210,14 @@ func indexOfNonSpaceChar(src []byte) int {
}

// hasQuotePrefix reports whether charset starts with single or double quote and returns quote character
func hasQuotePrefix(src []byte) (quote byte, isQuoted bool) {
func hasQuotePrefix(src []byte) (byte, bool) {
if len(src) == 0 {
return 0, false
}

switch prefix := src[0]; prefix {
switch quote := src[0]; quote {
case prefixDoubleQuote, prefixSingleQuote:
return prefix, true
return quote, true // isQuoted
default:
return 0, false
}
Expand Down
3 changes: 2 additions & 1 deletion golangci.yml
Expand Up @@ -11,6 +11,7 @@ linters:
- gosimple
- ineffassign
- misspell
- nakedret
- govet
linters-settings:
gocritic:
Expand All @@ -23,4 +24,4 @@ linters-settings:
disabled-checks:
- paramTypeCombine
- unnamedResult
- whyNoLint
- whyNoLint

0 comments on commit 422bb5c

Please sign in to comment.