Skip to content

Commit fc9ad5f

Browse files
committed
fix: lint issues
1 parent ac464b2 commit fc9ad5f

File tree

18 files changed

+102
-149
lines changed

18 files changed

+102
-149
lines changed

.goreleaser.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
version: 2
2+
13
builds:
24
- skip: true
35

6+
release:
7+
prerelease: auto
8+
49
changelog:
510
use: github-native
611

cmd/webgrapple/reverseproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
const DefaultServiceAddress = "127.0.0.1:8888"
1111

12-
var MiddlewareFactory server.WebGrappleMiddleWareCreator = nil // should be overriden
12+
var MiddlewareFactory server.WebGrappleMiddleWareCreator // should be overridden
1313

1414
var (
1515
flagAddresses = []string{"https://localhost"}

pkg/clientconfig/clientconfig.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ func ReadConfig(file string) (multiServerConfig vo.ClientConfig, err error) {
1313
if errRead != nil {
1414
return nil, errRead
1515
}
16-
return readConfig(configBytes)
16+
return readConfigBytes(configBytes)
1717
}
1818

19-
func readConfig(configBytes []byte) (multiServerConfig vo.ClientConfig, err error) {
20-
19+
func readConfigBytes(configBytes []byte) (vo.ClientConfig, error) {
2120
// a list of services
2221
clientConfig := vo.ClientConfig{}
2322
if yaml.Unmarshal(configBytes, &clientConfig) == nil {
2423
return clientConfig, nil
2524
}
2625

2726
// just one service
28-
service := &vo.Service{}
27+
var service *vo.Service
2928
if yaml.Unmarshal(configBytes, &service) == nil {
3029
return vo.ClientConfig{
3130
service,

pkg/clientconfig/clientconfig_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/foomo/webgrapple/pkg/vo"
77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
var (
@@ -22,11 +23,10 @@ id: my-service
2223
)
2324

2425
func TestReadConfigService(t *testing.T) {
25-
serviceConfigServices, errRead := readConfig(configBytesServices)
26-
assert.NoError(t, errRead)
26+
serviceConfigServices, errRead := readConfigBytes(configBytesServices)
27+
require.NoError(t, errRead)
2728
assert.Equal(t, vo.ServiceID("hello"), serviceConfigServices[1].ID)
28-
serviceConfigService, errRead := readConfig(configBytesService)
29-
assert.NoError(t, errRead)
29+
serviceConfigService, errRead := readConfigBytes(configBytesService)
30+
require.NoError(t, errRead)
3031
assert.Equal(t, vo.ServiceID("my-service"), serviceConfigService[0].ID)
31-
return
3232
}

pkg/clientnpm/clientnpm.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func Run(
7878
port = ports[0]
7979
}
8080

81-
debugPort := 0
81+
var debugPort int
8282
if flagDebugServerPort == 0 && flagStartVSCode {
8383
debugPorts, errTakeDebugPort := freeport.Take(1)
8484
if errTakeDebugPort != nil {
@@ -89,7 +89,9 @@ func Run(
8989
debugPort = flagDebugServerPort
9090
}
9191
if flagStartVSCode {
92-
vscodedebug(l, workDir, name, debugPort)
92+
if err := vscodedebug(l, workDir, name, debugPort); err != nil {
93+
return err
94+
}
9395
}
9496

9597
// ports have to be set in env
@@ -145,12 +147,12 @@ func Run(
145147
}()
146148

147149
go func() {
148-
if _, err := io.Copy(os.Stdout, stdOutPipe); err != nil && err.(*os.PathError).Err != os.ErrClosed {
150+
if _, err := io.Copy(os.Stdout, stdOutPipe); err != nil && !errors.Is(err, os.ErrClosed) {
149151
l.Error(fmt.Sprintf("could not copy std out: %v", err))
150152
}
151153
}()
152154
go func() {
153-
if _, err := io.Copy(os.Stderr, stdErrPipe); err != nil && err.(*os.PathError).Err != os.ErrClosed {
155+
if _, err := io.Copy(os.Stderr, stdErrPipe); err != nil && !errors.Is(err, os.ErrClosed) {
154156
l.Error(fmt.Sprintf("could not copy std err: %v", err))
155157
}
156158
}()
@@ -176,8 +178,8 @@ func Run(
176178
}
177179

178180
func removeServices(ctx context.Context, l log.Logger, address string, config vo.ClientConfig) {
179-
client := server.NewServiceGoTSRPCClient(string(address), server.DefaultEndPoint)
180-
serviceIDs := []vo.ServiceID{}
181+
client := server.NewServiceGoTSRPCClient(address, server.DefaultEndPoint)
182+
var serviceIDs []vo.ServiceID
181183
for _, s := range config {
182184
serviceIDs = append(serviceIDs, s.ID)
183185
}
@@ -191,7 +193,7 @@ func removeServices(ctx context.Context, l log.Logger, address string, config vo
191193
}
192194

193195
func addServices(ctx context.Context, address string, config vo.ClientConfig, port int) error {
194-
client := server.NewServiceGoTSRPCClient(string(address), server.DefaultEndPoint)
196+
client := server.NewServiceGoTSRPCClient(address, server.DefaultEndPoint)
195197
errUpsert, errClient := client.Upsert(ctx, config)
196198
if errClient != nil {
197199
return errClient

pkg/clientnpm/vscodedebug.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,22 @@ func vscodedebug(logger log.Logger, path, name string, debugPort int) error {
6464

6565
debugConfig, errDebugConfig := vscodeDebugConfig(name, debugPort)
6666
if errDebugConfig != nil {
67-
return nil
67+
return errDebugConfig
6868
}
6969

70-
const tries = 5
71-
7270
go func() {
7371
logger.Info("starting vscode")
7472
launchOutput, errLaunch := exec.Command("code", vscodeTarget).CombinedOutput()
7573
if errLaunch == nil {
7674
launchedVSCode := false
77-
for i := 0; i < 5; i++ {
75+
for range 5 {
7876
_, errRunVSCodeStatus := exec.Command("code", "-s").CombinedOutput()
79-
if errRunVSCodeStatus != nil {
80-
logger.Info("waiting for vscode to start...")
81-
} else {
77+
if errRunVSCodeStatus == nil {
8278
logger.Info("vscode is up")
8379
launchedVSCode = true
8480
break
8581
}
82+
logger.Info("waiting for vscode to start...")
8683
}
8784
if launchedVSCode {
8885
logger.Info(fmt.Sprintf("launching vscode: %s", debugConfig))

pkg/httputils/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (s *serverErrorLogWriter) Write(p []byte) (int, error) {
3030
return len(p), nil
3131
}
3232

33-
func GracefulHttpServer(ctx context.Context, l log.Logger, name string, address string, handler http.Handler) *http.Server {
33+
func GracefulHTTPServer(ctx context.Context, l log.Logger, name string, address string, handler http.Handler) *http.Server {
3434
httpServer := &http.Server{
3535
Addr: address,
3636
Handler: handler,
@@ -39,6 +39,7 @@ func GracefulHttpServer(ctx context.Context, l log.Logger, name string, address
3939
name: name,
4040
}, "", 0),
4141
}
42+
cancelCtx := context.WithoutCancel(ctx)
4243

4344
idleConnectionsClosed := make(chan struct{})
4445
go func() {
@@ -53,7 +54,8 @@ func GracefulHttpServer(ctx context.Context, l log.Logger, name string, address
5354
l.Info(fmt.Sprintf("server %q shutdown initiated due to context", address))
5455
}
5556

56-
shutdownCtx, _ := context.WithTimeout(context.Background(), DefaultShutdownTimeout)
57+
shutdownCtx, cancel := context.WithTimeout(cancelCtx, DefaultShutdownTimeout)
58+
defer cancel()
5759
if err := httpServer.Shutdown(shutdownCtx); err != nil {
5860
l.Info(fmt.Sprintf("HTTP Server Shutdown Error: %v", err))
5961
}

pkg/server/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package server
22

33
import (
4-
http "net/http"
4+
"net/http"
55
"net/url"
66

77
"github.com/foomo/webgrapple/pkg/vo"

pkg/server/registry.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"github.com/foomo/webgrapple/pkg/vo"
1010
)
1111

12-
func (sm ServiceMap) cp() (copy ServiceMap) {
13-
copy = ServiceMap{}
12+
func (sm ServiceMap) cp() ServiceMap {
13+
c := ServiceMap{}
1414
for id, s := range sm {
15-
copy[id] = s
15+
c[id] = s
1616
}
17-
return
17+
return c
1818
}
1919

2020
type registryState struct {
@@ -38,33 +38,33 @@ func newRegistry(l log.Logger, backendURL *url.URL, middlewareFactory WebGrapple
3838
}
3939

4040
func (r *registry) getServicesCopy() ServiceMap {
41-
copy := ServiceMap{}
41+
c := ServiceMap{}
4242
if r.state != nil && r.state.services != nil {
43-
copy = r.state.services.cp()
43+
c = r.state.services.cp()
4444
}
45-
return copy
45+
return c
4646
}
4747

4848
func (r *registry) upsert(services []*vo.Service) (err error) {
49-
copy := r.getServicesCopy()
49+
c := r.getServicesCopy()
5050
for _, service := range services {
5151
r.logger.Info(fmt.Sprintf("upserting service %q with backend %q", service.ID, service.Address))
52-
copy[service.ID] = service
52+
c[service.ID] = service
5353
}
54-
return r.update(copy)
54+
return r.update(c)
5555
}
5656

5757
func (r *registry) remove(ids []vo.ServiceID) (err error) {
58-
copy := r.getServicesCopy()
58+
c := r.getServicesCopy()
5959
for _, id := range ids {
60-
_, found := copy[id]
60+
_, found := c[id]
6161
if !found {
6262
return errors.New("service not found")
6363
}
6464
r.logger.Info(fmt.Sprintf("removing service with ID %q", id))
65-
delete(copy, id)
65+
delete(c, id)
6666
}
67-
return r.update(copy)
67+
return r.update(c)
6868
}
6969

7070
func (r *registry) update(services ServiceMap) error {

pkg/server/run.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func ensureCertAndKey(
142142
return certFile, keyFile, nil
143143
}
144144

145-
func checkHosts(l log.Logger, hostList []hostName) (hostAdresses map[hostName]string) {
146-
hostAdresses = map[hostName]string{}
145+
func checkHosts(l log.Logger, hostList []hostName) map[hostName]string {
146+
hostAdresses := map[hostName]string{}
147147
for _, host := range hostList {
148148
addresses, errLookup := net.LookupHost(string(host))
149149
if errLookup != nil {
@@ -222,7 +222,7 @@ func Run(
222222
if usedAddressPorts[addressPort] == 1 {
223223
g.Go(func() error {
224224
name := fmt.Sprintf("proxy (%s)", u)
225-
httpServer := httputils.GracefulHttpServer(gctx, l, name, listenAddress, s)
225+
httpServer := httputils.GracefulHTTPServer(gctx, l, name, listenAddress, s)
226226
l.Info(fmt.Sprintf("starting server on %s", addressPort))
227227
if useTLS {
228228
return httpServer.ListenAndServeTLS(certFile, keyFile)
@@ -236,7 +236,7 @@ func Run(
236236

237237
g.Go(func() error {
238238
l.Info(fmt.Sprintf("starting dev client service on %q", serviceAddress))
239-
httpDevClient := httputils.GracefulHttpServer(gctx, l, "dev-client", serviceAddress, s.serviceHandler)
239+
httpDevClient := httputils.GracefulHTTPServer(gctx, l, "dev-client", serviceAddress, s.serviceHandler)
240240
return httpDevClient.ListenAndServe()
241241
})
242242

0 commit comments

Comments
 (0)