Skip to content

Commit

Permalink
tune linters
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Feb 9, 2024
1 parent 6a8f72d commit be81b95
Show file tree
Hide file tree
Showing 26 changed files with 182 additions and 153 deletions.
23 changes: 23 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ linters:

# Whitespace Linter - Forces you to use empty lines!
- wsl
- whitespace

# A linter that detect the possibility to use variables/constants from the Go standard library.
- usestdlibvars
Expand All @@ -135,6 +136,17 @@ linters:
# Align and sort struct tags consistently
- tagalign

# Reports wrong mirror patterns of bytes/strings usage.
- mirror

- dupl
- inamedparam
- perfsprint
- testifylint
- usestdlibvars
- varnamelen
# - gochecknoglobals

issues:
exclude-rules:
# ignoring this check from 'unused' since setID is indeed used, but
Expand Down Expand Up @@ -204,13 +216,24 @@ linters-settings:
excludes: []

exhaustive:
# Program elements to check for exhaustiveness.
check:
- switch
- map
# Presence of "default" case in switch statements satisfies exhaustiveness, even if all enum members are not listed.
# Default: false
default-signifies-exhaustive: true

unparam:
check-exported: true

varnamelen:
check-return: true
check-type-param: true
ignore-names:
- tt
- err

tagalign:
# Align and sort can be used together or separately.
#
Expand Down
3 changes: 2 additions & 1 deletion cmd/disable/disable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package disable

import (
"errors"
"fmt"

"github.com/jippi/dottie/pkg"
Expand All @@ -15,7 +16,7 @@ var Command = &cobra.Command{
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.FilterDisabledStatements).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("Missing required argument: KEY")
return errors.New("Missing required argument: KEY")
}
key := args[0]

Expand Down
3 changes: 2 additions & 1 deletion cmd/enable/enable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package enable

import (
"errors"
"fmt"

"github.com/jippi/dottie/pkg"
Expand All @@ -15,7 +16,7 @@ var Command = &cobra.Command{
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.FilterActiveStatements).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("Missing required argument: KEY")
return errors.New("Missing required argument: KEY")
}

env, _, err := shared.Setup(cmd.Flags())
Expand Down
3 changes: 2 additions & 1 deletion cmd/groups/groups.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package groups

import (
"errors"
"fmt"

"github.com/gosimple/slug"
Expand All @@ -19,7 +20,7 @@ var Command = &cobra.Command{

groups := env.Groups
if len(groups) == 0 {
return fmt.Errorf("No groups found")
return errors.New("No groups found")
}

fmt.Println("The following groups was found:")
Expand Down
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ func indent(in string) string {
func buildVersion() goversion.Info {
return goversion.GetVersionInfo(
// goversion.WithAppDetails("dottie", "Making .env file management easy", "https://github.com/jippi/dottie"),
func(i *goversion.Info) {
func(versionInfo *goversion.Info) {
if commit != "" {
i.GitCommit = commit
versionInfo.GitCommit = commit
}

if treeState != "" {
i.GitTreeState = treeState
versionInfo.GitTreeState = treeState
}

if date != "" {
i.BuildDate = date
versionInfo.BuildDate = date
}

if version != "" {
i.GitVersion = version
versionInfo.GitVersion = version
}
},
)
Expand Down
5 changes: 3 additions & 2 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package set

import (
"errors"
"fmt"
"strings"

Expand All @@ -25,7 +26,7 @@ func Command() *cobra.Command {
}

if len(args) == 0 {
return fmt.Errorf("Missing required argument: KEY=VALUE")
return errors.New("Missing required argument: KEY=VALUE")
}

comments, _ := cmd.Flags().GetStringArray("comment")
Expand All @@ -40,7 +41,7 @@ func Command() *cobra.Command {
for _, stringPair := range args {
pairSlice := strings.SplitN(stringPair, "=", 2)
if len(pairSlice) != 2 {
return fmt.Errorf("expected KEY=VALUE pair, missing '='")
return errors.New("expected KEY=VALUE pair, missing '='")
}

key := pairSlice[0]
Expand Down
3 changes: 2 additions & 1 deletion cmd/value/value.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package value

import (
"errors"
"fmt"

"github.com/jippi/dottie/pkg/cli/shared"
Expand All @@ -14,7 +15,7 @@ var Command = &cobra.Command{
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.FilterDisabledStatements).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("Missing required argument: KEY")
return errors.New("Missing required argument: KEY")
}

env, _, err := shared.Setup(cmd.Flags())
Expand Down
4 changes: 2 additions & 2 deletions pkg/ast/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ast

import (
"bytes"
"fmt"
"errors"
"reflect"
"strings"

Expand Down Expand Up @@ -85,7 +85,7 @@ func (a *Assignment) ValidationRules() string {

func (a *Assignment) IsValid() error {
if !a.Quote.Valid() {
return fmt.Errorf("invalid quote-style")
return errors.New("invalid quote-style")
}

return validator.
Expand Down
15 changes: 8 additions & 7 deletions pkg/ast/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package ast

import (
"errors"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -81,17 +82,17 @@ func (d *Document) Get(name string) *Assignment {

func (doc *Document) Interpolate(target *Assignment) (string, error) {
if target == nil {
return "", fmt.Errorf("can't interpolate a nil assignment")
return "", errors.New("can't interpolate a nil assignment")
}

lookup := func(in string) (string, bool) {
lookup := func(input string) (string, bool) {
// Lookup in process environment
if val, ok := os.LookupEnv(in); ok {
if val, ok := os.LookupEnv(input); ok {
return val, ok
}

// Search the currently available assignments in the document
result := doc.Get(in)
result := doc.Get(input)
if result == nil {
return "", false
}
Expand Down Expand Up @@ -159,14 +160,14 @@ func (doc *Document) Upsert(input *Assignment, options UpsertOptions) (*Assignme
var res []Statement

for _, stmt := range group.Statements {
x, ok := stmt.(*Assignment)
assignment, ok := stmt.(*Assignment)
if !ok {
res = append(res, stmt)

continue
}

if x.Name == before {
if assignment.Name == before {
res = append(res, existing)
}

Expand Down Expand Up @@ -194,7 +195,7 @@ func (doc *Document) Upsert(input *Assignment, options UpsertOptions) (*Assignme
if found {
interpolated, err := doc.Interpolate(existing)
if err != nil {
return nil, fmt.Errorf("could not interpolate variable")
return nil, errors.New("could not interpolate variable")
}

existing.Interpolated = interpolated
Expand Down
2 changes: 1 addition & 1 deletion pkg/ast/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// Statement represents syntax tree node of .env file statement (like: assignment or comment).
type Statement interface {
statementNode()
Is(Statement) bool
Is(statement Statement) bool
Type() string
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pkg

import (
"fmt"
"errors"
"io"
"os"

Expand All @@ -12,28 +12,28 @@ import (
)

func Load(filename string) (doc *ast.Document, err error) {
r, err := os.Open(filename)
file, err := os.Open(filename)
if err != nil {
return
}
defer r.Close()
defer file.Close()

return Parse(r, filename)
return Parse(file, filename)
}

func Save(filename string, doc *ast.Document) error {
f, err := os.Create(filename)
file, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
defer file.Close()

res := render.NewFormatter().Statement(doc)
if res.IsEmpty() {
return fmt.Errorf("The rendered .env file is unexpectedly 0 bytes long - please report this as a bug (unless your file is empty)")
return errors.New("The rendered .env file is unexpectedly 0 bytes long - please report this as a bug (unless your file is empty)")
}

_, err = f.WriteString(res.String())
_, err = file.WriteString(res.String())

return err
}
Expand Down
Loading

0 comments on commit be81b95

Please sign in to comment.