Skip to content

Commit 446c54a

Browse files
authored
Merge pull request #466 from kolyshkin/modernize
Modernize sources, add golangci-lint
2 parents 66963a1 + 60c4c55 commit 446c54a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+421
-537
lines changed

.github/workflows/go.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@ jobs:
2424
uses: actions/setup-go@v5
2525
with:
2626
go-version: ${{ matrix.go }}
27-
- name: Go fmt
28-
run: ./scripts/ci-runner.sh go_fmt
2927
- name: Go build (source)
3028
run: ./scripts/ci-runner.sh build_source
3129
- name: Go build (tests)
3230
run: ./scripts/ci-runner.sh build_tests
33-
- name: Go vet
34-
run: ./scripts/ci-runner.sh go_vet
31+
lint:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Install dependencies
35+
run: |
36+
sudo apt-get -qq update
37+
sudo apt-get -qq install libsystemd-dev
38+
- uses: actions/checkout@v5
39+
- uses: actions/setup-go@v5
40+
with:
41+
go-version: stable
42+
- uses: golangci/golangci-lint-action@v8
43+
with:
44+
version: v2.3
3545

3646
all-done:
3747
needs:

.golangci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- gofumpt
6+
7+
linters:
8+
settings:
9+
staticcheck:
10+
checks:
11+
- all
12+
- -ST1003 # https://staticcheck.dev/docs/checks/#ST1003 Should not use underscores in Go names.
13+
- -ST1005 # https://staticcheck.dev/docs/checks/#ST1005 Error strings should not be capitalized.
14+
govet:
15+
enable:
16+
- nilness
17+
exclusions:
18+
presets:
19+
- std-error-handling

activation/files_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ import (
2424

2525
// correctStringWritten fails the text if the correct string wasn't written
2626
// to the other side of the pipe.
27-
func correctStringWritten(t *testing.T, r *os.File, expected string) bool {
27+
func correctStringWritten(t *testing.T, r io.Reader, expected string) {
28+
t.Helper()
29+
2830
bytes := make([]byte, len(expected))
29-
io.ReadAtLeast(r, bytes, len(expected))
31+
_, err := io.ReadAtLeast(r, bytes, len(expected))
32+
if err != nil {
33+
t.Fatal(err)
34+
}
3035

3136
if string(bytes) != expected {
3237
t.Fatalf("Unexpected string %s", string(bytes))
3338
}
34-
35-
return true
3639
}
3740

3841
// TestActivation forks out a copy of activation.go example and reads back two
@@ -53,7 +56,7 @@ func TestActivation(t *testing.T) {
5356

5457
err := cmd.Run()
5558
if err != nil {
56-
t.Fatalf(err.Error())
59+
t.Fatal(err)
5760
}
5861

5962
correctStringWritten(t, r1, "Hello world: fd1")
@@ -68,7 +71,7 @@ func TestActivationNoFix(t *testing.T) {
6871

6972
out, _ := cmd.CombinedOutput()
7073
if !bytes.Contains(out, []byte("No files")) {
71-
t.Fatalf("Child didn't error out as expected")
74+
t.Fatal("Child didn't error out as expected")
7275
}
7376
}
7477

@@ -80,6 +83,6 @@ func TestActivationNoFiles(t *testing.T) {
8083

8184
out, _ := cmd.CombinedOutput()
8285
if !bytes.Contains(out, []byte("No files")) {
83-
t.Fatalf("Child didn't error out as expected")
86+
t.Fatal("Child didn't error out as expected")
8487
}
8588
}

activation/files_unix.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
//go:build !windows
16-
// +build !windows
1716

1817
// Package activation implements primitives for systemd socket activation.
1918
package activation
@@ -38,9 +37,12 @@ const (
3837
// fd usage and to avoid leaking environment flags to child processes.
3938
func Files(unsetEnv bool) []*os.File {
4039
if unsetEnv {
41-
defer os.Unsetenv("LISTEN_PID")
42-
defer os.Unsetenv("LISTEN_FDS")
43-
defer os.Unsetenv("LISTEN_FDNAMES")
40+
defer func() {
41+
// Unsetenv implementation for unix never returns an error.
42+
_ = os.Unsetenv("LISTEN_PID")
43+
_ = os.Unsetenv("LISTEN_FDS")
44+
_ = os.Unsetenv("LISTEN_FDNAMES")
45+
}()
4446
}
4547

4648
pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))

activation/listeners_test.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,12 @@
1515
package activation
1616

1717
import (
18-
"io"
1918
"net"
2019
"os"
2120
"os/exec"
2221
"testing"
2322
)
2423

25-
// correctStringWritten fails the text if the correct string wasn't written
26-
// to the other side of the pipe.
27-
func correctStringWrittenNet(t *testing.T, r net.Conn, expected string) bool {
28-
bytes := make([]byte, len(expected))
29-
io.ReadAtLeast(r, bytes, len(expected))
30-
31-
if string(bytes) != expected {
32-
t.Fatalf("Unexpected string %s", string(bytes))
33-
}
34-
35-
return true
36-
}
37-
3824
// TestActivation forks out a copy of activation.go example and reads back two
3925
// strings from the pipes that are passed in.
4026
func TestListeners(t *testing.T) {
@@ -43,11 +29,11 @@ func TestListeners(t *testing.T) {
4329

4430
l1, err := net.Listen("tcp", ":9999")
4531
if err != nil {
46-
t.Fatalf(err.Error())
32+
t.Fatal(err)
4733
}
4834
l2, err := net.Listen("tcp", ":1234")
4935
if err != nil {
50-
t.Fatalf(err.Error())
36+
t.Fatal(err)
5137
}
5238

5339
t1 := l1.(*net.TCPListener)
@@ -63,25 +49,28 @@ func TestListeners(t *testing.T) {
6349

6450
r1, err := net.Dial("tcp", "127.0.0.1:9999")
6551
if err != nil {
66-
t.Fatalf(err.Error())
52+
t.Fatal(err)
53+
}
54+
if _, err := r1.Write([]byte("Hi")); err != nil {
55+
t.Fatal(err)
6756
}
68-
r1.Write([]byte("Hi"))
6957

7058
r2, err := net.Dial("tcp", "127.0.0.1:1234")
7159
if err != nil {
72-
t.Fatalf(err.Error())
60+
t.Fatal(err)
61+
}
62+
if _, err := r2.Write([]byte("Hi")); err != nil {
63+
t.Fatal(err)
7364
}
74-
r2.Write([]byte("Hi"))
7565

7666
cmd.Env = os.Environ()
7767
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")
7868

7969
out, err := cmd.CombinedOutput()
8070
if err != nil {
81-
println(string(out))
82-
t.Fatalf(err.Error())
71+
t.Fatalf("Unexpected error: %v (command output: %s)", err, out)
8372
}
8473

85-
correctStringWrittenNet(t, r1, "Hello world: fd1")
86-
correctStringWrittenNet(t, r2, "Goodbye world: fd2")
74+
correctStringWritten(t, r1, "Hello world: fd1")
75+
correctStringWritten(t, r2, "Goodbye world: fd2")
8776
}

activation/packetconns_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ func TestPacketConns(t *testing.T) {
2929

3030
u1, err := net.ListenUDP("udp", &net.UDPAddr{Port: 9999})
3131
if err != nil {
32-
t.Fatalf(err.Error())
32+
t.Fatal(err)
3333
}
3434
u2, err := net.ListenUDP("udp", &net.UDPAddr{Port: 1234})
3535
if err != nil {
36-
t.Fatalf(err.Error())
36+
t.Fatal(err)
3737
}
3838

3939
f1, _ := u1.File()
@@ -46,15 +46,19 @@ func TestPacketConns(t *testing.T) {
4646

4747
r1, err := net.Dial("udp", "127.0.0.1:9999")
4848
if err != nil {
49-
t.Fatalf(err.Error())
49+
t.Fatal(err)
50+
}
51+
if _, err := r1.Write([]byte("Hi")); err != nil {
52+
t.Fatal(err)
5053
}
51-
r1.Write([]byte("Hi"))
5254

5355
r2, err := net.Dial("udp", "127.0.0.1:1234")
5456
if err != nil {
55-
t.Fatalf(err.Error())
57+
t.Fatal(err)
58+
}
59+
if _, err := r2.Write([]byte("Hi")); err != nil {
60+
t.Fatal(err)
5661
}
57-
r2.Write([]byte("Hi"))
5862

5963
cmd.Env = os.Environ()
6064
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")
@@ -64,6 +68,6 @@ func TestPacketConns(t *testing.T) {
6468
t.Fatalf("Cmd output '%s', err: '%s'\n", out, err)
6569
}
6670

67-
correctStringWrittenNet(t, r1, "Hello world")
68-
correctStringWrittenNet(t, r2, "Goodbye world")
71+
correctStringWritten(t, r1, "Hello world")
72+
correctStringWritten(t, r2, "Goodbye world")
6973
}

daemon/sdnotify_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,22 @@ package daemon
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"net"
2120
"os"
2221
"testing"
2322
)
2423

25-
// TestSdNotify
2624
func TestSdNotify(t *testing.T) {
27-
28-
testDir, e := ioutil.TempDir("/tmp/", "test-")
29-
if e != nil {
30-
panic(e)
31-
}
32-
defer os.RemoveAll(testDir)
25+
testDir := t.TempDir()
3326

3427
notifySocket := testDir + "/notify-socket.sock"
3528
laddr := net.UnixAddr{
3629
Name: notifySocket,
3730
Net: "unixgram",
3831
}
39-
_, e = net.ListenUnixgram("unixgram", &laddr)
40-
if e != nil {
41-
panic(e)
32+
_, err := net.ListenUnixgram("unixgram", &laddr)
33+
if err != nil {
34+
t.Fatal(err)
4235
}
4336

4437
tests := []struct {
@@ -57,10 +50,7 @@ func TestSdNotify(t *testing.T) {
5750
}
5851

5952
for i, tt := range tests {
60-
must(os.Unsetenv("NOTIFY_SOCKET"))
61-
if tt.envSocket != "" {
62-
must(os.Setenv("NOTIFY_SOCKET", tt.envSocket))
63-
}
53+
t.Setenv("NOTIFY_SOCKET", tt.envSocket)
6454
sent, err := SdNotify(tt.unsetEnv, fmt.Sprintf("TestSdNotify test message #%d", i))
6555

6656
if sent != tt.wsent {

daemon/watchdog_test.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ import (
2121
"time"
2222
)
2323

24-
func must(err error) {
25-
if err != nil {
26-
panic(err)
27-
}
28-
}
29-
3024
func TestSdWatchdogEnabled(t *testing.T) {
3125
mypid := strconv.Itoa(os.Getpid())
3226
tests := []struct {
@@ -57,16 +51,8 @@ func TestSdWatchdogEnabled(t *testing.T) {
5751
}
5852

5953
for i, tt := range tests {
60-
if tt.usec != "" {
61-
must(os.Setenv("WATCHDOG_USEC", tt.usec))
62-
} else {
63-
must(os.Unsetenv("WATCHDOG_USEC"))
64-
}
65-
if tt.pid != "" {
66-
must(os.Setenv("WATCHDOG_PID", tt.pid))
67-
} else {
68-
must(os.Unsetenv("WATCHDOG_PID"))
69-
}
54+
t.Setenv("WATCHDOG_USEC", tt.usec)
55+
t.Setenv("WATCHDOG_PID", tt.pid)
7056

7157
delay, err := SdWatchdogEnabled(tt.unsetEnv)
7258

dbus/dbus.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Integration with the systemd D-Bus API. See http://www.freedesktop.org/wiki/Software/systemd/dbus/
15+
// Package dbus provides integration with the systemd D-Bus API.
16+
// See http://www.freedesktop.org/wiki/Software/systemd/dbus/
1617
package dbus
1718

1819
import (

dbus/dbus_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
func TestNeedsEscape(t *testing.T) {
2222
// Anything not 0-9a-zA-Z should always be escaped
2323
for want, vals := range map[bool][]byte{
24-
false: []byte{'a', 'b', 'z', 'A', 'Q', '1', '4', '9'},
25-
true: []byte{'#', '%', '$', '!', '.', '_', '-', '%', '\\'},
24+
false: {'a', 'b', 'z', 'A', 'Q', '1', '4', '9'},
25+
true: {'#', '%', '$', '!', '.', '_', '-', '%', '\\'},
2626
} {
2727
for i := 1; i < 10; i++ {
2828
for _, b := range vals {
@@ -36,8 +36,8 @@ func TestNeedsEscape(t *testing.T) {
3636

3737
// 0-9 in position 0 should be escaped
3838
for want, vals := range map[bool][]byte{
39-
false: []byte{'A', 'a', 'e', 'x', 'Q', 'Z'},
40-
true: []byte{'0', '4', '5', '9'},
39+
false: {'A', 'a', 'e', 'x', 'Q', 'Z'},
40+
true: {'0', '4', '5', '9'},
4141
} {
4242
for _, b := range vals {
4343
got := needsEscape(0, b)
@@ -46,7 +46,6 @@ func TestNeedsEscape(t *testing.T) {
4646
}
4747
}
4848
}
49-
5049
}
5150

5251
func TestPathBusEscape(t *testing.T) {
@@ -64,7 +63,6 @@ func TestPathBusEscape(t *testing.T) {
6463
t.Errorf("bad result for PathBusEscape(%s): got %q, want %q", in, got, want)
6564
}
6665
}
67-
6866
}
6967

7068
func TestPathBusUnescape(t *testing.T) {
@@ -89,7 +87,6 @@ func TestPathBusUnescape(t *testing.T) {
8987
// TestNew ensures that New() works without errors.
9088
func TestNew(t *testing.T) {
9189
_, err := New()
92-
9390
if err != nil {
9491
t.Fatal(err)
9592
}

0 commit comments

Comments
 (0)