Skip to content

Commit

Permalink
Merge pull request #7175 from mook-as/lint/go/errcheck
Browse files Browse the repository at this point in the history
golangci-lint: Partially restore linters from the main repo
  • Loading branch information
jandubois authored Jul 17, 2024
2 parents 9e0639b + c77c78b commit cd4a74a
Show file tree
Hide file tree
Showing 47 changed files with 425 additions and 283 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/config/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@ linters-settings:
linters:
disable-all: true
enable:
- bodyclose
- dogsled
- errcheck
- exportloopref
- goconst
- gofmt
- goimports
- goprintffuncname
- ineffassign
- typecheck
- unconvert
- unused
- whitespace
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
Expand All @@ -61,6 +73,11 @@ issues:
- gosec
- errcheck
- gocritic
- # Exclude bodyclose when it's passed to client.ProcessRequestForAPI
# or client.ProcessRequestForUtility which internally closes the body.
path: src/go/rdctl/
linters: [ bodyclose ]
source: "client.ProcessRequestFor(API|Utility)\\(rdClient.DoRequest(WithPayload)?\\("
- # Exclude ST1005 when it encounters errors starting with proper noun
linters: [ stylecheck ]
text: "ST1005:"
Expand All @@ -73,6 +90,6 @@ issues:
path: src/go/wsl-helper/pkg/dockerproxy/platform/vsock_linux\.go
linters: [ gocritic ]
text: todoCommentWithoutDetail
- # Exclude ignoring errors from syscall
- # Ignore errors from syscall
linters: [ dogsled ]
source: ^\s*_, _, _ = .*\.Call\(
File renamed without changes.
34 changes: 34 additions & 0 deletions src/go/guestagent/pkg/kube/watcher_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build !linux

/*
Copyright © 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kube

import (
"context"
"fmt"
"net"

"github.com/rancher-sandbox/rancher-desktop/src/go/guestagent/pkg/tracker"
)

func WatchForServices(
ctx context.Context,
configPath string,
k8sServiceListenerIP net.IP,
enableListeners bool,
portTracker tracker.Tracker,
) error {
return fmt.Errorf("not implemented for non-linux")
}
36 changes: 36 additions & 0 deletions src/go/guestagent/pkg/tracker/listenertracker_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build !linux

/*
Copyright © 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tracker

import (
"context"
"fmt"
"net"
)

type ListenerTracker struct{}

func NewListenerTracker() *ListenerTracker {
panic("not implement for non-Linux")
}

func (l *ListenerTracker) AddListener(ctx context.Context, ip net.IP, port int) error {
return fmt.Errorf("not implemented for non-Linux")
}

func (l *ListenerTracker) RemoveListener(_ context.Context, ip net.IP, port int) error {
return fmt.Errorf("not implemented for non-Linux")
}
2 changes: 2 additions & 0 deletions src/go/nerdctl-stub/generate/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func main() {
logrus.WithError(err).WithField("path", outputPath).Fatal("error creating output")
}
defer output.Close()
//nolint:dogsled // we only require the file name; we can also ignore `ok`, as
// on failure we just have no useful file name.
_, filename, _, _ := runtime.Caller(0)
data := map[string]interface{}{
"package": filename,
Expand Down
6 changes: 3 additions & 3 deletions src/go/privileged-service/pkg/manage/install_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func InstallService(name, displayName, desc string) error {
if err != nil {
return err
}
defer m.Disconnect()
defer disconnect(m)

// We always need uninstall first to unregister,
// the event logger recreation service can yield to a registry key error
// e.g RancherDesktopPrivilegedService registry key already exists
UninstallService(name)
_ = UninstallService(name)

s, err := m.CreateService(name, instPath, mgr.Config{DisplayName: displayName, Description: desc})
if err != nil {
Expand All @@ -62,7 +62,7 @@ func InstallService(name, displayName, desc string) error {

err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
_ = s.Delete()
return fmt.Errorf("setup event log for [%s] failed: %w", name, err)
}
return nil
Expand Down
15 changes: 12 additions & 3 deletions src/go/privileged-service/pkg/manage/manage_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func StartService(name string) error {
if err != nil {
return err
}
defer m.Disconnect()
defer disconnect(m)
s, err := openService(m.Handle, name)
if err != nil {
return fmt.Errorf("could not access service: %w", err)
Expand All @@ -61,7 +61,7 @@ func ControlService(name string, control svc.Cmd, desiredState svc.State) error
if err != nil {
return err
}
defer m.Disconnect()
defer disconnect(m)
s, err := openService(m.Handle, name)
if err != nil {
return fmt.Errorf("could not access service: %w", err)
Expand Down Expand Up @@ -97,10 +97,19 @@ func connect() (*mgr.Mgr, error) {
return &mgr.Mgr{Handle: h}, nil
}

// disconnect from the manager, swallowing errors.
func disconnect(m *mgr.Mgr) {
_ = m.Disconnect()
}

// connect is same as mgr.OpenService with minimal
// access control
func openService(svcHandle windows.Handle, name string) (*mgr.Service, error) {
h, err := windows.OpenService(svcHandle, syscall.StringToUTF16Ptr(name), SERVICE_MINIMAL_ACCESS)
ptr, err := syscall.UTF16PtrFromString(name)
if err != nil {
return nil, err
}
h, err := windows.OpenService(svcHandle, ptr, SERVICE_MINIMAL_ACCESS)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion src/go/privileged-service/pkg/manage/uninstall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func UninstallService(name string) error {
if err != nil {
return fmt.Errorf("connect to service [%s] failed: %w", name, err)
}
defer m.Disconnect()
defer disconnect(m)
s, err := m.OpenService(name)
if err != nil {
if errors.Is(err, windows.ERROR_SERVICE_DOES_NOT_EXIST) {
Expand Down
12 changes: 6 additions & 6 deletions src/go/privileged-service/pkg/port/server_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *Server) Start() error {
if err != nil {
select {
case <-s.quit:
s.eventLogger.Info(uint32(windows.NO_ERROR), "port server received a stop signal")
_ = s.eventLogger.Info(uint32(windows.NO_ERROR), "port server received a stop signal")
return nil
default:
return fmt.Errorf("port server connection accept error: %w", err)
Expand All @@ -93,22 +93,22 @@ func (s *Server) handleEvent(conn net.Conn) {
var pm types.PortMapping
err := json.NewDecoder(conn).Decode(&pm)
if err != nil {
s.eventLogger.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("port server decoding received payload error: %v", err))
_ = s.eventLogger.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("port server decoding received payload error: %v", err))
return
}
s.eventLogger.Info(uint32(windows.NO_ERROR), fmt.Sprintf("handleEvent for %+v", pm))
_ = s.eventLogger.Info(uint32(windows.NO_ERROR), fmt.Sprintf("handleEvent for %+v", pm))
if err = s.proxy.exec(pm); err != nil {
s.eventLogger.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("port proxy [%+v] failed: %v", pm, err))
_ = s.eventLogger.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("port proxy [%+v] failed: %v", pm, err))
}
}

// Stop shuts down the server gracefully
func (s *Server) Stop() {
close(s.quit)
s.listener.Close()
s.eventLogger.Info(uint32(windows.NO_ERROR), fmt.Sprintf("remove all %+v", s.proxy.portMappings))
_ = s.eventLogger.Info(uint32(windows.NO_ERROR), fmt.Sprintf("remove all %+v", s.proxy.portMappings))
if err := s.proxy.removeAll(); err != nil {
s.eventLogger.Warning(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), err.Error())
_ = s.eventLogger.Warning(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), err.Error())
}
s.stopped = true
}
6 changes: 3 additions & 3 deletions src/go/privileged-service/pkg/svc/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func RunService(name string, isDebug bool) error {
return errors.Wrap(err, "RunService could not initialize event logger")
}
defer elog.Close()
elog.Info(uint32(windows.NO_ERROR), fmt.Sprintf("%s service starting", name))
_ = elog.Info(uint32(windows.NO_ERROR), fmt.Sprintf("%s service starting", name))
run := svc.Run
if isDebug {
run = debug.Run
Expand All @@ -45,10 +45,10 @@ func RunService(name string, isDebug bool) error {
supervisor := NewSupervisor(portServer, elog)
err = run(name, supervisor)
if err != nil {
elog.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("%s service failed: %v", name, err))
_ = elog.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("%s service failed: %v", name, err))
return err
}
elog.Info(uint32(windows.NO_ERROR), fmt.Sprintf("%s service stopped", name))
_ = elog.Info(uint32(windows.NO_ERROR), fmt.Sprintf("%s service stopped", name))
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions src/go/privileged-service/pkg/svc/supervisor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@ func (s *Supervisor) Execute(args []string, r <-chan svc.ChangeRequest, changes
changes <- svc.Status{State: svc.StartPending}
startErr := make(chan error)
go func() {
s.eventLogger.Info(uint32(windows.NO_ERROR), "port server is starting")
_ = s.eventLogger.Info(uint32(windows.NO_ERROR), "port server is starting")
startErr <- s.portServer.Start()
}()
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
case e := <-startErr:
s.eventLogger.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("supervisor failed to start: %v", e))
_ = s.eventLogger.Error(uint32(windows.ERROR_EXCEPTION_IN_SERVICE), fmt.Sprintf("supervisor failed to start: %v", e))
return false, uint32(windows.ERROR_SERVICE_NEVER_STARTED)
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
s.portServer.Stop()
s.eventLogger.Info(uint32(windows.NO_ERROR), "port server is stopping")
_ = s.eventLogger.Info(uint32(windows.NO_ERROR), "port server is stopping")
changes <- svc.Status{State: svc.Stopped, Accepts: cmdsAccepted}
break loop
default:
s.eventLogger.Error(uint32(windows.ERROR_INVALID_SERVICE_CONTROL), fmt.Sprintf("unexpected control request #%d", c))
_ = s.eventLogger.Error(uint32(windows.ERROR_INVALID_SERVICE_CONTROL), fmt.Sprintf("unexpected control request #%d", c))
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/go/rdctl/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ func doAPICommand(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
response, err := rdClient.DoRequestWithPayload(apiSettings.Method, endpoint, bytes.NewBuffer(contents))
result, errorPacket, err = client.ProcessRequestForAPI(response, err)
method := apiSettings.Method
payload := bytes.NewBuffer(contents)
result, errorPacket, err = client.ProcessRequestForAPI(rdClient.DoRequestWithPayload(method, endpoint, payload))
} else if apiSettings.Body != "" {
if apiSettings.Method == "" {
apiSettings.Method = "PUT"
}
response, err := rdClient.DoRequestWithPayload(apiSettings.Method, endpoint, bytes.NewBufferString(apiSettings.Body))
result, errorPacket, err = client.ProcessRequestForAPI(response, err)
method := apiSettings.Method
payload := bytes.NewBufferString(apiSettings.Body)
result, errorPacket, err = client.ProcessRequestForAPI(rdClient.DoRequestWithPayload(method, endpoint, payload))
} else {
if apiSettings.Method == "" {
apiSettings.Method = "GET"
Expand Down
10 changes: 5 additions & 5 deletions src/go/rdctl/cmd/createProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ func createProfile() (string, error) {
// This should have been caught in validateProfileFormatFlags
return "", fmt.Errorf(`no input format specified: must specify exactly one input format of "--input FILE|-", "--body|-b STRING", or "--from-settings"`)
}
connectionInfo, err := config.GetConnectionInfo(false)
if err != nil {
return "", fmt.Errorf("failed to get connection info: %w", err)
connectionInfo, err2 := config.GetConnectionInfo(false)
if err2 != nil {
return "", fmt.Errorf("failed to get connection info: %w", err2)
}
rdClient := client.NewRDClient(connectionInfo)
response, err := rdClient.DoRequest("GET", client.VersionCommand("", "settings"))
output, err = client.ProcessRequestForUtility(response, err)
command := client.VersionCommand("", "settings")
output, err = client.ProcessRequestForUtility(rdClient.DoRequest("GET", command))
}
if err != nil {
return "", err
Expand Down
1 change: 1 addition & 0 deletions src/go/rdctl/cmd/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand Down
1 change: 1 addition & 0 deletions src/go/rdctl/cmd/extensionInstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd

import (
"fmt"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/client"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/config"
"github.com/spf13/cobra"
Expand Down
5 changes: 3 additions & 2 deletions src/go/rdctl/cmd/listSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"fmt"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/client"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,6 +53,6 @@ func getListSettings() ([]byte, error) {
return []byte{}, fmt.Errorf("failed to get connection info: %w", err)
}
rdClient := client.NewRDClient(connectionInfo)
response, err := rdClient.DoRequest("GET", client.VersionCommand("", "settings"))
return client.ProcessRequestForUtility(response, err)
command := client.VersionCommand("", "settings")
return client.ProcessRequestForUtility(rdClient.DoRequest("GET", command))
}
3 changes: 2 additions & 1 deletion src/go/rdctl/cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"encoding/json"
"fmt"
"os"

p "github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/paths"
"github.com/spf13/cobra"
"os"
)

var pathsCmd = &cobra.Command{
Expand Down
7 changes: 4 additions & 3 deletions src/go/rdctl/cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/client"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/config"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/options/generated"
options "github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/options/generated"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -65,8 +65,9 @@ func doSetCommand(cmd *cobra.Command) error {
return err
}

response, err := rdClient.DoRequestWithPayload("PUT", client.VersionCommand("", "settings"), bytes.NewBuffer(jsonBuffer))
result, err := client.ProcessRequestForUtility(response, err)
command := client.VersionCommand("", "settings")
buf := bytes.NewBuffer(jsonBuffer)
result, err := client.ProcessRequestForUtility(rdClient.DoRequestWithPayload("PUT", command, buf))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cd4a74a

Please sign in to comment.