Skip to content

Commit 64b8d76

Browse files
author
Abhijeet
authored
fix additional lint issues and expand linter scope (#344)
* use grpc.WithContextDialer instead of deprecated grpc.WithDialer `time.Duration`s were being expected by the WithDialer callback. However, the callback functions were not making use of this parameter and hence their removal is safe. * fix additional lint issues and expand linter scope
1 parent 810097c commit 64b8d76

File tree

24 files changed

+66
-90
lines changed

24 files changed

+66
-90
lines changed

.github/workflows/test.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ jobs:
3333
fi
3434
- name: Lint code
3535
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
36-
with:
37-
args: --enable-only errcheck # this is temporary until the other lint errors are fixed
3836
go-test:
3937
needs: go-fmt-and-vet
4038
runs-on: ubuntu-latest

client.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"fmt"
1515
"hash"
1616
"io"
17-
"io/ioutil"
1817
"net"
1918
"os"
2019
"os/exec"
@@ -202,7 +201,7 @@ type ClientConfig struct {
202201
// SyncStdout, SyncStderr can be set to override the
203202
// respective os.Std* values in the plugin. Care should be taken to
204203
// avoid races here. If these are nil, then this will be set to
205-
// ioutil.Discard.
204+
// io.Discard.
206205
SyncStdout io.Writer
207206
SyncStderr io.Writer
208207

@@ -401,7 +400,7 @@ func NewClient(config *ClientConfig) (c *Client) {
401400
}
402401

403402
if config.Stderr == nil {
404-
config.Stderr = ioutil.Discard
403+
config.Stderr = io.Discard
405404
}
406405

407406
if config.SyncStdout == nil {
@@ -853,15 +852,15 @@ func (c *Client) Start() (addr net.Addr, err error) {
853852
var coreProtocol int
854853
coreProtocol, err = strconv.Atoi(parts[0])
855854
if err != nil {
856-
err = fmt.Errorf("Error parsing core protocol version: %s", err)
855+
err = fmt.Errorf("error parsing core protocol version: %s", err)
857856
return
858857
}
859858

860859
if coreProtocol != CoreProtocolVersion {
861-
err = fmt.Errorf("Incompatible core API version with plugin. "+
860+
err = fmt.Errorf("incompatible core API version with plugin. "+
862861
"Plugin version: %s, Core version: %d\n\n"+
863862
"To fix this, the plugin usually only needs to be recompiled.\n"+
864-
"Please report this to the plugin author.", parts[0], CoreProtocolVersion)
863+
"Please report this to the plugin author", parts[0], CoreProtocolVersion)
865864
return
866865
}
867866
}
@@ -887,10 +886,16 @@ func (c *Client) Start() (addr net.Addr, err error) {
887886
switch network {
888887
case "tcp":
889888
addr, err = net.ResolveTCPAddr("tcp", address)
889+
if err != nil {
890+
return nil, err
891+
}
890892
case "unix":
891893
addr, err = net.ResolveUnixAddr("unix", address)
894+
if err != nil {
895+
return nil, err
896+
}
892897
default:
893-
err = fmt.Errorf("Unknown address type: %s", address)
898+
return nil, fmt.Errorf("unknown address type: %s", address)
894899
}
895900

896901
// If we have a server type, then record that. We default to net/rpc
@@ -908,7 +913,7 @@ func (c *Client) Start() (addr net.Addr, err error) {
908913
}
909914
}
910915
if !found {
911-
err = fmt.Errorf("Unsupported plugin protocol %q. Supported: %v",
916+
err = fmt.Errorf("unsupported plugin protocol %q. Supported: %v",
912917
c.protocol, c.config.AllowedProtocols)
913918
return addr, err
914919
}
@@ -1041,7 +1046,7 @@ func (c *Client) checkProtoVersion(protoVersion string) (int, PluginSet, error)
10411046
return version, plugins, nil
10421047
}
10431048

1044-
return 0, nil, fmt.Errorf("Incompatible API version with plugin. "+
1049+
return 0, nil, fmt.Errorf("incompatible API version with plugin. "+
10451050
"Plugin version: %d, Client versions: %d", serverVersion, clientVersions)
10461051
}
10471052

@@ -1097,8 +1102,8 @@ func (c *Client) Protocol() Protocol {
10971102
return c.protocol
10981103
}
10991104

1100-
func netAddrDialer(addr net.Addr) func(string, time.Duration) (net.Conn, error) {
1101-
return func(_ string, _ time.Duration) (net.Conn, error) {
1105+
func netAddrDialer(addr net.Addr) func(context.Context, string) (net.Conn, error) {
1106+
return func(context.Context, string) (net.Conn, error) {
11021107
// Connect to the client
11031108
conn, err := net.Dial(addr.Network(), addr.String())
11041109
if err != nil {
@@ -1115,7 +1120,7 @@ func netAddrDialer(addr net.Addr) func(string, time.Duration) (net.Conn, error)
11151120

11161121
// dialer is compatible with grpc.WithDialer and creates the connection
11171122
// to the plugin.
1118-
func (c *Client) dialer(_ string, timeout time.Duration) (net.Conn, error) {
1123+
func (c *Client) dialer(ctx context.Context, _ string) (net.Conn, error) {
11191124
muxer, err := c.getGRPCMuxer(c.address)
11201125
if err != nil {
11211126
return nil, err
@@ -1128,7 +1133,7 @@ func (c *Client) dialer(_ string, timeout time.Duration) (net.Conn, error) {
11281133
return nil, err
11291134
}
11301135
} else {
1131-
conn, err = netAddrDialer(c.address)("", timeout)
1136+
conn, err = netAddrDialer(c.address)(ctx, "")
11321137
if err != nil {
11331138
return nil, err
11341139
}

client_posix_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestClient_testInterfaceReattach(t *testing.T) {
3939
if reattach == nil {
4040
c.Kill()
4141
t.Fatal("reattach config should be non-nil")
42+
return // Required for staticcheck SA5011
4243
}
4344

4445
// Find the process and defer a kill so we know it is gone

client_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func testClient_reattachGRPC(t *testing.T, useReattachFunc bool) {
668668

669669
func TestClient_reattachNotFound(t *testing.T) {
670670
// Find a bad pid
671-
var pid int = 5000
671+
pid := 5000
672672
for i := pid; i < 32000; i++ {
673673
if _, err := os.FindProcess(i); err != nil {
674674
pid = i
@@ -916,11 +916,7 @@ func TestClient_Stdin(t *testing.T) {
916916
t.Fatalf("error: %s", err)
917917
}
918918

919-
for {
920-
if c.Exited() {
921-
break
922-
}
923-
919+
for !c.Exited() {
924920
time.Sleep(50 * time.Millisecond)
925921
}
926922

@@ -954,10 +950,7 @@ func TestClient_SkipHostEnv(t *testing.T) {
954950
t.Fatalf("error: %s", err)
955951
}
956952

957-
for {
958-
if c.Exited() {
959-
break
960-
}
953+
for !c.Exited() {
961954

962955
time.Sleep(50 * time.Millisecond)
963956
}
@@ -1072,7 +1065,7 @@ func TestClient_TLS(t *testing.T) {
10721065
}
10731066

10741067
// Grab the impl
1075-
raw, err := clientBad.Dispense("test")
1068+
_, err = clientBad.Dispense("test")
10761069
if err == nil {
10771070
t.Fatal("expected error, got nil")
10781071
}
@@ -1101,7 +1094,7 @@ func TestClient_TLS(t *testing.T) {
11011094
}
11021095

11031096
// Grab the impl
1104-
raw, err = client.Dispense("test")
1097+
raw, err := client.Dispense("test")
11051098
if err != nil {
11061099
t.Fatalf("err should be nil, got %s", err)
11071100
}

client_unix_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"os/exec"
1313
"os/user"
1414
"path/filepath"
15-
"runtime"
1615
"syscall"
1716
"testing"
1817

@@ -22,10 +21,6 @@ import (
2221
)
2322

2423
func TestSetGroup(t *testing.T) {
25-
if runtime.GOOS == "windows" {
26-
t.Skip("go-plugin doesn't support unix sockets on Windows")
27-
}
28-
2924
group, err := user.LookupGroupId(fmt.Sprintf("%d", os.Getgid()))
3025
if err != nil {
3126
t.Fatal(err)

examples/bidirectional/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55

66
import (
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"log"
1010
"os"
1111
"os/exec"
@@ -23,7 +23,7 @@ func (*addHelper) Sum(a, b int64) (int64, error) {
2323

2424
func main() {
2525
// We don't want to see the plugin logs.
26-
log.SetOutput(ioutil.Discard)
26+
log.SetOutput(io.Discard)
2727

2828
// We're a host. Start by launching the plugin process.
2929
client := plugin.NewClient(&plugin.ClientConfig{

examples/bidirectional/plugin-go-grpc/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55

66
import (
77
"encoding/json"
8-
"io/ioutil"
8+
"os"
99

1010
"github.com/hashicorp/go-plugin"
1111
"github.com/hashicorp/go-plugin/examples/bidirectional/shared"
@@ -33,11 +33,11 @@ func (k *Counter) Put(key string, value int64, a shared.AddHelper) error {
3333
return err
3434
}
3535

36-
return ioutil.WriteFile("kv_"+key, buf, 0644)
36+
return os.WriteFile("kv_"+key, buf, 0644)
3737
}
3838

3939
func (k *Counter) Get(key string) (int64, error) {
40-
dataRaw, err := ioutil.ReadFile("kv_" + key)
40+
dataRaw, err := os.ReadFile("kv_" + key)
4141
if err != nil {
4242
return 0, err
4343
}

examples/grpc/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55

66
import (
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"log"
1010
"os"
1111
"os/exec"
@@ -57,15 +57,15 @@ func run() error {
5757
}
5858

5959
default:
60-
return fmt.Errorf("Please only use 'get' or 'put', given: %q", os.Args[0])
60+
return fmt.Errorf("please only use 'get' or 'put', given: %q", os.Args[0])
6161
}
6262

6363
return nil
6464
}
6565

6666
func main() {
6767
// We don't want to see the plugin logs.
68-
log.SetOutput(ioutil.Discard)
68+
log.SetOutput(io.Discard)
6969

7070
if err := run(); err != nil {
7171
fmt.Printf("error: %+v\n", err)

examples/grpc/plugin-go-grpc/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55

66
import (
77
"fmt"
8-
"io/ioutil"
8+
"os"
99

1010
"github.com/hashicorp/go-plugin"
1111
"github.com/hashicorp/go-plugin/examples/grpc/shared"
@@ -17,11 +17,11 @@ type KV struct{}
1717

1818
func (KV) Put(key string, value []byte) error {
1919
value = []byte(fmt.Sprintf("%s\n\nWritten from plugin-go-grpc", string(value)))
20-
return ioutil.WriteFile("kv_"+key, value, 0644)
20+
return os.WriteFile("kv_"+key, value, 0644)
2121
}
2222

2323
func (KV) Get(key string) ([]byte, error) {
24-
return ioutil.ReadFile("kv_" + key)
24+
return os.ReadFile("kv_" + key)
2525
}
2626

2727
func main() {

examples/grpc/plugin-go-netrpc/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55

66
import (
77
"fmt"
8-
"io/ioutil"
8+
"os"
99

1010
"github.com/hashicorp/go-plugin"
1111
"github.com/hashicorp/go-plugin/examples/grpc/shared"
@@ -17,11 +17,11 @@ type KV struct{}
1717

1818
func (KV) Put(key string, value []byte) error {
1919
value = []byte(fmt.Sprintf("%s\n\nWritten from plugin-go-netrpc", string(value)))
20-
return ioutil.WriteFile("kv_"+key, value, 0644)
20+
return os.WriteFile("kv_"+key, value, 0644)
2121
}
2222

2323
func (KV) Get(key string) ([]byte, error) {
24-
return ioutil.ReadFile("kv_" + key)
24+
return os.ReadFile("kv_" + key)
2525
}
2626

2727
func main() {

0 commit comments

Comments
 (0)