Print command output in line (#1354)
This is an attempt to log helm exec output as the helm exec command is running. It only prints if the --debug flag is set. Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
parent
0fc0869671
commit
dc6c59dc14
|
|
@ -0,0 +1,13 @@
|
||||||
|
package helmexec
|
||||||
|
|
||||||
|
import "math/rand"
|
||||||
|
|
||||||
|
var executionIDComponents = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
|
|
||||||
|
func newExecutionID() string {
|
||||||
|
b := make([]rune, 5)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = executionIDComponents[rand.Intn(len(executionIDComponents))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package helmexec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type logWriterGenerator struct {
|
||||||
|
log *zap.SugaredLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g logWriterGenerator) Writer(prefix string) *logWriter {
|
||||||
|
return &logWriter{
|
||||||
|
log: g.log,
|
||||||
|
prefix: prefix,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type logWriter struct {
|
||||||
|
log *zap.SugaredLogger
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *logWriter) Write(p []byte) (int, error) {
|
||||||
|
w.log.Debugf("%s%s", w.prefix, strings.TrimSpace(string(p)))
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
@ -4,12 +4,14 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.uber.org/zap"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -34,21 +36,34 @@ func (shell ShellRunner) Execute(cmd string, args []string, env map[string]strin
|
||||||
preparedCmd := exec.Command(cmd, args...)
|
preparedCmd := exec.Command(cmd, args...)
|
||||||
preparedCmd.Dir = shell.Dir
|
preparedCmd.Dir = shell.Dir
|
||||||
preparedCmd.Env = mergeEnv(os.Environ(), env)
|
preparedCmd.Env = mergeEnv(os.Environ(), env)
|
||||||
return Output(preparedCmd)
|
return Output(preparedCmd, &logWriterGenerator{
|
||||||
|
log: shell.Logger,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Output(c *exec.Cmd) ([]byte, error) {
|
func Output(c *exec.Cmd, logWriterGenerators ...*logWriterGenerator) ([]byte, error) {
|
||||||
if c.Stdout != nil {
|
if c.Stdout != nil {
|
||||||
return nil, errors.New("exec: Stdout already set")
|
return nil, errors.New("exec: Stdout already set")
|
||||||
}
|
}
|
||||||
if c.Stderr != nil {
|
if c.Stderr != nil {
|
||||||
return nil, errors.New("exec: Stderr already set")
|
return nil, errors.New("exec: Stderr already set")
|
||||||
}
|
}
|
||||||
|
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
var combined bytes.Buffer
|
var combined bytes.Buffer
|
||||||
c.Stdout = io.MultiWriter(&stdout, &combined)
|
|
||||||
c.Stderr = io.MultiWriter(&stderr, &combined)
|
var logWriters []io.Writer
|
||||||
|
|
||||||
|
id := newExecutionID()
|
||||||
|
for _, g := range logWriterGenerators {
|
||||||
|
logPrefix := fmt.Sprintf("%s:%s> ", filepath.Base(c.Path), id)
|
||||||
|
logWriters = append(logWriters, g.Writer(logPrefix))
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Stdout = io.MultiWriter(append([]io.Writer{&stdout, &combined}, logWriters...)...)
|
||||||
|
c.Stderr = io.MultiWriter(append([]io.Writer{&stderr, &combined}, logWriters...)...)
|
||||||
|
|
||||||
err := c.Run()
|
err := c.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue