Skip to content

Commit dd89a90

Browse files
authored
Merge pull request #22
chore: multichecker & build ldflags
2 parents 1b5aa8a + 328a990 commit dd89a90

File tree

11 files changed

+299
-0
lines changed

11 files changed

+299
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ To startup the server, run in the terminal:
2929
./cmd/server/server # in the root directory of the project
3030
```
3131

32+
Build and compilation flags (ldflags):
33+
34+
- buildVersion: any string (example, `-X 'main.buildVersion=v0.0.1'`)
35+
- buildDate: `$(date -u '+%Y/%m/%d %H:%M:%S')'`
36+
- buildCommit: `$(git rev-parse --short HEAD || echo 'unknown')'`
37+
3238
##### Envs & flags
3339

3440
* `ADDRESS` | `-a` – address and port to run server. Default `:8080`
@@ -52,6 +58,12 @@ To startup the agent, run in the terminal:
5258
./cmd/server/agent # in the root directory of the project
5359
```
5460

61+
Build and compilation flags (-ldflags):
62+
63+
- buildVersion: any string (example, `-X 'main.buildVersion=v0.0.1'`)
64+
- buildDate: `$(date -u '+%Y/%m/%d %H:%M:%S')'`
65+
- buildCommit: `$(git rev-parse --short HEAD || echo 'unknown')'`
66+
5567
##### Envs & flags
5668

5769
* `ADDRESS` | `-a` – address and port to run agent. Default `localhost:8080`

cmd/agent/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package main
33

44
import (
5+
"fmt"
56
"log"
67

78
config "github.com/srg-bnd/observator/config/agent"
@@ -10,7 +11,31 @@ import (
1011
"github.com/srg-bnd/observator/internal/storage"
1112
)
1213

14+
var (
15+
buildVersion string
16+
buildDate string
17+
buildCommit string
18+
)
19+
20+
func buildInfo() {
21+
strOrNA := func(str string) string {
22+
if str == "" {
23+
return "N/A"
24+
}
25+
26+
return str
27+
}
28+
29+
fmt.Printf(
30+
"Build version: %s\nBuild date: %s\nBuild commit: %s\n",
31+
strOrNA(buildVersion),
32+
strOrNA(buildDate),
33+
strOrNA(buildCommit),
34+
)
35+
}
36+
1337
func init() {
38+
buildInfo()
1439
config.Flags.ParseFlags()
1540
}
1641

cmd/server/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package main
33

44
import (
5+
"fmt"
56
"log"
67

78
config "github.com/srg-bnd/observator/config/server"
@@ -14,7 +15,31 @@ import (
1415
"github.com/srg-bnd/observator/internal/storage"
1516
)
1617

18+
var (
19+
buildVersion string
20+
buildDate string
21+
buildCommit string
22+
)
23+
24+
func buildInfo() {
25+
strOrNA := func(str string) string {
26+
if str == "" {
27+
return "N/A"
28+
}
29+
30+
return str
31+
}
32+
33+
fmt.Printf(
34+
"Build version: %s\nBuild date: %s\nBuild commit: %s\n",
35+
strOrNA(buildVersion),
36+
strOrNA(buildDate),
37+
strOrNA(buildCommit),
38+
)
39+
}
40+
1741
func init() {
42+
buildInfo()
1843
config.Flags.ParseFlags()
1944
}
2045

cmd/staticlint/.keep

Whitespace-only changes.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package main
2+
3+
import (
4+
"golang.org/x/tools/go/analysis"
5+
"golang.org/x/tools/go/analysis/passes/asmdecl"
6+
"golang.org/x/tools/go/analysis/passes/assign"
7+
"golang.org/x/tools/go/analysis/passes/atomic"
8+
"golang.org/x/tools/go/analysis/passes/atomicalign"
9+
"golang.org/x/tools/go/analysis/passes/bools"
10+
"golang.org/x/tools/go/analysis/passes/buildtag"
11+
"golang.org/x/tools/go/analysis/passes/cgocall"
12+
"golang.org/x/tools/go/analysis/passes/composite"
13+
"golang.org/x/tools/go/analysis/passes/copylock"
14+
"golang.org/x/tools/go/analysis/passes/ctrlflow"
15+
"golang.org/x/tools/go/analysis/passes/deepequalerrors"
16+
"golang.org/x/tools/go/analysis/passes/defers"
17+
"golang.org/x/tools/go/analysis/passes/directive"
18+
"golang.org/x/tools/go/analysis/passes/errorsas"
19+
"golang.org/x/tools/go/analysis/passes/fieldalignment"
20+
"golang.org/x/tools/go/analysis/passes/findcall"
21+
"golang.org/x/tools/go/analysis/passes/httpresponse"
22+
"golang.org/x/tools/go/analysis/passes/ifaceassert"
23+
"golang.org/x/tools/go/analysis/passes/loopclosure"
24+
"golang.org/x/tools/go/analysis/passes/lostcancel"
25+
"golang.org/x/tools/go/analysis/passes/nilfunc"
26+
"golang.org/x/tools/go/analysis/passes/nilness"
27+
"golang.org/x/tools/go/analysis/passes/pkgfact"
28+
"golang.org/x/tools/go/analysis/passes/printf"
29+
"golang.org/x/tools/go/analysis/passes/reflectvaluecompare"
30+
"golang.org/x/tools/go/analysis/passes/shadow"
31+
"golang.org/x/tools/go/analysis/passes/shift"
32+
"golang.org/x/tools/go/analysis/passes/sortslice"
33+
"golang.org/x/tools/go/analysis/passes/stdmethods"
34+
"golang.org/x/tools/go/analysis/passes/stringintconv"
35+
"golang.org/x/tools/go/analysis/passes/structtag"
36+
"golang.org/x/tools/go/analysis/passes/testinggoroutine"
37+
"golang.org/x/tools/go/analysis/passes/tests"
38+
"golang.org/x/tools/go/analysis/passes/unmarshal"
39+
"golang.org/x/tools/go/analysis/passes/unreachable"
40+
"golang.org/x/tools/go/analysis/passes/unsafeptr"
41+
"golang.org/x/tools/go/analysis/passes/unusedresult"
42+
"golang.org/x/tools/go/analysis/passes/unusedwrite"
43+
"golang.org/x/tools/go/analysis/passes/usesgenerics"
44+
"honnef.co/go/tools/staticcheck"
45+
46+
"github.com/kisielk/errcheck/errcheck"
47+
custom "github.com/srg-bnd/observator/cmd/staticlint/multichecker/custom/exitcheckanalyser"
48+
// TODO: Update Go version
49+
// "github.com/securego/gosec/v2/loader"
50+
)
51+
52+
func standardAnalyzers() []*analysis.Analyzer {
53+
return []*analysis.Analyzer{
54+
printf.Analyzer,
55+
shadow.Analyzer,
56+
shift.Analyzer,
57+
structtag.Analyzer,
58+
asmdecl.Analyzer,
59+
assign.Analyzer,
60+
atomic.Analyzer,
61+
atomicalign.Analyzer,
62+
bools.Analyzer,
63+
buildtag.Analyzer,
64+
cgocall.Analyzer,
65+
composite.Analyzer,
66+
copylock.Analyzer,
67+
ctrlflow.Analyzer,
68+
deepequalerrors.Analyzer,
69+
defers.Analyzer,
70+
directive.Analyzer,
71+
errorsas.Analyzer,
72+
fieldalignment.Analyzer,
73+
findcall.Analyzer,
74+
httpresponse.Analyzer,
75+
ifaceassert.Analyzer,
76+
loopclosure.Analyzer,
77+
lostcancel.Analyzer,
78+
nilfunc.Analyzer,
79+
nilness.Analyzer,
80+
pkgfact.Analyzer,
81+
reflectvaluecompare.Analyzer,
82+
sortslice.Analyzer,
83+
stdmethods.Analyzer,
84+
stringintconv.Analyzer,
85+
testinggoroutine.Analyzer,
86+
tests.Analyzer,
87+
unmarshal.Analyzer,
88+
unreachable.Analyzer,
89+
unsafeptr.Analyzer,
90+
unusedresult.Analyzer,
91+
unusedwrite.Analyzer,
92+
usesgenerics.Analyzer,
93+
}
94+
}
95+
96+
func staticcheckAnalyzers(checks []string) []*analysis.Analyzer {
97+
var analyzers []*analysis.Analyzer
98+
99+
checkMap := make(map[string]bool)
100+
for _, check := range checks {
101+
checkMap[check] = true
102+
}
103+
104+
for _, analyzer := range staticcheck.Analyzers {
105+
if analyzer.Analyzer.Name[:2] == "SA" {
106+
analyzers = append(analyzers, analyzer.Analyzer)
107+
}
108+
109+
if checkMap[analyzer.Analyzer.Name] {
110+
analyzers = append(analyzers, analyzer.Analyzer)
111+
}
112+
}
113+
114+
return analyzers
115+
}
116+
117+
func vendorAnalyzers() []*analysis.Analyzer {
118+
var analyzers []*analysis.Analyzer
119+
120+
// TODO: Update Go version
121+
// linters, err := loader.LoadAnalyzers([]string{"--include=G101,G201,G301"})
122+
// if err != nil {
123+
// panic(err)
124+
// }
125+
// Checks the code for vulnerabilities
126+
// analyzers = append(analyzers, linters...)
127+
128+
// Checks that all returned errors are handled
129+
analyzers = append(analyzers, errcheck.Analyzer)
130+
131+
return analyzers
132+
}
133+
134+
func customAnalyzers() []*analysis.Analyzer {
135+
var analyzers []*analysis.Analyzer
136+
137+
analyzers = append(analyzers, custom.NewNoOsExitInMain())
138+
139+
return analyzers
140+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"staticcheck": [
3+
"S1011",
4+
"S1012"
5+
]
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Custom Analyzers
2+
package custom
3+
4+
import (
5+
"golang.org/x/tools/go/analysis"
6+
)
7+
8+
// An analyzer that prohibits the use of a direct call to `os.Exit` in the main function of the main package
9+
func NewNoOsExitInMain() *analysis.Analyzer {
10+
return &analysis.Analyzer{
11+
Name: "noosexitinmain",
12+
Doc: "forbids direct calls to os.Exit in main.main function",
13+
Run: runNoOsExitInMain,
14+
}
15+
}
16+
17+
func runNoOsExitInMain(pass *analysis.Pass) (interface{}, error) {
18+
return nil, nil
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
8+
"golang.org/x/tools/go/analysis"
9+
"golang.org/x/tools/go/analysis/multichecker"
10+
)
11+
12+
// Config is the name of the configuration file
13+
const Config = `config.json`
14+
15+
// ConfigData describes the structure of the configuration file
16+
type ConfigData struct {
17+
Staticheck []string
18+
}
19+
20+
func getConfig() *ConfigData {
21+
appfile, err := os.Executable()
22+
if err != nil {
23+
panic(err)
24+
}
25+
data, err := os.ReadFile(filepath.Join(filepath.Dir(appfile), Config))
26+
if err != nil {
27+
panic(err)
28+
}
29+
var cfg ConfigData
30+
if err = json.Unmarshal(data, &cfg); err != nil {
31+
panic(err)
32+
}
33+
34+
return &cfg
35+
}
36+
37+
func main() {
38+
cfg := getConfig()
39+
var analyzers []*analysis.Analyzer
40+
41+
// Adds standard analyzers
42+
analyzers = append(analyzers, standardAnalyzers()...)
43+
// Adds staticcheck analyzers
44+
analyzers = append(analyzers, staticcheckAnalyzers(cfg.Staticheck)...)
45+
// Adds vendor analyzers
46+
analyzers = append(analyzers, vendorAnalyzers()...)
47+
// Adds custom analyzers
48+
analyzers = append(analyzers, customAnalyzers()...)
49+
50+
// Runs multichecker
51+
multichecker.Main(analyzers...)
52+
}
14.2 MB
Binary file not shown.

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ require (
77
github.com/go-chi/chi v1.5.5
88
github.com/go-resty/resty/v2 v2.16.5
99
github.com/jackc/pgx/v5 v5.7.5
10+
github.com/kisielk/errcheck v1.9.0
1011
github.com/pressly/goose/v3 v3.24.3
1112
github.com/shirou/gopsutil/v4 v4.25.7
1213
github.com/stretchr/testify v1.10.0
1314
go.uber.org/zap v1.27.0
15+
golang.org/x/tools v0.35.0
16+
honnef.co/go/tools v0.6.1
1417
)
1518

1619
require (
@@ -30,9 +33,12 @@ require (
3033
github.com/yusufpapurcu/wmi v1.2.4 // indirect
3134
go.uber.org/multierr v1.11.0 // indirect
3235
golang.org/x/crypto v0.41.0 // indirect
36+
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678 // indirect
37+
golang.org/x/mod v0.26.0 // indirect
3338
golang.org/x/net v0.43.0 // indirect
3439
golang.org/x/sync v0.16.0 // indirect
3540
golang.org/x/sys v0.35.0 // indirect
3641
golang.org/x/text v0.28.0 // indirect
42+
golang.org/x/tools/go/expect v0.1.1-deprecated // indirect
3743
gopkg.in/yaml.v3 v3.0.1 // indirect
3844
)

0 commit comments

Comments
 (0)