summaryrefslogtreecommitdiff
path: root/internal/clients/grepclient.go
diff options
context:
space:
mode:
authorPaul Bütow <pbuetow@mimecast.com>2020-01-20 18:41:05 +0000
committerPaul Bütow <pbuetow@mimecast.com>2020-01-21 14:35:23 +0000
commitc128865c4c7411c29a59fca9a3a2f95537686d7b (patch)
tree193bccc70d942c8b70cc93fae2670263701e43aa /internal/clients/grepclient.go
parent3755a9911ecb05886577095f2b8cc8b9e4066a3a (diff)
Move commands to cmd/ and move internal dependencies to internal/
Diffstat (limited to 'internal/clients/grepclient.go')
-rw-r--r--internal/clients/grepclient.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/clients/grepclient.go b/internal/clients/grepclient.go
new file mode 100644
index 0000000..c568f63
--- /dev/null
+++ b/internal/clients/grepclient.go
@@ -0,0 +1,53 @@
+package clients
+
+import (
+ "errors"
+ "fmt"
+ "runtime"
+ "strings"
+
+ "github.com/mimecast/dtail/internal/clients/handlers"
+ "github.com/mimecast/dtail/internal/clients/remote"
+ "github.com/mimecast/dtail/internal/omode"
+ "github.com/mimecast/dtail/internal/ssh/client"
+
+ gossh "golang.org/x/crypto/ssh"
+)
+
+// GrepClient searches a remote file for all lines matching a regular expression. Only the matching lines are displayed.
+type GrepClient struct {
+ baseClient
+}
+
+// NewGrepClient creates a new grep client.
+func NewGrepClient(args Args) (*GrepClient, error) {
+ if args.Regex == "" {
+ return nil, errors.New("No regex specified, use '-regex' flag")
+ }
+ args.Mode = omode.GrepClient
+
+ c := GrepClient{
+ baseClient: baseClient{
+ Args: args,
+ stop: make(chan struct{}),
+ stopped: make(chan struct{}),
+ throttleCh: make(chan struct{}, args.ConnectionsPerCPU*runtime.NumCPU()),
+ retry: false,
+ },
+ }
+
+ c.init(c)
+
+ return &c, nil
+}
+
+func (c GrepClient) makeConnection(server string, sshAuthMethods []gossh.AuthMethod, hostKeyCallback *client.HostKeyCallback) *remote.Connection {
+ conn := remote.NewConnection(server, c.UserName, sshAuthMethods, hostKeyCallback)
+ conn.Handler = handlers.NewClientHandler(server, c.PingTimeout)
+
+ for _, file := range strings.Split(c.Files, ",") {
+ conn.Commands = append(conn.Commands, fmt.Sprintf("%s %s regex %s", c.Mode.String(), file, c.Regex))
+ }
+
+ return conn
+}