summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-09-18 14:41:25 +0300
committerPaul Buetow <paul@buetow.org>2021-10-02 12:26:29 +0300
commit6506e20f6c80f4acb7434eb9dd14f784a67189cd (patch)
tree1554831a5468729aa48c0baf3d5c9a78a8499288
parent7fbea88cf55af9b3354b4a1334e49c38d0d920fc (diff)
add spartan mode
-rw-r--r--TODO.md5
-rw-r--r--cmd/dcat/main.go28
-rw-r--r--cmd/dgrep/main.go28
-rw-r--r--cmd/dmap/main.go28
-rw-r--r--cmd/dserver/main.go4
-rw-r--r--cmd/dtail/main.go32
-rw-r--r--internal/clients/args.go34
-rw-r--r--internal/clients/baseclient.go3
-rw-r--r--internal/clients/catclient.go6
-rw-r--r--internal/clients/grepclient.go7
-rw-r--r--internal/clients/handlers/basehandler.go15
-rw-r--r--internal/clients/maprclient.go7
-rw-r--r--internal/clients/tailclient.go7
-rw-r--r--internal/config/read.go5
-rw-r--r--internal/io/fs/readfile.go13
-rw-r--r--internal/server/handlers/serverhandler.go31
16 files changed, 133 insertions, 120 deletions
diff --git a/TODO.md b/TODO.md
index d4f8312..7674577 100644
--- a/TODO.md
+++ b/TODO.md
@@ -20,3 +20,8 @@ This is a loose list of what to do. Maybe for the next releae or maybe for a lat
[x] By default connect to localhost
[x] Can use additional args as file lists
[ ] Document the two things above
+[x] Implement spartan mode
+[x] Make sure that diff is the same (plain file fs dcatted-file) in spartan mode
+[ ] document spartan mode
+[ ] Default client log dir is ~/log not ./log
+ [ ] Make sure dmap results aren't in color in local log file
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go
index 2ac773a..63b4b61 100644
--- a/cmd/dcat/main.go
+++ b/cmd/dcat/main.go
@@ -4,7 +4,6 @@ import (
"context"
"flag"
"os"
- "strings"
"github.com/mimecast/dtail/internal/clients"
"github.com/mimecast/dtail/internal/config"
@@ -20,16 +19,16 @@ func main() {
var cfgFile string
var debugEnable bool
var displayVersion bool
- var noColor bool
var sshPort int
userName := user.Name()
+ flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
+ flag.BoolVar(&args.Spartan, "spartan", false, "Spartan output mode")
flag.BoolVar(&args.TrustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
- flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
+ flag.BoolVar(&args.NoColor, "noColor", false, "Disable ANSII terminal colors")
flag.IntVar(&args.ConnectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method")
@@ -40,25 +39,16 @@ func main() {
flag.StringVar(&cfgFile, "cfg", "", "Config file path")
flag.Parse()
-
- if args.What == "" {
- // Interpret additional args as file list.
- var files []string
- for _, file := range flag.Args() {
- files = append(files, file)
- }
- args.What = strings.Join(files, ",")
- }
-
- config.Read(cfgFile, sshPort)
- if noColor {
- config.Client.TermColorsEnable = false
- }
+ args.Transform(flag.Args())
+ config.Read(cfgFile, sshPort, args.NoColor)
+ args.TransformAfterConfigFile()
if displayVersion {
version.PrintAndExit()
}
- version.Print()
+ if !args.Spartan {
+ version.Print()
+ }
ctx := context.TODO()
logger.Start(ctx, logger.Modes{
diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go
index 35b2236..7b96472 100644
--- a/cmd/dgrep/main.go
+++ b/cmd/dgrep/main.go
@@ -4,7 +4,6 @@ import (
"context"
"flag"
"os"
- "strings"
"github.com/mimecast/dtail/internal/clients"
"github.com/mimecast/dtail/internal/config"
@@ -21,17 +20,17 @@ func main() {
var debugEnable bool
var displayVersion bool
var grep string
- var noColor bool
var sshPort int
userName := user.Name()
+ flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
flag.BoolVar(&args.RegexInvert, "invert", false, "Invert regex")
+ flag.BoolVar(&args.Spartan, "spartan", false, "Spartan output mode")
flag.BoolVar(&args.TrustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
- flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
+ flag.BoolVar(&args.NoColor, "noColor", false, "Disable ANSII terminal colors")
flag.IntVar(&args.ConnectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method")
@@ -44,25 +43,16 @@ func main() {
flag.StringVar(&grep, "grep", "", "Alias for -regex")
flag.Parse()
-
- if args.What == "" {
- // Interpret additional args as file list.
- var files []string
- for _, file := range flag.Args() {
- files = append(files, file)
- }
- args.What = strings.Join(files, ",")
- }
-
- config.Read(cfgFile, sshPort)
- if noColor {
- config.Client.TermColorsEnable = false
- }
+ args.Transform(flag.Args())
+ config.Read(cfgFile, sshPort, args.NoColor)
+ args.TransformAfterConfigFile()
if displayVersion {
version.PrintAndExit()
}
- version.Print()
+ if !args.Spartan {
+ version.Print()
+ }
ctx := context.TODO()
logger.Start(ctx, logger.Modes{
diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go
index ab710ac..4525503 100644
--- a/cmd/dmap/main.go
+++ b/cmd/dmap/main.go
@@ -4,7 +4,6 @@ import (
"context"
"flag"
"os"
- "strings"
"github.com/mimecast/dtail/internal/clients"
"github.com/mimecast/dtail/internal/config"
@@ -20,7 +19,6 @@ func main() {
var cfgFile string
var debugEnable bool
var displayVersion bool
- var noColor bool
var queryStr string
var sshPort int
@@ -30,11 +28,12 @@ func main() {
userName := user.Name()
+ flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
+ flag.BoolVar(&args.Spartan, "spartan", false, "Spartan output mode")
flag.BoolVar(&args.TrustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
- flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
+ flag.BoolVar(&args.NoColor, "noColor", false, "Disable ANSII terminal colors")
flag.IntVar(&args.ConnectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&args.Timeout, "timeout", 0, "Max time dtail server will collect data until disconnection")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
@@ -47,25 +46,16 @@ func main() {
flag.StringVar(&queryStr, "query", "", "Map reduce query")
flag.Parse()
-
- if args.What == "" {
- // Interpret additional args as file list.
- var files []string
- for _, file := range flag.Args() {
- files = append(files, file)
- }
- args.What = strings.Join(files, ",")
- }
-
- config.Read(cfgFile, sshPort)
- if noColor {
- config.Client.TermColorsEnable = false
- }
+ args.Transform(flag.Args())
+ config.Read(cfgFile, sshPort, args.NoColor)
+ args.TransformAfterConfigFile()
if displayVersion {
version.PrintAndExit()
}
- version.Print()
+ if !args.Spartan {
+ version.Print()
+ }
ctx := context.TODO()
logger.Start(ctx, logger.Modes{
diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go
index 1fe77a7..bc3cb91 100644
--- a/cmd/dserver/main.go
+++ b/cmd/dserver/main.go
@@ -43,8 +43,8 @@ func main() {
flag.StringVar(&logDir, "logDir", "", "Log dir path")
flag.Parse()
- config.Read(cfgFile, sshPort)
- config.Client.TermColorsEnable = color
+ config.Read(cfgFile, sshPort, !color)
+
if logDir != "" {
config.Common.LogDir = logDir
if config.Common.LogStrategy == "" {
diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go
index 3c3443f..5c2d393 100644
--- a/cmd/dtail/main.go
+++ b/cmd/dtail/main.go
@@ -8,7 +8,6 @@ import (
_ "net/http"
_ "net/http/pprof"
"os"
- "strings"
"time"
"github.com/mimecast/dtail/internal/clients"
@@ -30,7 +29,6 @@ func main() {
var displayColorTable bool
var displayVersion bool
var grep string
- var noColor bool
var pprof int
var queryStr string
var shutdownAfter int
@@ -40,12 +38,13 @@ func main() {
flag.BoolVar(&args.Quiet, "quiet", false, "Quiet output mode")
flag.BoolVar(&args.RegexInvert, "invert", false, "Invert regex")
+ flag.BoolVar(&args.Spartan, "spartan", false, "Spartan output mode")
flag.BoolVar(&args.TrustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.BoolVar(&checkHealth, "checkHealth", false, "Only check for server health")
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayColorTable, "colorTable", false, "Show color table")
flag.BoolVar(&displayVersion, "version", false, "Display version")
- flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
+ flag.BoolVar(&args.NoColor, "noColor", false, "Disable ANSII terminal colors")
flag.IntVar(&args.ConnectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&args.Timeout, "timeout", 0, "Max time dtail server will collect data until disconnection")
flag.IntVar(&pprof, "pprof", -1, "Start PProf server this port")
@@ -62,32 +61,21 @@ func main() {
flag.StringVar(&queryStr, "query", "", "Map reduce query")
flag.Parse()
-
- if args.What == "" {
- // Interpret additional args as file list.
- var files []string
- for _, file := range flag.Args() {
- files = append(files, file)
- }
- args.What = strings.Join(files, ",")
- }
-
if grep != "" {
args.RegexStr = grep
}
-
- config.Read(cfgFile, sshPort)
- if noColor {
- config.Client.TermColorsEnable = false
- }
+ args.Transform(flag.Args())
+ config.Read(cfgFile, sshPort, args.NoColor)
+ args.TransformAfterConfigFile()
if displayVersion {
version.PrintAndExit()
}
- version.Print()
-
- if displayColorTable {
- color.TablePrintAndExit(debugEnable)
+ if !args.Spartan {
+ version.Print()
+ if displayColorTable {
+ color.TablePrintAndExit(debugEnable)
+ }
}
ctx, cancel := context.WithCancel(context.Background())
diff --git a/internal/clients/args.go b/internal/clients/args.go
index 7ce1634..081b911 100644
--- a/internal/clients/args.go
+++ b/internal/clients/args.go
@@ -1,6 +1,7 @@
package clients
import (
+ "flag"
"fmt"
"os"
"path/filepath"
@@ -30,14 +31,35 @@ type Args struct {
SSHHostKeyCallback gossh.HostKeyCallback
PrivateKeyPathFile string
Quiet bool
+ Spartan bool
+ NoColor bool
}
-// When no servers are given, connect to localhost!
-func (a *Args) handleEmptyServer() {
- if a.Discovery != "" || a.ServersStr != "" {
- return
+// Transform the arguments based on certain conditions.
+func (a *Args) Transform(args []string) {
+ // Interpret additional args as file list.
+ if a.What == "" {
+ var files []string
+ for _, file := range flag.Args() {
+ files = append(files, file)
+ }
+ a.What = strings.Join(files, ",")
+ }
+
+ if a.Spartan {
+ a.Quiet = true
+ a.NoColor = true
}
+}
+// TransformAfterConfigFile same as Transform, but after the config file has been read.
+func (a *Args) TransformAfterConfigFile() {
+ if a.Discovery == "" && a.ServersStr == "" {
+ a.handleEmptyServer()
+ }
+}
+
+func (a *Args) handleEmptyServer() {
fqdn, err := os.Hostname()
if err != nil {
logger.FatalExit(err)
@@ -68,3 +90,7 @@ func (a *Args) handleEmptyServer() {
a.What = strings.Join(filePaths, ",")
logger.Debug("Clean file paths", a.What)
}
+
+func (a *Args) SerializeOptions() string {
+ return fmt.Sprintf("quiet=%v:spartan=%v", a.Quiet, a.Spartan)
+}
diff --git a/internal/clients/baseclient.go b/internal/clients/baseclient.go
index 455174e..de0c101 100644
--- a/internal/clients/baseclient.go
+++ b/internal/clients/baseclient.go
@@ -41,9 +41,6 @@ type baseClient struct {
func (c *baseClient) init() {
logger.Debug("Initiating base client")
- // When empty server list provided, connect to localhost by default.
- c.Args.handleEmptyServer()
-
flag := regex.Default
if c.Args.RegexInvert {
flag = regex.Invert
diff --git a/internal/clients/catclient.go b/internal/clients/catclient.go
index b7b6131..d14cdcc 100644
--- a/internal/clients/catclient.go
+++ b/internal/clients/catclient.go
@@ -42,9 +42,11 @@ func (c CatClient) makeHandler(server string) handlers.Handler {
}
func (c CatClient) makeCommands() (commands []string) {
- options := fmt.Sprintf("quiet=%v", c.Args.Quiet)
for _, file := range strings.Split(c.What, ",") {
- commands = append(commands, fmt.Sprintf("%s:%s %s %s", c.Mode.String(), options, file, c.Regex.Serialize()))
+ commands = append(commands, fmt.Sprintf("%s:%s %s %s",
+ c.Mode.String(),
+ c.Args.SerializeOptions(),
+ file, c.Regex.Serialize()))
}
return
}
diff --git a/internal/clients/grepclient.go b/internal/clients/grepclient.go
index 652c31b..ea5022b 100644
--- a/internal/clients/grepclient.go
+++ b/internal/clients/grepclient.go
@@ -41,9 +41,12 @@ func (c GrepClient) makeHandler(server string) handlers.Handler {
}
func (c GrepClient) makeCommands() (commands []string) {
- options := fmt.Sprintf("quiet=%v", c.Args.Quiet)
for _, file := range strings.Split(c.What, ",") {
- commands = append(commands, fmt.Sprintf("%s:%s %s %s", c.Mode.String(), options, file, c.Regex.Serialize()))
+ commands = append(commands, fmt.Sprintf("%s:%s %s %s",
+ c.Mode.String(),
+ c.Args.SerializeOptions(),
+ file,
+ c.Regex.Serialize()))
}
return
diff --git a/internal/clients/handlers/basehandler.go b/internal/clients/handlers/basehandler.go
index 74559e9..0f2d1b5 100644
--- a/internal/clients/handlers/basehandler.go
+++ b/internal/clients/handlers/basehandler.go
@@ -67,9 +67,12 @@ func (h *baseHandler) Write(p []byte) (n int, err error) {
*/
case '\n', protocol.MessageDelimiter:
message := h.receiveBuf.String()
- if len(message) == 0 {
- continue
- }
+ /*
+ // dcat/grep should actually display empty lines.
+ if len(message) == 0 {
+ continue
+ }
+ */
h.handleMessageType(message)
h.receiveBuf.Reset()
default:
@@ -93,12 +96,8 @@ func (h *baseHandler) Read(p []byte) (n int, err error) {
// Handle various message types.
func (h *baseHandler) handleMessageType(message string) {
- if len(message) == 0 {
- return
- }
-
// Hidden server commands starti with a dot "."
- if message[0] == '.' {
+ if len(message) > 0 && message[0] == '.' {
h.handleHiddenMessage(message)
return
}
diff --git a/internal/clients/maprclient.go b/internal/clients/maprclient.go
index 68352ea..e6ab96b 100644
--- a/internal/clients/maprclient.go
+++ b/internal/clients/maprclient.go
@@ -116,7 +116,6 @@ func (c MaprClient) makeHandler(server string) handlers.Handler {
func (c MaprClient) makeCommands() (commands []string) {
commands = append(commands, fmt.Sprintf("map %s", c.query.RawQuery))
- options := fmt.Sprintf("quiet=%v", c.Args.Quiet)
modeStr := "cat"
if c.Mode == omode.TailClient {
@@ -128,7 +127,11 @@ func (c MaprClient) makeCommands() (commands []string) {
commands = append(commands, fmt.Sprintf("timeout %d %s %s %s", c.Timeout, modeStr, file, c.Regex.Serialize()))
continue
}
- commands = append(commands, fmt.Sprintf("%s:%s %s %s", modeStr, options, file, c.Regex.Serialize()))
+ commands = append(commands, fmt.Sprintf("%s:%s %s %s",
+ modeStr,
+ c.Args.SerializeOptions(),
+ file,
+ c.Regex.Serialize()))
}
return
diff --git a/internal/clients/tailclient.go b/internal/clients/tailclient.go
index cefbaa7..360354b 100644
--- a/internal/clients/tailclient.go
+++ b/internal/clients/tailclient.go
@@ -38,9 +38,12 @@ func (c TailClient) makeHandler(server string) handlers.Handler {
}
func (c TailClient) makeCommands() (commands []string) {
- options := fmt.Sprintf("quiet=%v", c.Args.Quiet)
for _, file := range strings.Split(c.What, ",") {
- commands = append(commands, fmt.Sprintf("%s:%s %s %s", c.Mode.String(), options, file, c.Regex.Serialize()))
+ commands = append(commands, fmt.Sprintf("%s:%s %s %s",
+ c.Mode.String(),
+ c.Args.SerializeOptions(),
+ file,
+ c.Regex.Serialize()))
}
logger.Debug(commands)
diff --git a/internal/config/read.go b/internal/config/read.go
index a4e605b..ea358f8 100644
--- a/internal/config/read.go
+++ b/internal/config/read.go
@@ -5,7 +5,7 @@ import (
)
// Read the DTail configuration.
-func Read(configFile string, sshPort int) {
+func Read(configFile string, sshPort int, noColor bool) {
initializer := configInitializer{
Common: newDefaultCommonConfig(),
Server: newDefaultServerConfig(),
@@ -34,4 +34,7 @@ func Read(configFile string, sshPort int) {
if sshPort != 2222 {
Common.SSHPort = sshPort
}
+ if noColor {
+ Client.TermColorsEnable = false
+ }
}
diff --git a/internal/io/fs/readfile.go b/internal/io/fs/readfile.go
index c0d44dd..ec33c60 100644
--- a/internal/io/fs/readfile.go
+++ b/internal/io/fs/readfile.go
@@ -182,11 +182,14 @@ func (f readFile) read(ctx context.Context, fd *os.File, rawLines chan *bytes.Bu
switch b {
case '\n':
- if message.Len() == 0 {
- time.Sleep(time.Millisecond * 100)
- continue
- }
- message.WriteString("\n")
+ /*
+ // dcat/dgrep should actually transfer empty lines
+ if message.Len() == 0 {
+ time.Sleep(time.Millisecond * 100)
+ continue
+ }
+ */
+ //message.WriteString("\n")
select {
case rawLines <- message:
message = pool.BytesBuffer.Get().(*bytes.Buffer)
diff --git a/internal/server/handlers/serverhandler.go b/internal/server/handlers/serverhandler.go
index ed19412..2f3b73b 100644
--- a/internal/server/handlers/serverhandler.go
+++ b/internal/server/handlers/serverhandler.go
@@ -45,6 +45,7 @@ type ServerHandler struct {
ackCloseReceived chan struct{}
activeCommands int32
quiet bool
+ spartan bool
readBuf bytes.Buffer
writeBuf bytes.Buffer
}
@@ -118,16 +119,18 @@ func (h *ServerHandler) Read(p []byte) (n int, err error) {
n = copy(p, h.readBuf.Bytes())
case line := <-h.lines:
- h.readBuf.WriteString("REMOTE")
- h.readBuf.WriteString(protocol.FieldDelimiter)
- h.readBuf.WriteString(h.hostname)
- h.readBuf.WriteString(protocol.FieldDelimiter)
- h.readBuf.WriteString(fmt.Sprintf("%3d", line.TransmittedPerc))
- h.readBuf.WriteString(protocol.FieldDelimiter)
- h.readBuf.WriteString(fmt.Sprintf("%v", line.Count))
- h.readBuf.WriteString(protocol.FieldDelimiter)
- h.readBuf.WriteString(line.SourceID)
- h.readBuf.WriteString(protocol.FieldDelimiter)
+ if !h.spartan {
+ h.readBuf.WriteString("REMOTE")
+ h.readBuf.WriteString(protocol.FieldDelimiter)
+ h.readBuf.WriteString(h.hostname)
+ h.readBuf.WriteString(protocol.FieldDelimiter)
+ h.readBuf.WriteString(fmt.Sprintf("%3d", line.TransmittedPerc))
+ h.readBuf.WriteString(protocol.FieldDelimiter)
+ h.readBuf.WriteString(fmt.Sprintf("%v", line.Count))
+ h.readBuf.WriteString(protocol.FieldDelimiter)
+ h.readBuf.WriteString(line.SourceID)
+ h.readBuf.WriteString(protocol.FieldDelimiter)
+ }
h.readBuf.WriteString(line.Content.String())
h.readBuf.WriteByte(protocol.MessageDelimiter)
n = copy(p, h.readBuf.Bytes())
@@ -275,6 +278,12 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args []
h.quiet = true
}
}
+ if spartan, ok := options["spartan"]; ok {
+ if spartan == "true" {
+ logger.Debug(h.user, "Enabling spartan mode")
+ h.spartan = true
+ }
+ }
switch commandName {
case "grep", "cat":
@@ -397,6 +406,7 @@ func (h *ServerHandler) decrementActiveCommands() int32 {
}
func readOptions(opts []string) (map[string]string, error) {
+ logger.Debug("Parsing options", opts)
options := make(map[string]string, len(opts))
for _, o := range opts {
@@ -416,6 +426,7 @@ func readOptions(opts []string) (map[string]string, error) {
val = string(decoded)
}
+ logger.Debug("Setting option", key, val)
options[key] = val
}