Skip to content

Commit

Permalink
add raw stdout flag
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Jan 29, 2019
1 parent ca8d21f commit 48f452c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ build
.goxc.local.json
vendor/
dist
snap
stage
prime
parts
snap/**
stage/**
prime/**
parts/**
*.snap
77 changes: 76 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions cmd/monexec/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package main

import (
"context"
"github.com/reddec/monexec/monexec"
"github.com/reddec/monexec/plugins"
"github.com/reddec/monexec/pool"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
"os"
"context"
"log"
"github.com/reddec/monexec/monexec"
"os"
"os/signal"
"syscall"
"github.com/reddec/monexec/plugins"
"github.com/reddec/monexec/pool"
)

var (
Expand All @@ -27,6 +27,7 @@ var (
runLabel = runCommand.Flag("label", "Label name for executable. Default - autogenerated").Short('l').String()
runWorkDir = runCommand.Flag("workdir", "Workdir for executable").Short('w').String()
runEnv = runCommand.Flag("env", "Environment addition variables").Short('e').StringMap()
runRawOutput = runCommand.Flag("raw", "Raw stdout without prefixes").Short('R').Bool()

runConsulEnable = runCommand.Flag("consul", "Enable consul integration").Bool()
runConsulAddress = runCommand.Flag("consul-address", "Consul address").Default("http://localhost:8500").String()
Expand All @@ -52,6 +53,7 @@ func run() {
StopTimeout: *runStopTimeout,
WorkDir: *runWorkDir,
Environment: *runEnv,
RawOutput: *runRawOutput,
})
monexec.FillDefaultExecutable(&config.Services[0])

Expand Down
13 changes: 13 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,16 @@ critical:
```

# Raw stdout

For several reasons (i.e. use in a bash tools) raw stdout is required from application.

Since `0.1.12` to disable all prefixes in STDOUT (in STDERR they will still persists) use flag `--raw, -R`.

**Example:**

```bash
monexec -R echo 123 > sample.txt # run echo command. Use CTRL+C to interrupt when needed
```

The file `sample.txt` will now contains ONLY result of echo command (i.e. `123`)
30 changes: 21 additions & 9 deletions pool/executable.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package pool

import (
"time"
"os/exec"
"os"
"context"
"io"
"log"
"context"
"strings"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)

// Executable - basic information about process
Expand All @@ -23,6 +23,7 @@ type Executable struct {
RestartTimeout time.Duration `yaml:"restart_delay,omitempty"` // Restart delay
Restart int `yaml:"restart,omitempty"` // How much restart allowed. -1 infinite
LogFile string `yaml:"logFile,omitempty"` // if empty - only to log. If not absolute - relative to workdir
RawOutput bool `yaml:"raw,omitempty"` // print stdout as-is without prefixes

log *log.Logger
loggerInit sync.Once
Expand Down Expand Up @@ -94,8 +95,18 @@ func (exe *Executable) run(ctx context.Context) error {
setAttrs(cmd)

var outputs []io.Writer
var stderr []io.Writer
var stdout []io.Writer

output := NewLoggerStream(exe.logger(), "out:")
outputs = append(outputs, output)
defer output.Close()
stderr = outputs
stdout = outputs

outputs = append(outputs, NewLoggerStream(exe.logger(), "out:"))
if exe.RawOutput {
stdout = append(stdout, os.Stdout)
}

res := make(chan error, 1)

Expand All @@ -115,10 +126,11 @@ func (exe *Executable) run(ctx context.Context) error {
}
}

logStream := io.MultiWriter(outputs...)
logStderrStream := io.MultiWriter(stderr...)
logStdoutStream := io.MultiWriter(stdout...)

cmd.Stderr = logStream
cmd.Stdout = logStream
cmd.Stderr = logStderrStream
cmd.Stdout = logStdoutStream

err := cmd.Start()
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion pool/logger_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type LogInterface interface {
Println(v ...interface{})
}

func NewLoggerStream(logger LogInterface, prefix string) (io.WriteCloser) {
func NewLoggerStream(logger LogInterface, prefix string) io.WriteCloser {
reader, writer := io.Pipe()
go func() {
scanner := bufio.NewReader(reader)
Expand Down

0 comments on commit 48f452c

Please sign in to comment.