summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2024-02-23 15:01:41 +0200
committerPaul Buetow <pbuetow@mimecast.com>2024-02-23 15:01:41 +0200
commita3e10757a52fa47a0608afd88986162ca5eb22cc (patch)
treedce61c6695bc3badd455a64767252e6947b32711
parent85780654df870dc4170b93a8ed5a5dbfa917fe5d (diff)
lint warnings
-rw-r--r--cmd/dcat/main.go6
-rw-r--r--cmd/dgrep/main.go6
-rw-r--r--cmd/dmap/main.go6
-rw-r--r--cmd/dserver/main.go6
-rw-r--r--cmd/dtail/main.go6
-rw-r--r--cmd/dtailhealth/main.go6
-rw-r--r--integrationtests/commandutils.go16
-rw-r--r--integrationtests/dserver_test.go4
-rw-r--r--integrationtests/dtail_test.go8
-rw-r--r--integrationtests/dtailhealth_test.go2
-rw-r--r--integrationtests/fileutils.go11
-rw-r--r--internal/clients/baseclient.go2
-rw-r--r--internal/clients/connectors/serverconnection.go18
-rw-r--r--internal/clients/connectors/serverless.go18
-rw-r--r--internal/clients/maprclient.go2
-rw-r--r--internal/clients/stats.go8
-rw-r--r--internal/config/initializer.go21
-rw-r--r--internal/io/dlog/dlog.go5
-rw-r--r--internal/io/dlog/loggers/file.go7
-rw-r--r--internal/io/dlog/loggers/stdout.go2
-rw-r--r--internal/io/fs/readfile.go20
-rw-r--r--internal/io/fs/readfilelcontext.go19
-rw-r--r--internal/mapr/client/aggregate.go2
-rw-r--r--internal/mapr/fieldtypes.go12
-rw-r--r--internal/mapr/groupsetresult.go36
-rw-r--r--internal/mapr/query.go2
-rw-r--r--internal/mapr/token.go3
-rw-r--r--internal/server/handlers/basehandler.go8
-rw-r--r--internal/server/server.go32
-rw-r--r--internal/ssh/client/clientkeypair.go3
-rw-r--r--internal/ssh/client/knownhostscallback.go8
-rw-r--r--internal/ssh/server/hostkey.go5
-rw-r--r--internal/ssh/server/publickeycallback.go3
-rw-r--r--internal/ssh/ssh.go7
-rw-r--r--internal/user/server/user.go6
-rw-r--r--internal/version/version.go2
36 files changed, 181 insertions, 147 deletions
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go
index 98da089..a50be51 100644
--- a/cmd/dcat/main.go
+++ b/cmd/dcat/main.go
@@ -59,8 +59,10 @@ func main() {
dlog.Start(ctx, &wg, source.Client)
if pprof != "" {
- go http.ListenAndServe(pprof, nil)
- dlog.Client.Info("Started PProf", pprof)
+ dlog.Client.Info("Starting PProf", pprof)
+ go func() {
+ panic(http.ListenAndServe(pprof, nil))
+ }()
}
client, err := clients.NewCatClient(args)
diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go
index 7c3cc3e..19f818b 100644
--- a/cmd/dgrep/main.go
+++ b/cmd/dgrep/main.go
@@ -69,8 +69,10 @@ func main() {
}
if pprof != "" {
- go http.ListenAndServe(pprof, nil)
- dlog.Client.Info("Started PProf", pprof)
+ dlog.Client.Info("Starting PProf", pprof)
+ go func() {
+ panic(http.ListenAndServe(pprof, nil))
+ }()
}
client, err := clients.NewGrepClient(args)
diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go
index e024b37..a8a52a2 100644
--- a/cmd/dmap/main.go
+++ b/cmd/dmap/main.go
@@ -64,8 +64,10 @@ func main() {
dlog.Start(ctx, &wg, source.Client)
if pprof != "" {
- go http.ListenAndServe(pprof, nil)
- dlog.Client.Info("Started PProf", pprof)
+ dlog.Client.Info("Starting PProf", pprof)
+ go func() {
+ panic(http.ListenAndServe(pprof, nil))
+ }()
}
client, err := clients.NewMaprClient(args, clients.DefaultMode)
diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go
index 14188e1..3377273 100644
--- a/cmd/dserver/main.go
+++ b/cmd/dserver/main.go
@@ -71,8 +71,10 @@ func main() {
dlog.Start(ctx, &wg, source.Server)
if pprof != "" {
- dlog.Server.Info("Starting PProf", pprof)
- go http.ListenAndServe(pprof, nil)
+ dlog.Client.Info("Starting PProf", pprof)
+ go func() {
+ panic(http.ListenAndServe(pprof, nil))
+ }()
}
serv := server.New()
diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go
index bd8eb87..e18923a 100644
--- a/cmd/dtail/main.go
+++ b/cmd/dtail/main.go
@@ -102,8 +102,10 @@ func main() {
}
if pprof != "" {
- go http.ListenAndServe(pprof, nil)
- dlog.Client.Info("Started PProf", pprof)
+ dlog.Client.Info("Starting PProf", pprof)
+ go func() {
+ panic(http.ListenAndServe(pprof, nil))
+ }()
}
var client clients.Client
diff --git a/cmd/dtailhealth/main.go b/cmd/dtailhealth/main.go
index 326c43a..a0ca84e 100644
--- a/cmd/dtailhealth/main.go
+++ b/cmd/dtailhealth/main.go
@@ -44,8 +44,10 @@ func main() {
dlog.Start(ctx, &wg, source.HealthCheck)
if pprof != "" {
- go http.ListenAndServe(pprof, nil)
- dlog.Client.Info("Started PProf", pprof)
+ dlog.Client.Info("Starting PProf", pprof)
+ go func() {
+ panic(http.ListenAndServe(pprof, nil))
+ }()
}
healthClient, _ := clients.NewHealthClient(args)
diff --git a/integrationtests/commandutils.go b/integrationtests/commandutils.go
index 959288a..8d81955 100644
--- a/integrationtests/commandutils.go
+++ b/integrationtests/commandutils.go
@@ -17,7 +17,7 @@ func runCommand(ctx context.Context, t *testing.T, stdoutFile, cmdStr string,
args ...string) (int, error) {
if _, err := os.Stat(cmdStr); err != nil {
- return 0, fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err)
+ return 0, fmt.Errorf("no such executable '%s', please compile first: %w", cmdStr, err)
}
t.Log("Creating stdout file", stdoutFile)
@@ -31,7 +31,7 @@ func runCommand(ctx context.Context, t *testing.T, stdoutFile, cmdStr string,
cmd := exec.CommandContext(ctx, cmdStr, args...)
out, err := cmd.CombinedOutput()
t.Log("Done running command!", err)
- fd.Write(out)
+ _, _ = fd.Write(out)
return exitCodeFromError(err), err
}
@@ -56,7 +56,7 @@ func startCommand(ctx context.Context, t *testing.T, inPipeFile,
if _, err := os.Stat(cmdStr); err != nil {
return stdoutCh, stderrCh, nil,
- fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err)
+ fmt.Errorf("no such executable '%s', please compile first: %w", cmdStr, err)
}
t.Log(cmdStr, strings.Join(args, " "))
@@ -85,13 +85,13 @@ func startCommand(ctx context.Context, t *testing.T, inPipeFile,
// Read input file and send to stdin pipe?
if inPipeFile != "" {
- t.Log(fmt.Sprintf("Piping %s to stdin pipe", inPipeFile))
+ t.Logf("Piping %s to stdin pipe", inPipeFile)
fd, err := os.Open(inPipeFile)
if err != nil {
return stdoutCh, stderrCh, nil, err
}
go func() {
- io.Copy(stdinPipe, bufio.NewReader(fd))
+ _, _ = io.Copy(stdinPipe, bufio.NewReader(fd))
stdinPipe.Close()
fd.Close()
}()
@@ -135,8 +135,8 @@ func waitForCommand(ctx context.Context, t *testing.T,
t.Log(line)
}
case cmdErr := <-cmdErrCh:
- t.Log(fmt.Sprintf("Command finished with with exit code %d: %v",
- exitCodeFromError(cmdErr), cmdErr))
+ t.Logf("Command finished with with exit code %d: %v",
+ exitCodeFromError(cmdErr), cmdErr)
return
}
}
@@ -150,5 +150,5 @@ func exitCodeFromError(err error) int {
ws := exitError.Sys().(syscall.WaitStatus)
return ws.ExitStatus()
}
- panic(fmt.Sprintf("Unable to get process exit code from error: %v", err))
+ panic(fmt.Errorf("Unable to get process exit code from error: %w", err))
}
diff --git a/integrationtests/dserver_test.go b/integrationtests/dserver_test.go
index 5b8a091..eaca23b 100644
--- a/integrationtests/dserver_test.go
+++ b/integrationtests/dserver_test.go
@@ -89,8 +89,8 @@ func TestDServer2(t *testing.T) {
parts := []string{"INFO", "19801011-424242", "1", "dserver_test.go",
"1", "1", "1", "1.0", "1m", "MAPREDUCE:INTEGRATIONTEST",
"foo=1", "bar=42"}
- fd.WriteString(strings.Join(parts, "|"))
- fd.WriteString("\n")
+ _, _ = fd.WriteString(strings.Join(parts, "|"))
+ _, _ = fd.WriteString("\n")
case <-ctx.Done():
return
}
diff --git a/integrationtests/dtail_test.go b/integrationtests/dtail_test.go
index 64a32f1..752dafb 100644
--- a/integrationtests/dtail_test.go
+++ b/integrationtests/dtail_test.go
@@ -75,8 +75,8 @@ func TestDTailWithServer(t *testing.T) {
for {
select {
case <-time.After(time.Second):
- fd.WriteString(time.Now().String())
- fd.WriteString(fmt.Sprintf(" - Hello %s\n", greetings[circular]))
+ _, _ = fd.WriteString(time.Now().String())
+ _, _ = fd.WriteString(fmt.Sprintf(" - Hello %s\n", greetings[circular]))
circular = (circular + 1) % len(greetings)
case <-ctx.Done():
return
@@ -120,8 +120,8 @@ func TestDTailWithServer(t *testing.T) {
for i, g := range greetingsRecv {
index := (i + offset) % len(greetings)
if greetings[index] != g {
- t.Error(fmt.Sprintf("Expected '%s' but got '%s' at '%v' vs '%v'\n",
- g, greetings[index], greetings, greetingsRecv))
+ t.Errorf("Expected '%s' but got '%s' at '%v' vs '%v'\n",
+ g, greetings[index], greetings, greetingsRecv)
return
}
}
diff --git a/integrationtests/dtailhealth_test.go b/integrationtests/dtailhealth_test.go
index 49fbd38..7946824 100644
--- a/integrationtests/dtailhealth_test.go
+++ b/integrationtests/dtailhealth_test.go
@@ -20,7 +20,7 @@ func TestDTailHealth1(t *testing.T) {
t.Log("Serverless check, is supposed to exit with warning state.")
exitCode, err := runCommand(context.TODO(), t, outFile, "../dtailhealth")
if exitCode != 1 {
- t.Error(fmt.Sprintf("Expected exit code '1' but got '%d': %v", exitCode, err))
+ t.Errorf("Expected exit code '1' but got '%d': %v", exitCode, err)
return
}
diff --git a/integrationtests/fileutils.go b/integrationtests/fileutils.go
index d13617d..a1e1051 100644
--- a/integrationtests/fileutils.go
+++ b/integrationtests/fileutils.go
@@ -5,7 +5,6 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
- "io/ioutil"
"os"
"os/exec"
"strings"
@@ -25,7 +24,7 @@ func mapFile(t *testing.T, file string) (map[string]int, error) {
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
- count, _ := contents[line]
+ count := contents[line]
contents[line] = count + 1
}
@@ -59,13 +58,11 @@ func compareFilesContents(t *testing.T, fileA, fileB string) error {
}
// The mapreduce result can be in a different order each time (Golang maps are not sorted).
- t.Log(fmt.Sprintf("Checking whether %s has same lines as file %s (ignoring line order)",
- fileA, fileB))
+ t.Logf("Checking whether %s has same lines as file %s (ignoring line order)", fileA, fileB)
if err := compareMaps(a, b); err != nil {
return err
}
- t.Log(fmt.Sprintf("Checking whether %s has same lines as file %s (ignoring line order)",
- fileB, fileA))
+ t.Logf("Checking whether %s has same lines as file %s (ignoring line order)", fileB, fileA)
if err := compareMaps(b, a); err != nil {
return err
}
@@ -108,7 +105,7 @@ func fileContainsStr(t *testing.T, file, str string) error {
}
func shaOfFile(t *testing.T, file string) string {
- bytes, err := ioutil.ReadFile(file)
+ bytes, err := os.ReadFile(file)
if err != nil {
t.Error(err)
}
diff --git a/internal/clients/baseclient.go b/internal/clients/baseclient.go
index 3025f72..013f2f2 100644
--- a/internal/clients/baseclient.go
+++ b/internal/clients/baseclient.go
@@ -20,8 +20,6 @@ type baseClient struct {
config.Args
// To display client side stats
stats *stats
- // List of remote servers to connect to.
- servers []string
// We have one connection per remote server.
connections []connectors.Connector
// SSH auth methods to use to connect to the remote servers.
diff --git a/internal/clients/connectors/serverconnection.go b/internal/clients/connectors/serverconnection.go
index aeb2a41..5c3d455 100644
--- a/internal/clients/connectors/serverconnection.go
+++ b/internal/clients/connectors/serverconnection.go
@@ -172,25 +172,31 @@ func (c *ServerConnection) handle(ctx context.Context, cancel context.CancelFunc
}
go func() {
- io.Copy(stdinPipe, c.handler)
- cancel()
+ defer cancel()
+ if _, err := io.Copy(stdinPipe, c.handler); err != nil {
+ dlog.Client.Trace(err)
+ }
}()
go func() {
- io.Copy(c.handler, stdoutPipe)
- cancel()
+ defer cancel()
+ if _, err := io.Copy(c.handler, stdoutPipe); err != nil {
+ dlog.Client.Trace(err)
+ }
}()
go func() {
+ defer cancel()
select {
case <-c.handler.Done():
case <-ctx.Done():
}
- cancel()
}()
// Send all commands to client.
for _, command := range c.commands {
dlog.Client.Debug(command)
- c.handler.SendMessage(command)
+ if err := c.handler.SendMessage(command); err != nil {
+ dlog.Client.Debug(err)
+ }
}
if !c.throttlingDone {
diff --git a/internal/clients/connectors/serverless.go b/internal/clients/connectors/serverless.go
index 431247a..631186a 100644
--- a/internal/clients/connectors/serverless.go
+++ b/internal/clients/connectors/serverless.go
@@ -83,29 +83,35 @@ func (s *Serverless) handle(ctx context.Context, cancel context.CancelFunc) erro
}
go func() {
- io.Copy(serverHandler, s.handler)
+ defer terminate()
+ if _, err := io.Copy(serverHandler, s.handler); err != nil {
+ dlog.Client.Trace(err)
+ }
dlog.Client.Trace("io.Copy(serverHandler, s.handler) => done")
- terminate()
}()
go func() {
- io.Copy(s.handler, serverHandler)
+ defer terminate()
+ if _, err := io.Copy(s.handler, serverHandler); err != nil {
+ dlog.Client.Trace(err)
+ }
dlog.Client.Trace("io.Copy(s.handler, serverHandler) => done")
- terminate()
}()
go func() {
+ defer terminate()
select {
case <-s.handler.Done():
dlog.Client.Trace("<-s.handler.Done()")
case <-ctx.Done():
dlog.Client.Trace("<-ctx.Done()")
}
- terminate()
}()
// Send all commands to client.
for _, command := range s.commands {
dlog.Client.Debug("Sending command to serverless server", command)
- s.handler.SendMessage(command)
+ if err := s.handler.SendMessage(command); err != nil {
+ dlog.Client.Debug(err)
+ }
}
<-ctx.Done()
diff --git a/internal/clients/maprclient.go b/internal/clients/maprclient.go
index 6362028..2bc66a4 100644
--- a/internal/clients/maprclient.go
+++ b/internal/clients/maprclient.go
@@ -82,7 +82,7 @@ func NewMaprClient(args config.Args, maprClientMode MaprClientMode) (*MaprClient
case "", ".":
c.RegexStr = "."
case "*":
- c.RegexStr = fmt.Sprintf("\\|MAPREDUCE:\\|")
+ c.RegexStr = "\\|MAPREDUCE:\\|"
default:
c.RegexStr = fmt.Sprintf("\\|MAPREDUCE:%s\\|", c.query.Table)
}
diff --git a/internal/clients/stats.go b/internal/clients/stats.go
index 2da3cf7..7a6643b 100644
--- a/internal/clients/stats.go
+++ b/internal/clients/stats.go
@@ -89,7 +89,7 @@ func (s *stats) printStatsDueInterrupt(messages []string) {
))
continue
}
- fmt.Println(fmt.Sprintf(" %s", message))
+ fmt.Printf(" %s\n", message)
}
time.Sleep(time.Second * time.Duration(config.InterruptTimeoutS))
dlog.Client.Resume()
@@ -128,12 +128,6 @@ func (s *stats) statsLine(connected, newConnections int, throttle int) string {
return sb.String()
}
-func (s *stats) numConnected() int {
- s.mutex.Lock()
- defer s.mutex.Unlock()
- return s.connected
-}
-
func percentOf(total float64, value float64) float64 {
if total == 0 || total == value {
return 100
diff --git a/internal/config/initializer.go b/internal/config/initializer.go
index 9724902..9c3bf64 100644
--- a/internal/config/initializer.go
+++ b/internal/config/initializer.go
@@ -4,7 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
- "io/ioutil"
+ "io"
"os"
"strings"
@@ -34,8 +34,11 @@ func (in *initializer) parseConfig(args *Args) error {
paths = append(paths, fmt.Sprintf("%s/.config/dtail/dtail.conf", homeDir))
paths = append(paths, fmt.Sprintf("%s/.dtail.conf", homeDir))
for _, configPath := range paths {
- if _, err := os.Stat(configPath); !os.IsNotExist(err) {
- in.parseSpecificConfig(configPath)
+ if _, err := os.Stat(configPath); os.IsNotExist(err) {
+ continue
+ }
+ if err := in.parseSpecificConfig(configPath); err != nil {
+ return err
}
}
}
@@ -46,17 +49,17 @@ func (in *initializer) parseConfig(args *Args) error {
func (in *initializer) parseSpecificConfig(configFile string) error {
fd, err := os.Open(configFile)
if err != nil {
- return fmt.Errorf("Unable to read config file: %v", err)
+ return fmt.Errorf("Unable to read config file: %w", err)
}
defer fd.Close()
- cfgBytes, err := ioutil.ReadAll(fd)
+ cfgBytes, err := io.ReadAll(fd)
if err != nil {
- return fmt.Errorf("Unable to read config file %s: %v", configFile, err)
+ return fmt.Errorf("Unable to read config file %s: %w", configFile, err)
}
if err := json.Unmarshal([]byte(cfgBytes), in); err != nil {
- return fmt.Errorf("Unable to parse config file %s: %v", configFile, err)
+ return fmt.Errorf("Unable to parse config file %s: %w", configFile, err)
}
return nil
@@ -116,7 +119,9 @@ func (in *initializer) setupConfig(sourceCb transformCb, args *Args,
}
setupLogDirectory(in)
- sourceCb(in, args, additionalArgs)
+ if err := sourceCb(in, args, additionalArgs); err != nil {
+ return err
+ }
if args.Plain {
setupPlainMode(in, args)
}
diff --git a/internal/io/dlog/dlog.go b/internal/io/dlog/dlog.go
index c5ab8f8..258fb68 100644
--- a/internal/io/dlog/dlog.go
+++ b/internal/io/dlog/dlog.go
@@ -3,7 +3,6 @@ package dlog
import (
"context"
"fmt"
- "io/ioutil"
"os"
"path/filepath"
"runtime"
@@ -225,14 +224,14 @@ func (d *DLog) Mapreduce(table string, data map[string]interface{}) string {
// level|date-time|process|caller|cpus|goroutines|cgocalls|loadavg|uptime|MAPREDUCE:TABLE|key=value|...
var loadAvg string
- if loadAvgBytes, err := ioutil.ReadFile("/proc/loadavg"); err == nil {
+ if loadAvgBytes, err := os.ReadFile("/proc/loadavg"); err == nil {
tmp := string(loadAvgBytes)
s := strings.SplitN(tmp, " ", 2)
loadAvg = s[0]
}
var uptime string
- if uptimeBytes, err := ioutil.ReadFile("/proc/uptime"); err == nil {
+ if uptimeBytes, err := os.ReadFile("/proc/uptime"); err == nil {
tmp := string(uptimeBytes)
s := strings.SplitN(tmp, ".", 2)
i, _ := strconv.ParseInt(s[0], 10, 64)
diff --git a/internal/io/dlog/loggers/file.go b/internal/io/dlog/loggers/file.go
index 6a09353..8e567bc 100644
--- a/internal/io/dlog/loggers/file.go
+++ b/internal/io/dlog/loggers/file.go
@@ -12,8 +12,6 @@ import (
"github.com/mimecast/dtail/internal/config"
)
-type fileWriter struct{}
-
type fileMessageBuf struct {
now time.Time
message string
@@ -124,9 +122,10 @@ func (f *file) write(m *fileMessageBuf) {
writer = f.getWriter(f.strategy.FileBase)
}
- writer.WriteString(m.message)
+ // Don't report any error, we won't be able to log it anyway!
+ _, _ = writer.WriteString(m.message)
if m.nl {
- writer.WriteByte('\n')
+ _ = writer.WriteByte('\n')
}
}
diff --git a/internal/io/dlog/loggers/stdout.go b/internal/io/dlog/loggers/stdout.go
index ef30855..b024243 100644
--- a/internal/io/dlog/loggers/stdout.go
+++ b/internal/io/dlog/loggers/stdout.go
@@ -69,4 +69,4 @@ func (s *stdout) Rotate() {
// This is empty because it isn't doing anything but has to satisfy the interface.
}
-func (stdout) SupportsColors() bool { return true }
+func (*stdout) SupportsColors() bool { return true }
diff --git a/internal/io/fs/readfile.go b/internal/io/fs/readfile.go
index 669f99f..dc1d8ea 100644
--- a/internal/io/fs/readfile.go
+++ b/internal/io/fs/readfile.go
@@ -116,23 +116,19 @@ func (f *readFile) makeReader() (*bufio.Reader, *os.File, error) {
return f.makeFileReader()
}
-func (f *readFile) makeFileReader() (*bufio.Reader, *os.File, error) {
- var reader *bufio.Reader
- fd, err := os.Open(f.filePath)
- if err != nil {
- return reader, fd, err
+func (f *readFile) makeFileReader() (reader *bufio.Reader, fd *os.File, err error) {
+ if fd, err = os.Open(f.filePath); err != nil {
+ return
}
if f.seekEOF {
- fd.Seek(0, io.SeekEnd)
+ if _, err = fd.Seek(0, io.SeekEnd); err != nil {
+ return
+ }
}
reader, err = f.makeCompressedFileReader(fd)
- if err != nil {
- return reader, fd, err
- }
-
- return reader, fd, nil
+ return
}
func (f *readFile) makePipeReader() (*bufio.Reader, *os.File, error) {
@@ -249,7 +245,7 @@ func (f *readFile) truncated(fd *os.File) (bool, error) {
dlog.Common.Debug(f.filePath, "File truncation check")
// Can not seek currently open FD.
- currentPosition, err := fd.Seek(0, os.SEEK_CUR)
+ currentPosition, err := fd.Seek(0, io.SeekCurrent)
if err != nil {
return true, err
}
diff --git a/internal/io/fs/readfilelcontext.go b/internal/io/fs/readfilelcontext.go
index 44ce17d..87d59d6 100644
--- a/internal/io/fs/readfilelcontext.go
+++ b/internal/io/fs/readfilelcontext.go
@@ -31,22 +31,17 @@ type ltxState struct {
func (f *readFile) filterWithoutLContext(ctx context.Context, rawLines <-chan *bytes.Buffer,
lines chan<- *line.Line, re regex.Regex) {
- for {
- select {
- case rawLine, ok := <-rawLines:
- f.updatePosition()
- if !ok {
+ for rawLine := range rawLines {
+ f.updatePosition()
+ if newLine, ok := f.transmittable(rawLine, len(lines), cap(lines), re); ok {
+ select {
+ case lines <- newLine:
+ case <-ctx.Done():
return
}
- if newLine, ok := f.transmittable(rawLine, len(lines), cap(lines), re); ok {
- select {
- case lines <- newLine:
- case <-ctx.Done():
- return
- }
- }
}
}
+ f.updatePosition()
}
// Filter log lines matching a given regular expression, however with local grep context.
diff --git a/internal/mapr/client/aggregate.go b/internal/mapr/client/aggregate.go
index 1704d43..2e9b61a 100644
--- a/internal/mapr/client/aggregate.go
+++ b/internal/mapr/client/aggregate.go
@@ -44,7 +44,7 @@ func (a *Aggregate) Aggregate(message string) error {
groupKey := parts[0]
samples, err := strconv.Atoi(parts[1])
if err != nil {
- return fmt.Errorf("unable to parse sample count '%s': %v", parts[1], err)
+ return fmt.Errorf("unable to parse sample count '%s': %w", parts[1], err)
}
fields := a.makeFields(parts[2:])
diff --git a/internal/mapr/fieldtypes.go b/internal/mapr/fieldtypes.go
index a64efd1..4242407 100644
--- a/internal/mapr/fieldtypes.go
+++ b/internal/mapr/fieldtypes.go
@@ -1,7 +1,5 @@
package mapr
-import "fmt"
-
type fieldType int
// The possible field types.
@@ -16,14 +14,14 @@ const (
func (w fieldType) String() string {
switch w {
case Field:
- return fmt.Sprintf("Field")
+ return "Field"
case String:
- return fmt.Sprintf("String")
+ return "String"
case Float:
- return fmt.Sprintf("Float")
+ return "Float"
case FunctionStack:
- return fmt.Sprintf("FunctionStack")
+ return "FunctionStack"
default:
- return fmt.Sprintf("UndefFieldType")
+ return "UndefFieldType"
}
}
diff --git a/internal/mapr/groupsetresult.go b/internal/mapr/groupsetresult.go
index d01a3c9..58663b8 100644
--- a/internal/mapr/groupsetresult.go
+++ b/internal/mapr/groupsetresult.go
@@ -169,9 +169,10 @@ func (*GroupSet) writeQueryFile(query *Query) error {
}
defer fd.Close()
- fd.WriteString(query.RawQuery)
- os.Rename(tmpQueryFile, queryFile)
- return nil
+ if _, err := fd.WriteString(query.RawQuery); err != nil {
+ return err
+ }
+ return os.Rename(tmpQueryFile, queryFile)
}
// WriteResult writes the result to an CSV outfile.
@@ -221,7 +222,9 @@ func (g *GroupSet) resultWriteUnformatted(query *Query, rows []result, fd *os.Fi
lastColumn := len(query.Select) - 1
if writeHeader {
- g.resultWriteUnformattedHeader(query, fd, lastColumn)
+ if err := g.resultWriteUnformattedHeader(query, fd, lastColumn); err != nil {
+ return err
+ }
}
// And now write the data
@@ -230,13 +233,19 @@ func (g *GroupSet) resultWriteUnformatted(query *Query, rows []result, fd *os.Fi
break
}
for j, value := range r.values {
- fd.WriteString(value)
+ if _, err := fd.WriteString(value); err != nil {
+ return err
+ }
if j == lastColumn {
continue
}
- fd.WriteString(protocol.CSVDelimiter)
+ if _, err := fd.WriteString(protocol.CSVDelimiter); err != nil {
+ return err
+ }
+ }
+ if _, err := fd.WriteString("\n"); err != nil {
+ return err
}
- fd.WriteString("\n")
}
if !query.Outfile.AppendMode {
@@ -250,13 +259,18 @@ func (g *GroupSet) resultWriteUnformatted(query *Query, rows []result, fd *os.Fi
return nil
}
-func (g *GroupSet) resultWriteUnformattedHeader(query *Query, fd *os.File, lastColumn int) {
+func (g *GroupSet) resultWriteUnformattedHeader(query *Query, fd *os.File, lastColumn int) (err error) {
for i, sc := range query.Select {
- fd.WriteString(sc.FieldStorage)
+ if _, err = fd.WriteString(sc.FieldStorage); err != nil {
+ return
+ }
if i == lastColumn {
continue
}
- fd.WriteString(protocol.CSVDelimiter)
+ if _, err = fd.WriteString(protocol.CSVDelimiter); err != nil {
+ return
+ }
}
- fd.WriteString("\n")
+ _, err = fd.WriteString("\n")
+ return
}
diff --git a/internal/mapr/query.go b/internal/mapr/query.go
index ddcbc90..139f04c 100644
--- a/internal/mapr/query.go
+++ b/internal/mapr/query.go
@@ -133,7 +133,7 @@ func (q *Query) parseTokens(tokens []token) ([]token, error) {
var err error
var found []token
- for tokens != nil && len(tokens) > 0 {
+ for len(tokens) > 0 {
switch strings.ToLower(tokens[0].str) {
case "select":
tokens, found = tokensConsume(tokens[1:])
diff --git a/internal/mapr/token.go b/internal/mapr/token.go
index 48d1192..77362f7 100644
--- a/internal/mapr/token.go
+++ b/internal/mapr/token.go
@@ -99,7 +99,8 @@ func tokensConsumeOptional(tokens []token, optional string) []token {
if len(tokens) < 1 {
return tokens
}
- if strings.ToLower(tokens[0].str) == strings.ToLower(optional) {
+ //if strings.ToLower(tokens[0].str) == strings.ToLower(optional) {
+ if strings.EqualFold(tokens[0].str, optional) {
return tokens[1:]
}
return tokens
diff --git a/internal/server/handlers/basehandler.go b/internal/server/handlers/basehandler.go
index 7daf071..f6ab3db 100644
--- a/internal/server/handlers/basehandler.go
+++ b/internal/server/handlers/basehandler.go
@@ -130,7 +130,7 @@ func (h *baseHandler) Write(p []byte) (n int, err error) {
for _, b := range p {
switch b {
case ';':
- h.handleCommand(string(h.writeBuf.Bytes()))
+ h.handleCommand(h.writeBuf.String())
h.writeBuf.Reset()
default:
h.writeBuf.WriteByte(b)
@@ -252,15 +252,15 @@ func (h *baseHandler) handleOptions(options map[string]string) {
// We can read the options only once, will cause a data race otherwise if
// changed multiple times for multiple incoming commands.
h.once.Do(func() {
- if quiet, _ := options["quiet"]; quiet == "true" {
+ if quiet := options["quiet"]; quiet == "true" {
dlog.Server.Debug(h.user, "Enabling quiet mode")
h.quiet = true
}
- if plain, _ := options["plain"]; plain == "true" {
+ if plain := options["plain"]; plain == "true" {
dlog.Server.Debug(h.user, "Enabling plain mode")
h.plain = true
}
- if serverless, _ := options["serverless"]; serverless == "true" {
+ if serverless := options["serverless"]; serverless == "true" {
dlog.Server.Debug(h.user, "Enabling serverless mode")
h.serverless = true
}
diff --git a/internal/server/server.go b/internal/server/server.go
index 761880d..8b581b1 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -130,7 +130,9 @@ func (s *Server) handleChannel(ctx context.Context, sshConn gossh.Conn,
user, err := user.New(sshConn.User(), sshConn.RemoteAddr().String())
if err != nil {
dlog.Server.Error(user, err)
- newChannel.Reject(gossh.Prohibited, err.Error())
+ if err := newChannel.Reject(gossh.Prohibited, err.Error()); err != nil {
+ dlog.Server.Debug(err)
+ }
return
}
@@ -138,7 +140,9 @@ func (s *Server) handleChannel(ctx context.Context, sshConn gossh.Conn,
if newChannel.ChannelType() != "session" {
err := errors.New("Don'w allow other channel types than session")
dlog.Server.Error(user, err)
- newChannel.Reject(gossh.Prohibited, err.Error())
+ if err := newChannel.Reject(gossh.Prohibited, err.Error()); err != nil {
+ dlog.Server.Debug(err)
+ }
return
}
@@ -160,7 +164,9 @@ func (s *Server) handleRequests(ctx context.Context, sshConn gossh.Conn,
dlog.Server.Info(user, "Invoking request handler")
for req := range in {
var payload = struct{ Value string }{}
- gossh.Unmarshal(req.Payload, &payload)
+ if err := gossh.Unmarshal(req.Payload, &payload); err != nil {
+ dlog.Server.Error(user, err)
+ }
switch req.Type {
case "shell":
@@ -177,14 +183,18 @@ func (s *Server) handleRequests(ctx context.Context, sshConn gossh.Conn,
}
go func() {
+ defer terminate()
// Broken pipe, cancel
- io.Copy(channel, handler)
- terminate()
+ if _, err := io.Copy(channel, handler); err != nil {
+ dlog.Server.Trace(user, fmt.Errorf("channel->handler: %w", err))
+ }
}()
go func() {
+ defer terminate()
// Broken pipe, cancel
- io.Copy(handler, channel)
- terminate()
+ if _, err := io.Copy(handler, channel); err != nil {
+ dlog.Server.Trace(user, fmt.Errorf("handler->channel: %w", err))
+ }
}()
go func() {
select {
@@ -203,9 +213,13 @@ func (s *Server) handleRequests(ctx context.Context, sshConn gossh.Conn,
}()
// Only serving shell type
- req.Reply(true, nil)
+ if err := req.Reply(true, nil); err != nil {
+ dlog.Server.Trace(user, fmt.Errorf("reply(true): %w", err))
+ }
default:
- req.Reply(false, nil)
+ if err := req.Reply(false, nil); err != nil {
+ dlog.Server.Trace(user, fmt.Errorf("reply(false): %w", err))
+ }
return fmt.Errorf("Closing SSH connection as unknown request received|%s|%v",
req.Type, payload.Value)
}
diff --git a/internal/ssh/client/clientkeypair.go b/internal/ssh/client/clientkeypair.go
index b35b25d..20cdc88 100644
--- a/internal/ssh/client/clientkeypair.go
+++ b/internal/ssh/client/clientkeypair.go
@@ -6,7 +6,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
- "io/ioutil"
"os"
"github.com/mimecast/dtail/internal/io/dlog"
@@ -83,7 +82,7 @@ func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) {
}
func writeKey(keyBytes []byte, saveFileTo string) error {
- err := ioutil.WriteFile(saveFileTo, keyBytes, 0600)
+ err := os.WriteFile(saveFileTo, keyBytes, 0600)
if err != nil {
return err
}
diff --git a/internal/ssh/client/knownhostscallback.go b/internal/ssh/client/knownhostscallback.go
index 393c4c7..fe3543c 100644
--- a/internal/ssh/client/knownhostscallback.go
+++ b/internal/ssh/client/knownhostscallback.go
@@ -232,8 +232,12 @@ func (c KnownHostsCallback) trustHosts(hosts []unknownHost) {
// And once as [IP]:PORT
addresses[knownhosts.Normalize(unknown.remote.String())] = struct{}{}
- newFd.WriteString(fmt.Sprintf("%s\n", unknown.hostLine))
- newFd.WriteString(fmt.Sprintf("%s\n", unknown.ipLine))
+ if _, err := newFd.WriteString(fmt.Sprintf("%s\n", unknown.hostLine)); err != nil {
+ panic(err)
+ }
+ if _, err := newFd.WriteString(fmt.Sprintf("%s\n", unknown.ipLine)); err != nil {
+ panic(err)
+ }
}
// Read old known hosts file, to see which are old and new entries
diff --git a/internal/ssh/server/hostkey.go b/internal/ssh/server/hostkey.go
index be23d85..b2d4569 100644
--- a/internal/ssh/server/hostkey.go
+++ b/internal/ssh/server/hostkey.go
@@ -1,7 +1,6 @@
package server
import (
- "io/ioutil"
"os"
"github.com/mimecast/dtail/internal/config"
@@ -26,7 +25,7 @@ func PrivateHostKey() []byte {
}
pem := ssh.EncodePrivateKeyToPEM(privateKey)
- if err := ioutil.WriteFile(hostKeyFile, pem, 0600); err != nil {
+ if err := os.WriteFile(hostKeyFile, pem, 0600); err != nil {
dlog.Server.Error("Unable to write private server RSA host key to file",
hostKeyFile, err)
}
@@ -34,7 +33,7 @@ func PrivateHostKey() []byte {
}
dlog.Server.Info("Reading private server RSA host key from file", hostKeyFile)
- pem, err := ioutil.ReadFile(hostKeyFile)
+ pem, err := os.ReadFile(hostKeyFile)
if err != nil {
dlog.Server.FatalPanic("Failed to load private server RSA host key", err)
}
diff --git a/internal/ssh/server/publickeycallback.go b/internal/ssh/server/publickeycallback.go
index f7655b4..bcc9004 100644
--- a/internal/ssh/server/publickeycallback.go
+++ b/internal/ssh/server/publickeycallback.go
@@ -2,7 +2,6 @@ package server
import (
"fmt"
- "io/ioutil"
"os"
goUser "os/user"
@@ -30,7 +29,7 @@ func PublicKeyCallback(c gossh.ConnMetadata,
}
dlog.Server.Info(user, "Reading", authorizedKeysFile)
- authorizedKeysBytes, err := ioutil.ReadFile(authorizedKeysFile)
+ authorizedKeysBytes, err := os.ReadFile(authorizedKeysFile)
if err != nil {
return nil, fmt.Errorf("Unable to read authorized keys file|%s|%s|%s",
authorizedKeysFile, user, err.Error())
diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go
index db5aaf1..9c2dcb8 100644
--- a/internal/ssh/ssh.go
+++ b/internal/ssh/ssh.go
@@ -6,7 +6,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
- "io/ioutil"
"net"
"os"
"syscall"
@@ -15,7 +14,7 @@ import (
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
- "golang.org/x/crypto/ssh/terminal"
+ "golang.org/x/term"
)
// GeneratePrivateRSAKey is used by the server to generate its key.
@@ -63,7 +62,7 @@ func Agent() (gossh.AuthMethod, error) {
// EnterKeyPhrase is required to read phrase protected private keys.
func EnterKeyPhrase(keyFile string) []byte {
fmt.Printf("Enter phrase for key %s: ", keyFile)
- phrase, err := terminal.ReadPassword(int(syscall.Stdin))
+ phrase, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
panic(err)
}
@@ -73,7 +72,7 @@ func EnterKeyPhrase(keyFile string) []byte {
// KeyFile returns the key as a SSH auth method.
func KeyFile(keyFile string) (gossh.AuthMethod, error) {
- buffer, err := ioutil.ReadFile(keyFile)
+ buffer, err := os.ReadFile(keyFile)
if err != nil {
return nil, err
}
diff --git a/internal/user/server/user.go b/internal/user/server/user.go
index abf74f3..d391672 100644
--- a/internal/user/server/user.go
+++ b/internal/user/server/user.go
@@ -81,7 +81,7 @@ func (u *User) HasFilePermission(filePath, permissionType string) (hasPermission
func (u *User) hasFilePermission(cleanPath, permissionType string) (bool, error) {
// First check file system Linux/UNIX permission.
if _, err := permissions.ToRead(u.Name, cleanPath); err != nil {
- return false, fmt.Errorf("User without OS file system permissions to read path: '%v'", err)
+ return false, fmt.Errorf("User without OS file system permissions to read path: %w", err)
}
dlog.Server.Info(u, cleanPath, permissionType,
"User with OS file system permissions to path")
@@ -89,7 +89,7 @@ func (u *User) hasFilePermission(cleanPath, permissionType string) (bool, error)
// Only allow to follow regular files or symlinks.
info, err := os.Lstat(cleanPath)
if err != nil {
- return false, fmt.Errorf("Unable to determine file type: '%v'", err)
+ return false, fmt.Errorf("Unable to determine file type: %w", err)
}
if !info.Mode().IsRegular() {
return false, fmt.Errorf("Can only open regular files or follow symlinks")
@@ -130,7 +130,7 @@ func (u *User) iteratePaths(cleanPath, permissionType string) (bool, error) {
re, err := regexp.Compile(regexStr)
if err != nil {
return false, fmt.Errorf("Permission test failed, can't compile regex "+
- "'%s': '%v'", regexStr, err)
+ "'%s': %w", regexStr, err)
}
if negate && re.MatchString(cleanPath) {
dlog.Server.Info(u, cleanPath, "Permission test failed partially, "+
diff --git a/internal/version/version.go b/internal/version/version.go
index 15ea50f..c8ad394 100644
--- a/internal/version/version.go
+++ b/internal/version/version.go
@@ -13,7 +13,7 @@ const (
// Name of DTail.
Name string = "DTail"
// Version of DTail.
- Version string = "4.3.0"
+ Version string = "4.3.2"
// Additional information for DTail
Additional string = "Have a lot of fun!"
)