fix most linter errors

This commit is contained in:
Michael Eischer
2025-02-02 22:45:41 +01:00
parent 3e0737a8bd
commit 9fc5066fc4
8 changed files with 57 additions and 42 deletions

View File

@@ -61,7 +61,10 @@ func TestUnixSocket(t *testing.T) {
if err != nil {
return err
}
resp.Body.Close()
err = resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode != test.StatusCode {
return fmt.Errorf("expected %d from server, instead got %d (path %s)", test.StatusCode, resp.StatusCode, test.Path)
}

View File

@@ -22,7 +22,7 @@ import (
type restServerApp struct {
CmdRoot *cobra.Command
Server restserver.Server
CpuProfile string
CPUProfile string
listenerAddressMu sync.Mutex
listenerAddress net.Addr // set after startup
@@ -36,7 +36,7 @@ func newRestServerApp() *restServerApp {
Short: "Run a REST server for use with restic",
SilenceErrors: true,
SilenceUsage: true,
Args: func(cmd *cobra.Command, args []string) error {
Args: func(_ *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("rest-server expects no arguments - unknown argument: %s", args[0])
}
@@ -52,7 +52,7 @@ func newRestServerApp() *restServerApp {
rv.CmdRoot.RunE = rv.runRoot
flags := rv.CmdRoot.Flags()
flags.StringVar(&rv.CpuProfile, "cpu-profile", rv.CpuProfile, "write CPU profile to file")
flags.StringVar(&rv.CPUProfile, "cpu-profile", rv.CPUProfile, "write CPU profile to file")
flags.BoolVar(&rv.Server.Debug, "debug", rv.Server.Debug, "output debug messages")
flags.StringVar(&rv.Server.Listen, "listen", rv.Server.Listen, "listen address")
flags.StringVar(&rv.Server.Log, "log", rv.Server.Log, "write HTTP requests in the combined log format to the specified `filename` (use \"-\" for logging to stdout)")
@@ -103,17 +103,19 @@ func (app *restServerApp) ListenerAddress() net.Addr {
return app.listenerAddress
}
func (app *restServerApp) runRoot(cmd *cobra.Command, args []string) error {
func (app *restServerApp) runRoot(_ *cobra.Command, _ []string) error {
log.SetFlags(0)
log.Printf("Data directory: %s", app.Server.Path)
if app.CpuProfile != "" {
f, err := os.Create(app.CpuProfile)
if app.CPUProfile != "" {
f, err := os.Create(app.CPUProfile)
if err != nil {
return err
}
defer f.Close()
defer func() {
_ = f.Close()
}()
if err := pprof.StartCPUProfile(f); err != nil {
return err

View File

@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
@@ -94,7 +93,7 @@ func TestTLSSettings(t *testing.T) {
}
func TestGetHandler(t *testing.T) {
dir, err := ioutil.TempDir("", "rest-server-test")
dir, err := os.MkdirTemp("", "rest-server-test")
if err != nil {
t.Fatal(err)
}
@@ -120,7 +119,7 @@ func TestGetHandler(t *testing.T) {
}
// With NoAuth = false and custom .htpasswd
htpFile, err := ioutil.TempFile(dir, "custom")
htpFile, err := os.CreateTemp(dir, "custom")
if err != nil {
t.Fatal(err)
}
@@ -137,7 +136,7 @@ func TestGetHandler(t *testing.T) {
// Create .htpasswd
htpasswd := filepath.Join(dir, ".htpasswd")
err = ioutil.WriteFile(htpasswd, []byte(""), 0644)
err = os.WriteFile(htpasswd, []byte(""), 0644)
if err != nil {
t.Fatal(err)
}
@@ -262,7 +261,10 @@ func TestHttpListen(t *testing.T) {
if err != nil {
return err
}
resp.Body.Close()
err = resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode != test.StatusCode {
return fmt.Errorf("expected %d from server, instead got %d (path %s)", test.StatusCode, resp.StatusCode, test.Path)
}