Skip to content

Commit

Permalink
Upgrade linter (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkyr authored Feb 3, 2023
1 parent 95c6c10 commit 461f223
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 76 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/go.yml

This file was deleted.

19 changes: 0 additions & 19 deletions .github/workflows/golangci-lint.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Build, lint and test

on:
push:
tags:
- v*
branches:
- master
pull_request:

env:
GO_VERSION: "1.20"

jobs:
lint:
runs-on: ubuntu-20.04
steps:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout repository
uses: actions/checkout@v3
- name: Run linter
run: make lint
- name: Run tests
run: make test
18 changes: 17 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ linters:
- goerr113
- exhaustive
- funlen
- varnamelen
- gci
- wrapcheck
- exhaustruct
- exhaustivestruct
- cyclop
- gofumpt
- typecheck
- ireturn
- nlreturn
- errname
- nonamedreturns

issues:
exclude-rules:
Expand All @@ -22,6 +34,10 @@ issues:
- paralleltest
- errorlint
- forcetypeassert
- cyclop
- maintidx
- path: doc\.go
linters:
- lll
- lll
- gofmt
- goimports
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
GOLANGCILINT=$(GOPATH)/bin/golangci-lint
$(GOLANGCILINT):
curl -fsSL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.29.0

.PHONY: test
test:
go test -v ./...

.PHONY: lint
lint: $(GOLANGCILINT)
golangci-lint run ./...
lint:
./build/lint.sh ./...
21 changes: 21 additions & 0 deletions build/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -o pipefail

export CGO_ENABLED=1

if ! command -v golangci-lint &> /dev/null; then
echo "Installing golangci-lint"
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.51.0
fi

echo -n "Running golangci-lint: "
ERRS=$(golangci-lint run "$@" 2>&1)
if [ $? -eq 1 ]; then
echo "FAIL"
echo "${ERRS}"
echo
exit 1
fi
echo "PASS"
echo
2 changes: 1 addition & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type field struct {
}

// name is the name of the field. if the field contains an alt name
// in the struct struct that name is used, else it falls back to
// in the struct that name is used, else it falls back to
// the field's name as defined in the struct.
// if this field is a slice field, then its name is simply its
// index in the slice.
Expand Down
1 change: 1 addition & 0 deletions fig.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func stringToRegexpHookFunc() mapstructure.DecodeHookFunc {
if t != reflect.TypeOf(&regexp.Regexp{}) {
return data, nil
}
//nolint:forcetypeassert
return regexp.Compile(data.(string))
}
}
Expand Down
4 changes: 1 addition & 3 deletions fig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,5 @@ func Test_fig_setSlice(t *testing.T) {

func setenv(t *testing.T, key, value string) {
t.Helper()
if err := os.Setenv(key, value); err != nil {
t.Fatalf("os.Setenv() unexpected error: %v", err)
}
t.Setenv(key, value)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kkyr/fig

go 1.16
go 1.20

require (
github.com/mitchellh/mapstructure v1.4.1
Expand Down
33 changes: 16 additions & 17 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Option func(f *fig)
// The name must include the extension of the file. Supported
// file types are `yaml`, `yml`, `json` and `toml`.
//
// fig.Load(&cfg, fig.File("config.toml"))
// fig.Load(&cfg, fig.File("config.toml"))
//
// If this option is not used then fig looks for a file with name `config.yaml`.
func File(name string) Option {
Expand All @@ -24,7 +24,7 @@ func File(name string) Option {
// is most useful in conjunction with the `UseEnv` option when you want to provide
// config values only via environment variables.
//
// fig.Load(&cfg, fig.IgnoreFile(), fig.UseEnv("my_app"))
// fig.Load(&cfg, fig.IgnoreFile(), fig.UseEnv("my_app"))
func IgnoreFile() Option {
return func(f *fig) {
f.ignoreFile = true
Expand All @@ -38,8 +38,7 @@ func IgnoreFile() Option {
//
// This is useful when you don't know where exactly your configuration will be during run-time:
//
// fig.Load(&cfg, fig.Dirs(".", "/etc/myapp", "/home/user/myapp"))
//
// fig.Load(&cfg, fig.Dirs(".", "/etc/myapp", "/home/user/myapp"))
//
// If this option is not used then fig looks in the directory it is run from.
func Dirs(dirs ...string) Option {
Expand All @@ -51,7 +50,7 @@ func Dirs(dirs ...string) Option {
// Tag returns an option that configures the tag key that fig uses
// when for the alt name struct tag key in fields.
//
// fig.Load(&cfg, fig.Tag("config"))
// fig.Load(&cfg, fig.Tag("config"))
//
// If this option is not used then fig uses the tag `fig`.
func Tag(tag string) Option {
Expand All @@ -63,7 +62,7 @@ func Tag(tag string) Option {
// TimeLayout returns an option that conmfigures the time layout that fig uses when
// parsing a time in a config file or in the default tag for time.Time fields.
//
// fig.Load(&cfg, fig.TimeLayout("2006-01-02"))
// fig.Load(&cfg, fig.TimeLayout("2006-01-02"))
//
// If this option is not used then fig parses times using `time.RFC3339` layout.
func TimeLayout(layout string) Option {
Expand All @@ -75,7 +74,7 @@ func TimeLayout(layout string) Option {
// UseEnv returns an option that configures fig to additionally load values
// from the environment.
//
// fig.Load(&cfg, fig.UseEnv("my_app"))
// fig.Load(&cfg, fig.UseEnv("my_app"))
//
// Values loaded from the environment overwrite values loaded by the config file (if any).
//
Expand All @@ -86,20 +85,20 @@ func TimeLayout(layout string) Option {
// an alternative name defined inside a struct tag then that name is
// preferred.
//
// type Config struct {
// Build time.Time
// LogLevel string `fig:"log_level"`
// Server struct {
// Host string
// }
// }
// type Config struct {
// Build time.Time
// LogLevel string `fig:"log_level"`
// Server struct {
// Host string
// }
// }
//
// With the struct above and UseEnv("myapp") fig would search for the following
// environment variables:
//
// MYAPP_BUILD
// MYAPP_LOG_LEVEL
// MYAPP_SERVER_HOST
// MYAPP_BUILD
// MYAPP_LOG_LEVEL
// MYAPP_SERVER_HOST
func UseEnv(prefix string) Option {
return func(f *fig) {
f.useEnv = true
Expand Down
5 changes: 5 additions & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/kkyr/fig/tools

go 1.20

require github.com/golangci/golangci-lint v1.51.0 // indirect
2 changes: 2 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/golangci/golangci-lint v1.51.0 h1:M1bpDymgdaPKNzPwQdebCGki/nzvVkr2f/eUfk9C9oU=
github.com/golangci/golangci-lint v1.51.0/go.mod h1:7taIMcmZ5ksCuRruCV/mm8Ir3sE+bIuOHVCvt1B4hi4=
10 changes: 10 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build never
// +build never

// Package tools is used to track binary dependencies with go modules
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
package tools

import (
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
)
6 changes: 3 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

// stringSlice converts a Go slice represented as a string
// into an an actual slice. The enclosing square brackets
// into an actual slice. The enclosing square brackets
// are not necessary.
// fields should be separated by a comma.
//
// "[1,2,3]" ---> []string{"1", "2", "3"}
// " foo , bar" ---> []string{" foo ", " bar"}
// "[1,2,3]" ---> []string{"1", "2", "3"}
// " foo , bar" ---> []string{" foo ", " bar"}
func stringSlice(s string) []string {
s = strings.TrimSuffix(strings.TrimPrefix(s, "["), "]")
return strings.Split(s, ",")
Expand Down

0 comments on commit 461f223

Please sign in to comment.