Skip to content

Commit b287391

Browse files
committed
Add mage
1 parent 9435f0d commit b287391

File tree

6 files changed

+178
-1
lines changed

6 files changed

+178
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin

.golangci.yml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# options for analysis running
2+
run:
3+
deadline: 20s
4+
tests: true
5+
skip-dirs:
6+
- generated
7+
skip-files:
8+
- ".*\\.pb\\.go"
9+
silent: true
10+
11+
# output configuration options
12+
output:
13+
format: colored-line-number
14+
print-issued-lines: true
15+
print-linter-name: true
16+
17+
# all available settings of specific linters
18+
linters-settings:
19+
errcheck:
20+
check-type-assertions: true
21+
check-blank: true
22+
govet:
23+
check-shadowing: false
24+
disable:
25+
- shadow
26+
gofmt:
27+
simplify: true
28+
goconst:
29+
min-len: 3
30+
min-occurrences: 3
31+
misspell:
32+
locale: UK
33+
ignore-words:
34+
- statuser
35+
unused:
36+
check-exported: true
37+
unparam:
38+
algo: cha
39+
nakedret:
40+
max-func-lines: 30
41+
prealloc:
42+
simple: true
43+
range-loops: true
44+
for-loops: true
45+
46+
linters:
47+
enable:
48+
- golint # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true]
49+
- misspell # Finds commonly misspelled English words in comments [fast: true]
50+
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true]
51+
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false]
52+
- gas # Inspects source code for security problems [fast: false]
53+
- structcheck # Finds an unused struct fields [fast: false]
54+
- unconvert # Remove unnecessary type conversions [fast: false]
55+
- unparam # Reports unused function parameters [fast: false]
56+
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true]
57+
- lll # Reports long lines [fast: true]
58+
- megacheck # 3 sub-linters in one: unused, gosimple and staticcheck [fast: false]
59+
60+
disable:
61+
- staticcheck # (megacheck) Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false]
62+
- prealloc # Finds slice declarations that could potentially be preallocated [fast: true]
63+
- unused # (megacheck) Checks Go code for unused constants, variables, functions and types [fast: false]
64+
- gosimple # (megacheck) Linter for Go source code that specializes in simplifying a code [fast: false]
65+
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true]
66+
- dupl # Tool for code clone detection [fast: true]
67+
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: false]
68+
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: false]
69+
- varcheck # Finds unused global variables and constants [fast: false]
70+
- deadcode # Finds unused code [fast: false]
71+
- goconst # Finds repeated strings that could be replaced by a constant [fast: true]
72+
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true]
73+
- maligned # Tool to detect Go structs that would take less memory if their fields were sorted [fast: false]
74+
75+
issues:
76+
exclude-use-default: false
77+
exclude-rules:
78+
- linters:
79+
- lll
80+
source: "github.com/utilitywarehouse/ordering-platform"
81+
exclude:
82+
# errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
83+
- Error return value of
84+
.((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv|.*Disconnect).
85+
is not checked
86+
87+
# golint: Exported variables are rarely used and generally reserved for errors which should be self explanitory
88+
- exported var \w+ should have comment or be unexported
89+
90+
# golint: False positive when tests are defined in package 'test'
91+
- func name will be used as test\.Test.* by other packages, and that
92+
stutters; consider calling this
93+
94+
# gas: Too many false-positives on 'unsafe' usage
95+
- Use of unsafe calls should be audited
96+
97+
# gas: Too many false-positives for parametrized shell calls
98+
- Subprocess launch(ed with variable|ing should be audited)
99+
100+
# gas: Duplicated errcheck checks
101+
- G104
102+
103+
# gas: Too many issues in popular repos
104+
- (Expect directory permissions to be 0750 or less|Expect file permissions
105+
to be 0600 or less)
106+
107+
# gas: False positive is triggered by 'src, err := ioutil.ReadFile(filename)'
108+
- Potential file inclusion via variable
109+
110+
# govet: Common false positives
111+
- (possible misuse of unsafe.Pointer|should have signature)
112+
113+
# megacheck: Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore
114+
- ineffective break statement. Did you mean to break out of the outer loop
115+
116+
# disable comments
117+
- exported [a-z]+ `?[^ ]+ should have comment

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/go-git/go-git/v5 v5.4.2
7+
github.com/magefile/mage v1.11.0
78
github.com/stretchr/testify v1.7.0
89
golang.org/x/mod v0.5.1
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4444
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4545
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4646
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
47+
github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls=
48+
github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
4749
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
4850
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
4951
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=

magefile.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:build mage
2+
// +build mage
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/magefile/mage/sh"
12+
)
13+
14+
// Lint runs code linters on current directory and all
15+
// subdirectories
16+
func Lint() error {
17+
linter := sh.OutCmd(filepath.Join(RepoRoot(), "bin", "golangci-lint"))
18+
version := "1.39.0"
19+
20+
currentVersion, err := linter("--version")
21+
if err != nil || !strings.Contains(currentVersion, version) {
22+
fmt.Println("linter binary outdated or missing, downloading a new one now")
23+
err := updateLinter(version)
24+
if err != nil {
25+
return err
26+
}
27+
}
28+
29+
out, err := linter("run", "--deadline=2m", "--config="+RepoRoot()+"/.golangci.yml", "./...")
30+
if out != "" {
31+
fmt.Println(out)
32+
}
33+
return err
34+
}
35+
36+
// Test tests all go code in the current directory and
37+
// all subdirectories
38+
func Test() error {
39+
return sh.RunV("go", "test", "./...")
40+
}
41+
42+
func updateLinter(version string) error {
43+
return sh.Run("bash", "-c",
44+
"curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "+
45+
RepoRoot()+"/bin v"+version,
46+
)
47+
}
48+
49+
func RepoRoot() string {
50+
path, err := sh.Output("git", "rev-parse", "--show-toplevel")
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
return path
56+
}

patrol/patrol_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func copy(source, destination string) error {
109109
if err1 != nil {
110110
return err1
111111
}
112-
return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0777)
112+
return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0600)
113113
}
114114

115115
return nil

0 commit comments

Comments
 (0)