Browse Source

fix: Make console output configurable for CLI tools

Matthias Ladkau 3 years ago
parent
commit
ad2b7ae72a
3 changed files with 36 additions and 19 deletions
  1. 16 7
      cli/tool/debug.go
  2. 10 6
      cli/tool/interpret.go
  3. 10 6
      cli/tool/pack.go

+ 16 - 7
cli/tool/debug.go

@@ -17,7 +17,9 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
+	"io"
 	"net"
+	"os"
 	"strings"
 	"time"
 
@@ -41,13 +43,17 @@ type CLIDebugInterpreter struct {
 	Interactive     *bool   // Flag if the interpreter should open a console in the current tty.
 	BreakOnStart    *bool   // Flag if the debugger should stop the execution on start
 	BreakOnError    *bool   // Flag if the debugger should stop when encountering an error
+
+	// Log output
+
+	LogOut io.Writer
 }
 
 /*
 NewCLIDebugInterpreter wraps an existing CLIInterpreter object and adds capabilities.
 */
 func NewCLIDebugInterpreter(i *CLIInterpreter) *CLIDebugInterpreter {
-	return &CLIDebugInterpreter{i, nil, nil, nil, nil, nil, nil}
+	return &CLIDebugInterpreter{i, nil, nil, nil, nil, nil, nil, os.Stdout}
 }
 
 /*
@@ -241,13 +247,14 @@ HandleConnection handles an incoming connection.
 func (s *debugTelnetServer) HandleConnection(conn net.Conn) {
 	tid := s.interpreter.RuntimeProvider.NewThreadID()
 	inputReader := bufio.NewReader(conn)
-	outputTerminal := OutputTerminal(&bufioWriterShim{fmt.Sprint(conn.RemoteAddr()), bufio.NewWriter(conn), s.echo})
+	outputTerminal := OutputTerminal(&bufioWriterShim{fmt.Sprint(conn.RemoteAddr()),
+		bufio.NewWriter(conn), s.echo, s.interpreter.LogOut})
 
 	line := ""
 
 	s.logger.LogDebug(s.logPrefix, "Connect ", conn.RemoteAddr())
 	if s.echo {
-		fmt.Println(fmt.Sprintf("%v : Connected", conn.RemoteAddr()))
+		fmt.Fprintln(s.interpreter.LogOut, fmt.Sprintf("%v : Connected", conn.RemoteAddr()))
 	}
 
 	for {
@@ -258,7 +265,7 @@ func (s *debugTelnetServer) HandleConnection(conn net.Conn) {
 			line = strings.TrimSpace(line)
 
 			if s.echo {
-				fmt.Println(fmt.Sprintf("%v > %v", conn.RemoteAddr(), line))
+				fmt.Fprintln(s.interpreter.LogOut, fmt.Sprintf("%v > %v", conn.RemoteAddr(), line))
 			}
 
 			if line == "exit" || line == "q" || line == "quit" || line == "bye" || line == "\x04" {
@@ -270,7 +277,8 @@ func (s *debugTelnetServer) HandleConnection(conn net.Conn) {
 			if !s.interpreter.CanHandle(line) || isHelpTable {
 				buffer := bytes.NewBuffer(nil)
 
-				s.interpreter.HandleInput(&bufioWriterShim{"tmpbuffer", bufio.NewWriter(buffer), false}, line, tid)
+				s.interpreter.HandleInput(&bufioWriterShim{"tmpbuffer",
+					bufio.NewWriter(buffer), false, s.interpreter.LogOut}, line, tid)
 
 				if isHelpTable {
 
@@ -300,7 +308,7 @@ func (s *debugTelnetServer) HandleConnection(conn net.Conn) {
 
 		if err != nil {
 			if s.echo {
-				fmt.Println(fmt.Sprintf("%v : Disconnected", conn.RemoteAddr()))
+				fmt.Fprintln(s.interpreter.LogOut, fmt.Sprintf("%v : Disconnected", conn.RemoteAddr()))
 			}
 			s.logger.LogDebug(s.logPrefix, "Disconnect ", conn.RemoteAddr(), " - ", err)
 			break
@@ -317,6 +325,7 @@ type bufioWriterShim struct {
 	id     string
 	writer *bufio.Writer
 	echo   bool
+	logOut io.Writer
 }
 
 /*
@@ -324,7 +333,7 @@ WriteString write a string to the writer.
 */
 func (shim *bufioWriterShim) WriteString(s string) {
 	if shim.echo {
-		fmt.Println(fmt.Sprintf("%v < %v", shim.id, s))
+		fmt.Fprintln(shim.logOut, fmt.Sprintf("%v < %v", shim.id, s))
 	}
 	shim.writer.WriteString(s)
 	shim.writer.Flush()

+ 10 - 6
cli/tool/interpret.go

@@ -61,13 +61,17 @@ type CLIInterpreter struct {
 	Dir      *string // Root dir for interpreter
 	LogFile  *string // Logfile (blank for stdout)
 	LogLevel *string // Log level string (Debug, Info, Error)
+
+	// Log output
+
+	LogOut io.Writer
 }
 
 /*
 NewCLIInterpreter creates a new commandline interpreter for ECAL.
 */
 func NewCLIInterpreter() *CLIInterpreter {
-	return &CLIInterpreter{scope.NewScope(scope.GlobalScope), nil, nil, "", "", "", nil, nil, nil}
+	return &CLIInterpreter{scope.NewScope(scope.GlobalScope), nil, nil, "", "", "", nil, nil, nil, os.Stdout}
 }
 
 /*
@@ -199,7 +203,7 @@ func (i *CLIInterpreter) Interpret(interactive bool) error {
 	clt, err := termutil.NewConsoleLineTerminal(os.Stdout)
 
 	if interactive {
-		fmt.Println(fmt.Sprintf("ECAL %v", config.ProductVersion))
+		fmt.Fprintln(i.LogOut, fmt.Sprintf("ECAL %v", config.ProductVersion))
 	}
 
 	// Create Runtime Provider
@@ -212,13 +216,13 @@ func (i *CLIInterpreter) Interpret(interactive bool) error {
 
 			if interactive {
 				if lll, ok := i.RuntimeProvider.Logger.(*util.LogLevelLogger); ok {
-					fmt.Print(fmt.Sprintf("Log level: %v - ", lll.Level()))
+					fmt.Fprint(i.LogOut, fmt.Sprintf("Log level: %v - ", lll.Level()))
 				}
 
-				fmt.Println(fmt.Sprintf("Root directory: %v", *i.Dir))
+				fmt.Fprintln(i.LogOut, fmt.Sprintf("Root directory: %v", *i.Dir))
 
 				if i.CustomWelcomeMessage != "" {
-					fmt.Println(fmt.Sprintf(i.CustomWelcomeMessage))
+					fmt.Fprintln(i.LogOut, fmt.Sprintf(i.CustomWelcomeMessage))
 				}
 			}
 
@@ -249,7 +253,7 @@ func (i *CLIInterpreter) Interpret(interactive bool) error {
 
 								defer clt.StopTerm()
 
-								fmt.Println("Type 'q' or 'quit' to exit the shell and '?' to get help")
+								fmt.Fprintln(i.LogOut, "Type 'q' or 'quit' to exit the shell and '?' to get help")
 
 								line, err = clt.NextLine()
 								for err == nil && !isExitLine(line) {

+ 10 - 6
cli/tool/pack.go

@@ -41,13 +41,17 @@ type CLIPacker struct {
 	Dir          *string // Root dir for interpreter (all files will be collected)
 	SourceBinary *string // Binary which is used by the packer
 	TargetBinary *string // Binary which will be build by the packer
+
+	// Log output
+
+	LogOut io.Writer
 }
 
 /*
 NewCLIPacker creates a new commandline packer.
 */
 func NewCLIPacker() *CLIPacker {
-	return &CLIPacker{"", nil, nil, nil}
+	return &CLIPacker{"", nil, nil, nil, os.Stdout}
 }
 
 /*
@@ -103,7 +107,7 @@ func (p *CLIPacker) Pack() error {
 		return nil
 	}
 
-	fmt.Println(fmt.Sprintf("Packing %v -> %v from %v with entry: %v", *p.Dir,
+	fmt.Fprintln(p.LogOut, fmt.Sprintf("Packing %v -> %v from %v with entry: %v", *p.Dir,
 		*p.TargetBinary, *p.SourceBinary, p.EntryFile))
 
 	source, err := os.Open(*p.SourceBinary)
@@ -119,7 +123,7 @@ func (p *CLIPacker) Pack() error {
 			// First copy the binary
 
 			if bytes, err = io.Copy(dest, source); err == nil {
-				fmt.Println(fmt.Sprintf("Copied %v bytes for interpreter.", bytes))
+				fmt.Fprintln(p.LogOut, fmt.Sprintf("Copied %v bytes for interpreter.", bytes))
 				var bytes int
 
 				end := "####"
@@ -127,7 +131,7 @@ func (p *CLIPacker) Pack() error {
 
 				if bytes, err = dest.WriteString(marker); err == nil {
 					var data []byte
-					fmt.Println(fmt.Sprintf("Writing marker %v bytes for source archive.", bytes))
+					fmt.Fprintln(p.LogOut, fmt.Sprintf("Writing marker %v bytes for source archive.", bytes))
 
 					// Create a new zip archive.
 
@@ -137,7 +141,7 @@ func (p *CLIPacker) Pack() error {
 						var f io.Writer
 						if f, err = w.Create(".ecalsrc-entry"); err == nil {
 							if bytes, err = f.Write(data); err == nil {
-								fmt.Println(fmt.Sprintf("Writing %v bytes for intro", bytes))
+								fmt.Fprintln(p.LogOut, fmt.Sprintf("Writing %v bytes for intro", bytes))
 
 								// Add files to the archive
 
@@ -172,7 +176,7 @@ func (p *CLIPacker) packFiles(w *zip.Writer, filePath string, zipPath string) er
 					var f io.Writer
 					if f, err = w.Create(filepath.Join(zipPath, file.Name())); err == nil {
 						if bytes, err = f.Write(data); err == nil {
-							fmt.Println(fmt.Sprintf("Writing %v bytes for %v",
+							fmt.Fprintln(p.LogOut, fmt.Sprintf("Writing %v bytes for %v",
 								bytes, filepath.Join(filePath, file.Name())))
 						}
 					}