Merge ~troyanov/maas:go-lint-all into maas:master

Proposed by Anton Troyanov
Status: Merged
Approved by: Anton Troyanov
Approved revision: 5b26cacf2761fc748eb48fa3bd4c844657d6c5d8
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~troyanov/maas:go-lint-all
Merge into: maas:master
Diff against target: 318 lines (+102/-39)
3 files modified
.golangci.yaml (+58/-9)
src/host-info/cmd/machine-resources/main.go (+2/-0)
src/host-info/pkg/info/info.go (+42/-30)
Reviewer Review Type Date Requested Status
Christian Grabowski Approve
MAAS Lander Approve
Review via email: mp+442131@code.launchpad.net

Commit message

ci: improve golangci-lint settings

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b go-lint-all lp:~troyanov/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/2450/console
COMMIT: 0480614a0d149c432ff63a6f66838995efc67383

review: Needs Fixing
Revision history for this message
Anton Troyanov (troyanov) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b go-lint-all lp:~troyanov/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/2451/console
COMMIT: 0480614a0d149c432ff63a6f66838995efc67383

review: Needs Fixing
~troyanov/maas:go-lint-all updated
5b26cac... by Anton Troyanov

fixup! ci: improve golangci-lint settings

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b go-lint-all lp:~troyanov/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 55d1174cf1cd7d9868f13ec4fbcd32a7f7249cda

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b go-lint-all lp:~troyanov/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 5b26cacf2761fc748eb48fa3bd4c844657d6c5d8

review: Approve
Revision history for this message
Christian Grabowski (cgrabowski) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.golangci.yaml b/.golangci.yaml
2index 6855546..b2c6528 100644
3--- a/.golangci.yaml
4+++ b/.golangci.yaml
5@@ -76,13 +76,11 @@ linters:
6 - errcheck
7 - exportloopref
8 - gocritic
9- - godot
10 - gofmt
11 - goimports
12 - gosec
13 - gosimple
14 - govet
15- # - loggercheck
16 - nilerr
17 - nilnil
18 - noctx
19@@ -90,12 +88,50 @@ linters:
20 - nonamedreturns
21 - revive
22 - staticcheck
23+ - stylecheck
24 - tagliatelle
25 - unused
26 - whitespace
27 - wsl
28
29 linters-settings:
30+ errcheck:
31+ check-type-assertions: true
32+ check-blank: true
33+ gosimple:
34+ checks: ["all"]
35+ govet:
36+ enable-all: true
37+ nolintlint:
38+ require-explanation: true
39+ require-specific: true
40+ revive:
41+ ignore-generated-header: true
42+ rules:
43+ - name: time-equal
44+ severity: warning
45+ disabled: false
46+ - name: errorf
47+ severity: warning
48+ disabled: false
49+ - name: context-as-argument
50+ severity: warning
51+ disabled: false
52+ - name: error-return
53+ severity: warning
54+ disabled: false
55+ - name: datarace
56+ severity: warning
57+ disabled: false
58+ staticcheck:
59+ checks: ["all"]
60+ stylecheck:
61+ checks: ["all"]
62+ tagliatelle:
63+ case:
64+ rules:
65+ json: snake
66+ yaml: snake
67
68
69 issues:
70@@ -127,6 +163,25 @@ issues:
71 - lll
72 source: "^//go:generate "
73
74+ - path: internal/
75+ text: "ST1000:"
76+ linters:
77+ - stylecheck
78+
79+ - path: internal/
80+ text: "package-comments:"
81+ linters:
82+ - revive
83+
84+ - path: internal/
85+ text: "exported:"
86+ linters:
87+ - revive
88+
89+ - path: main.go
90+ text: "package-comments:"
91+ linters:
92+ - revive
93 # Independently of option `exclude` we use default exclude patterns,
94 # it can be disabled by this option.
95 # To list all excluded by default patterns execute `golangci-lint run --help`.
96@@ -148,14 +203,8 @@ issues:
97 # Default: false.
98 new: false
99
100- # Show only new issues created after git revision `REV`.
101- new-from-rev: HEAD
102-
103- # Show only new issues created in git patch with set file path.
104- # new-from-patch: path/to/patch/file
105-
106 # Fix found issues (if it's supported by the linter).
107- fix: true
108+ fix: false
109
110
111 severity:
112diff --git a/src/host-info/cmd/machine-resources/main.go b/src/host-info/cmd/machine-resources/main.go
113index 2d4ebef..75b64b3 100644
114--- a/src/host-info/cmd/machine-resources/main.go
115+++ b/src/host-info/cmd/machine-resources/main.go
116@@ -15,6 +15,7 @@ func checkError(err error) {
117 if err == nil {
118 return
119 }
120+
121 fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
122 os.Exit(1)
123 }
124@@ -22,6 +23,7 @@ func checkError(err error) {
125 func main() {
126 data, err := info.GetInfo()
127 checkError(err)
128+
129 encoder := json.NewEncoder(os.Stdout)
130 encoder.SetEscapeHTML(false)
131 encoder.SetIndent("", " ")
132diff --git a/src/host-info/pkg/info/info.go b/src/host-info/pkg/info/info.go
133index 29cf9b3..b31470c 100644
134--- a/src/host-info/pkg/info/info.go
135+++ b/src/host-info/pkg/info/info.go
136@@ -1,12 +1,14 @@
137 // Copyright 2022 Canonical Ltd. This software is licensed under the
138 // GNU Affero General Public License version 3 (see the file LICENSE).
139
140+//nolint:stylecheck // ignore ST1000
141 package info
142
143 import (
144 "bufio"
145 "net"
146 "os"
147+ "path/filepath"
148 "strings"
149
150 "github.com/lxc/lxd/lxd/resources"
151@@ -15,12 +17,13 @@ import (
152 "github.com/lxc/lxd/shared/version"
153 )
154
155+// OSInfo represents OS information
156 type OSInfo struct {
157 OSName string `json:"os_name" yaml:"os_name"`
158 OSVersion string `json:"os_version" yaml:"os_version"`
159 }
160
161-// Subset of github.com/lxc/lxd/shared/api/server.ServerEnvironment
162+// ServerEnvironment is a subset of github.com/lxc/lxd/shared/api/server.ServerEnvironment
163 type ServerEnvironment struct {
164 Kernel string `json:"kernel" yaml:"kernel"`
165 KernelArchitecture string `json:"kernel_architecture" yaml:"kernel_architecture"`
166@@ -31,26 +34,32 @@ type ServerEnvironment struct {
167 ServerVersion string `json:"server_version" yaml:"server_version"`
168 }
169
170-// Subset of github.com/lxc/lxd/shared/api/server.HostInfo
171+// HostInfo is a subset of github.com/lxc/lxd/shared/api/server.HostInfo
172 type HostInfo struct {
173- APIExtensions []string `json:"api_extensions" yaml:"api_extensions"`
174- APIVersion string `json:"api_version" yaml:"api_version"`
175 Environment ServerEnvironment `json:"environment" yaml:"environment"`
176+ APIVersion string `json:"api_version" yaml:"api_version"`
177+ APIExtensions []string `json:"api_extensions" yaml:"api_extensions"`
178 }
179
180 type AllInfo struct {
181- HostInfo
182 Resources *lxdapi.Resources `json:"resources" yaml:"resources"`
183 Networks map[string]interface{} `json:"networks" yaml:"networks"`
184+ HostInfo
185 }
186
187 func parseKeyValueFile(path string) (map[string]string, error) {
188- parsed_file := make(map[string]string)
189- file, err := os.Open(path)
190+ parsedFile := make(map[string]string)
191+
192+ file, err := os.Open(filepath.Clean(path))
193 if err != nil {
194- return parsed_file, err
195+ return parsedFile, err
196 }
197- defer file.Close()
198+
199+ defer func() {
200+ if err := file.Close(); err != nil {
201+ panic(err)
202+ }
203+ }()
204
205 scanner := bufio.NewScanner(file)
206 for scanner.Scan() {
207@@ -63,27 +72,28 @@ func parseKeyValueFile(path string) (map[string]string, error) {
208 if len(tokens) != 2 {
209 continue
210 }
211- parsed_file[strings.Trim(tokens[0], `'"`)] = strings.Trim(tokens[1], `'"`)
212+
213+ parsedFile[strings.Trim(tokens[0], `'"`)] = strings.Trim(tokens[1], `'"`)
214 }
215
216- return parsed_file, nil
217+ return parsedFile, nil
218 }
219
220 func getOSNameVersion() OSInfo {
221 // Search both pathes as suggested by
222 // https://www.freedesktop.org/software/systemd/man/os-release.html
223 for _, path := range []string{"/etc/os-release", "/usr/lib/os-release"} {
224- parsed_file, err := parseKeyValueFile(path)
225+ parsedFile, err := parseKeyValueFile(path)
226 // LP:1876217 - As of 2.44 snapd only gives confined Snaps
227 // access to /etc/lsb-release from the host OS. /etc/os-release
228 // currently contains the Ubuntu Core version the Snap is
229 // running. Try /etc/os-release first for controllers installed
230 // with the Debian packages. At some point in the future snapd
231 // may provide the host OS version of /etc/os-release.
232- if err == nil && parsed_file["ID"] != "ubuntu-core" {
233+ if err == nil && parsedFile["ID"] != "ubuntu-core" {
234 return OSInfo{
235- OSName: strings.ToLower(parsed_file["ID"]),
236- OSVersion: strings.ToLower(parsed_file["VERSION_ID"]),
237+ OSName: strings.ToLower(parsedFile["ID"]),
238+ OSVersion: strings.ToLower(parsedFile["VERSION_ID"]),
239 }
240 }
241 }
242@@ -91,20 +101,18 @@ func getOSNameVersion() OSInfo {
243 // version try to get OS information from /etc/lsb-release as this
244 // file is passed from the running host OS. Only Ubuntu and Manjaro
245 // provide /etc/lsb-release.
246- parsed_file, err := parseKeyValueFile("/etc/lsb-release")
247- if err == nil {
248- return OSInfo{
249- OSName: strings.ToLower(parsed_file["DISTRIB_ID"]),
250- OSVersion: strings.ToLower(parsed_file["DISTRIB_RELEASE"]),
251- }
252- } else {
253- // If the OS information isn't detectable don't send anything.
254- // MAAS will keep the current OS information.
255+ parsedFile, err := parseKeyValueFile("/etc/lsb-release")
256+ if err != nil {
257 return OSInfo{}
258 }
259+
260+ return OSInfo{
261+ OSName: strings.ToLower(parsedFile["DISTRIB_ID"]),
262+ OSVersion: strings.ToLower(parsedFile["DISTRIB_RELEASE"]),
263+ }
264 }
265
266-func GetHostInfo() (*HostInfo, error) {
267+func getHostInfo() (*HostInfo, error) {
268 hostname, err := os.Hostname()
269 if err != nil {
270 return nil, err
271@@ -153,38 +161,42 @@ func GetHostInfo() (*HostInfo, error) {
272 }, nil
273 }
274
275-func GetResources() (*lxdapi.Resources, error) {
276+func getResources() (*lxdapi.Resources, error) {
277 return resources.GetResources()
278 }
279
280-func GetNetworks() (map[string]interface{}, error) {
281+func getNetworks() (map[string]interface{}, error) {
282 ifaces, err := net.Interfaces()
283 if err != nil {
284 return nil, err
285 }
286+
287 data := make(map[string]interface{}, len(ifaces))
288+
289 for _, iface := range ifaces {
290 netDetails, err := resources.GetNetworkState(iface.Name)
291 if err != nil {
292 return nil, err
293 }
294+
295 data[iface.Name] = netDetails
296 }
297+
298 return data, nil
299 }
300
301 func GetInfo() (*AllInfo, error) {
302- hostInfo, err := GetHostInfo()
303+ hostInfo, err := getHostInfo()
304 if err != nil {
305 return nil, err
306 }
307
308- resInfo, err := GetResources()
309+ resInfo, err := getResources()
310 if err != nil {
311 return nil, err
312 }
313
314- netInfo, err := GetNetworks()
315+ netInfo, err := getNetworks()
316 if err != nil {
317 return nil, err
318 }

Subscribers

People subscribed via source and target branches