Skip to content

Commit

Permalink
Enable staticcheck linter and solve issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vascoguita committed Nov 22, 2022
1 parent e023164 commit ca02115
Show file tree
Hide file tree
Showing 70 changed files with 275 additions and 218 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ linters:
- stylecheck # TODO: consider enabling the 'stylecheck' linter to enforce style rules.
- usestdlibvars # TODO: consider enabling the 'usestdlibvars' linter to detect the possibility to use variables/constants from the Go standard library.
- thelper # TODO: consider enabling the 'thelper' linter to detect golang test helpers without t.Helper() call and check the consistency of test helpers.
- staticcheck # TODO: consider enabling the 'staticcheck' linter to find bugs and performance issues, offer simplifications, and enforce style rules.
- predeclared # TODO: consider enabling the 'predeclared' linter to find code that shadows one of Go's predeclared identifiers.
- paralleltest # TODO: consider enabling the 'paralleltest' linter to detect missing usage of t.Parallel() method in Go test.
- ireturn # TODO: consider enabling the 'ireturn' linter to accept interfaces and return concrete types.
Expand Down
3 changes: 3 additions & 0 deletions changelog/unreleased/enhancement-staticcheck.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Enable staticcheck linter in golangci-lint and solve issues

https://github.com/cs3org/reva/pull/3464
10 changes: 5 additions & 5 deletions cmd/reva/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package main
import (
"bufio"
"encoding/json"
"io/ioutil"
"os"
gouser "os/user"
"path"
"strings"
Expand Down Expand Up @@ -50,7 +50,7 @@ func getConfigFile() string {
}

func readConfig() (*config, error) {
data, err := ioutil.ReadFile(getConfigFile())
data, err := os.ReadFile(getConfigFile())
if err != nil {
return nil, err
}
Expand All @@ -68,7 +68,7 @@ func writeConfig(c *config) error {
if err != nil {
return err
}
return ioutil.WriteFile(getConfigFile(), data, 0600)
return os.WriteFile(getConfigFile(), data, 0600)
}

func getTokenFile() string {
Expand All @@ -81,15 +81,15 @@ func getTokenFile() string {
}

func readToken() (string, error) {
data, err := ioutil.ReadFile(getTokenFile())
data, err := os.ReadFile(getTokenFile())
if err != nil {
return "", err
}
return string(data), nil
}

func writeToken(token string) {
err := ioutil.WriteFile(getTokenFile(), []byte(token), 0600)
err := os.WriteFile(getTokenFile(), []byte(token), 0600)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/revad/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ package config

import (
"io"
"io/ioutil"

"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)

// Read reads the configuration from the reader.
func Read(r io.Reader) (map[string]interface{}, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
err = errors.Wrap(err, "config: error reading from reader")
return nil, err
Expand Down
9 changes: 4 additions & 5 deletions cmd/revad/internal/grace/grace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package grace

import (
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (w *Watcher) clean() error {
}

func (w *Watcher) readPID() (int, error) {
piddata, err := ioutil.ReadFile(w.pidFile)
piddata, err := os.ReadFile(w.pidFile)
if err != nil {
return 0, err
}
Expand All @@ -123,7 +122,7 @@ func (w *Watcher) readPID() (int, error) {
// GetProcessFromFile reads the pidfile and returns the running process or error if the process or file
// are not available.
func GetProcessFromFile(pfile string) (*os.Process, error) {
data, err := ioutil.ReadFile(pfile)
data, err := os.ReadFile(pfile)
if err != nil {
return nil, err
}
Expand All @@ -144,7 +143,7 @@ func GetProcessFromFile(pfile string) (*os.Process, error) {
// WritePID writes the pid to the configured pid file.
func (w *Watcher) WritePID() error {
// Read in the pid file as a slice of bytes.
if piddata, err := ioutil.ReadFile(w.pidFile); err == nil {
if piddata, err := os.ReadFile(w.pidFile); err == nil {
// Convert the file contents to an integer.
if pid, err := strconv.Atoi(string(piddata)); err == nil {
// Look for the pid in the process list.
Expand Down Expand Up @@ -174,7 +173,7 @@ func (w *Watcher) WritePID() error {

// If we get here, then the pidfile didn't exist or we are are in graceful reload and thus we overwrite
// or the pid in it doesn't belong to the user running this app.
err := ioutil.WriteFile(w.pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0664)
err := os.WriteFile(w.pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0664)
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io/fs"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -163,10 +163,18 @@ func getConfigs() ([]map[string]interface{}, error) {
}

func getConfigsFromDir(dir string) (confs []string, err error) {
files, err := ioutil.ReadDir(*dirFlag)
entries, err := os.ReadDir(*dirFlag)
if err != nil {
return nil, err
}
files := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, err
}
files = append(files, info)
}

for _, value := range files {
if !value.IsDir() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ import (
)

func main() {
err := ioutil.WriteFile("file1", []byte(""), 0600)
err := os.WriteFile("file1", []byte(""), 0600)
if err != nil {
os.Exit(1)
}
err = ioutil.WriteFile("file2", []byte(""), 0600)
err = os.WriteFile("file2", []byte(""), 0600)
if err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -213,11 +213,11 @@ import (
)

func main() {
err := ioutil.WriteFile("file1", []byte(""), 0600)
err := os.WriteFile("file1", []byte(""), 0600)
if err != nil {
os.Exit(1)
}
err = ioutil.WriteFile("file2", []byte(""), 0600)
err = os.WriteFile("file2", []byte(""), 0600)
if err != nil {
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/plugin/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"os"
"strings"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (m *Manager) Configure(ml map[string]interface{}) error {
return err
}

f, err := ioutil.ReadFile(c.Users)
f, err := os.ReadFile(c.Users)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/text v0.3.7
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
Expand Down Expand Up @@ -150,7 +151,6 @@ require (
go.etcd.io/bbolt v1.3.6 // indirect
go.mongodb.org/mongo-driver v1.8.3 // indirect
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -114,7 +113,7 @@ func registerMimeTypes(mappingFile string) error {
// TODO(lopresti) this function also exists in the storage provider, to be seen if we want to factor it out, though a
// fileext <-> mimetype "service" would have to be served by the gateway for it to be accessible both by storage providers and app providers.
if mappingFile != "" {
f, err := ioutil.ReadFile(mappingFile)
f, err := os.ReadFile(mappingFile)
if err != nil {
return fmt.Errorf("appprovider: error reading the custom mime types file: +%v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/grpc/services/datatx/datatx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/url"
"os"
"sync"
Expand Down Expand Up @@ -334,7 +334,7 @@ func (s *service) extractEndpointInfo(ctx context.Context, targetURL string) (*w
func loadOrCreate(file string) (*txShareModel, error) {
_, err := os.Stat(file)
if os.IsNotExist(err) {
if err := ioutil.WriteFile(file, []byte("{}"), 0700); err != nil {
if err := os.WriteFile(file, []byte("{}"), 0700); err != nil {
err = errors.Wrap(err, "datatx service: error creating the transfer shares storage file: "+file)
return nil, err
}
Expand All @@ -347,7 +347,7 @@ func loadOrCreate(file string) (*txShareModel, error) {
}
defer fd.Close()

data, err := ioutil.ReadAll(fd)
data, err := io.ReadAll(fd)
if err != nil {
err = errors.Wrap(err, "datatx service: error reading the data")
return nil, err
Expand All @@ -374,7 +374,7 @@ func (m *txShareModel) saveTxShare() error {
return err
}

if err := ioutil.WriteFile(m.File, data, 0644); err != nil {
if err := os.WriteFile(m.File, data, 0644); err != nil {
err = errors.Wrap(err, "datatx service: error writing transfer share data to file: "+m.File)
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/grpc/services/storageprovider/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -144,7 +143,7 @@ func parseConfig(m map[string]interface{}) (*config, error) {

func registerMimeTypes(mappingFile string) error {
if mappingFile != "" {
f, err := ioutil.ReadFile(mappingFile)
f, err := os.ReadFile(mappingFile)
if err != nil {
return fmt.Errorf("storageprovider: error reading the custom mime types file: +%v", err)
}
Expand Down
16 changes: 3 additions & 13 deletions internal/http/interceptors/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ func makeLogger(w http.ResponseWriter) loggingResponseWriter {
if _, ok := w.(http.Hijacker); ok {
logger = &hijackLogger{responseLogger{w: w, status: http.StatusOK}}
}
h, ok1 := logger.(http.Hijacker)
c, ok2 := w.(http.CloseNotifier)
if ok1 && ok2 {
return hijackCloseNotifier{logger, h, c}
}
if ok2 {
return &closeNotifyWriter{logger, c}
h, ok := logger.(http.Hijacker)
if ok {
return hijackCloseNotifier{logger, h}
}
return logger
}
Expand Down Expand Up @@ -183,13 +179,7 @@ func (l *hijackLogger) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return conn, rw, err
}

type closeNotifyWriter struct {
loggingResponseWriter
http.CloseNotifier
}

type hijackCloseNotifier struct {
loggingResponseWriter
http.Hijacker
http.CloseNotifier
}
4 changes: 2 additions & 2 deletions internal/http/services/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package reverseproxy

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"

ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/rhttp/global"
Expand Down Expand Up @@ -63,7 +63,7 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error)
}
conf.init()

f, err := ioutil.ReadFile(conf.ProxyRulesJSON)
f, err := os.ReadFile(conf.ProxyRulesJSON)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/app/provider/wopi/wopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -205,7 +204,7 @@ func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.Resourc
}
defer openRes.Body.Close()

body, err := ioutil.ReadAll(openRes.Body)
body, err := io.ReadAll(openRes.Body)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/appauth/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package json
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"os"
"sync"
"time"
Expand Down Expand Up @@ -100,7 +100,7 @@ func parseConfig(m map[string]interface{}) (*config, error) {
func loadOrCreate(file string) (*jsonManager, error) {
stat, err := os.Stat(file)
if os.IsNotExist(err) || stat.Size() == 0 {
if err = ioutil.WriteFile(file, []byte("{}"), 0644); err != nil {
if err = os.WriteFile(file, []byte("{}"), 0644); err != nil {
return nil, errors.Wrapf(err, "error creating the file %s", file)
}
}
Expand All @@ -111,7 +111,7 @@ func loadOrCreate(file string) (*jsonManager, error) {
}
defer fd.Close()

data, err := ioutil.ReadAll(fd)
data, err := io.ReadAll(fd)
if err != nil {
return nil, errors.Wrapf(err, "error reading the file %s", file)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (mgr *jsonManager) save() error {
return errors.Wrap(err, "error encoding json file")
}

if err = ioutil.WriteFile(mgr.config.File, data, 0644); err != nil {
if err = os.WriteFile(mgr.config.File, data, 0644); err != nil {
return errors.Wrapf(err, "error writing to file %s", mgr.config.File)
}

Expand Down
Loading

0 comments on commit ca02115

Please sign in to comment.