From ca8f7fd79593bfa17379bc0e2ac942379aec23ac Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Fri, 15 Jul 2022 22:02:36 +0200 Subject: [PATCH] Add "nakedret" to the linters to put dotenv in conformity with the rest of the project Signed-off-by: Ulysses Souza --- dotenv/godotenv.go | 23 +++++++++++------------ dotenv/parser.go | 12 +++++++----- golangci.yml | 3 ++- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/dotenv/godotenv.go b/dotenv/godotenv.go index 50886f22..543df9b1 100644 --- a/dotenv/godotenv.go +++ b/dotenv/godotenv.go @@ -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...) } @@ -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 @@ -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)) } @@ -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() @@ -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) { diff --git a/dotenv/parser.go b/dotenv/parser.go index ecb94ee9..a0c862b8 100644 --- a/dotenv/parser.go +++ b/dotenv/parser.go @@ -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) @@ -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 } @@ -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 } diff --git a/golangci.yml b/golangci.yml index 77e85835..5c2e225a 100644 --- a/golangci.yml +++ b/golangci.yml @@ -11,6 +11,7 @@ linters: - gosimple - ineffassign - misspell + - nakedret - govet linters-settings: gocritic: @@ -23,4 +24,4 @@ linters-settings: disabled-checks: - paramTypeCombine - unnamedResult - - whyNoLint \ No newline at end of file + - whyNoLint