2017-03-05 07:56:08 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-03-12 08:46:59 +00:00
|
|
|
"strings"
|
2017-03-05 07:56:08 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Identifies the type of line in the logs.
|
|
|
|
const (
|
|
|
|
LineStdout int = iota
|
|
|
|
LineStderr
|
|
|
|
LineExitCode
|
|
|
|
LineMetadata
|
|
|
|
LineProgress
|
|
|
|
)
|
|
|
|
|
|
|
|
// Line is a line of console output.
|
|
|
|
type Line struct {
|
|
|
|
Proc string `json:"proc,omitempty"`
|
|
|
|
Time int64 `json:"time,omitempty"`
|
|
|
|
Type int `json:"type,omitempty"`
|
2021-10-09 00:43:44 +00:00
|
|
|
Pos int `json:"pos,omitempty"`
|
2017-03-05 07:56:08 +00:00
|
|
|
Out string `json:"out,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) String() string {
|
|
|
|
switch l.Type {
|
|
|
|
case LineExitCode:
|
|
|
|
return fmt.Sprintf("[%s] exit code %s", l.Proc, l.Out)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("[%s:L%v:%vs] %s", l.Proc, l.Pos, l.Time, l.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineWriter sends logs to the client.
|
|
|
|
type LineWriter struct {
|
2017-04-01 11:17:04 +00:00
|
|
|
peer Peer
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
num int
|
|
|
|
now time.Time
|
|
|
|
rep *strings.Replacer
|
|
|
|
lines []*Line
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLineWriter returns a new line reader.
|
2017-03-12 08:46:59 +00:00
|
|
|
func NewLineWriter(peer Peer, id, name string, secret ...string) *LineWriter {
|
2017-03-05 07:56:08 +00:00
|
|
|
w := new(LineWriter)
|
|
|
|
w.peer = peer
|
|
|
|
w.id = id
|
|
|
|
w.name = name
|
|
|
|
w.num = 0
|
|
|
|
w.now = time.Now().UTC()
|
2017-03-12 08:46:59 +00:00
|
|
|
|
|
|
|
var oldnew []string
|
|
|
|
for _, old := range secret {
|
|
|
|
oldnew = append(oldnew, old)
|
|
|
|
oldnew = append(oldnew, "********")
|
|
|
|
}
|
|
|
|
if len(oldnew) != 0 {
|
|
|
|
w.rep = strings.NewReplacer(oldnew...)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LineWriter) Write(p []byte) (n int, err error) {
|
2017-03-12 08:46:59 +00:00
|
|
|
out := string(p)
|
|
|
|
if w.rep != nil {
|
|
|
|
out = w.rep.Replace(out)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-03-12 08:46:59 +00:00
|
|
|
|
|
|
|
line := &Line{
|
|
|
|
Out: out,
|
|
|
|
Proc: w.name,
|
|
|
|
Pos: w.num,
|
|
|
|
Time: int64(time.Since(w.now).Seconds()),
|
|
|
|
Type: LineStdout,
|
|
|
|
}
|
|
|
|
w.peer.Log(context.Background(), w.id, line)
|
|
|
|
w.num++
|
|
|
|
|
|
|
|
// for _, part := range bytes.Split(p, []byte{'\n'}) {
|
|
|
|
// line := &Line{
|
|
|
|
// Out: string(part),
|
|
|
|
// Proc: w.name,
|
|
|
|
// Pos: w.num,
|
|
|
|
// Time: int64(time.Since(w.now).Seconds()),
|
|
|
|
// Type: LineStdout,
|
|
|
|
// }
|
|
|
|
// w.peer.Log(context.Background(), w.id, line)
|
|
|
|
// w.num++
|
|
|
|
// }
|
2017-04-01 11:17:04 +00:00
|
|
|
w.lines = append(w.lines, line)
|
2017-03-05 07:56:08 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
|
|
|
|
// Lines returns the line history
|
|
|
|
func (w *LineWriter) Lines() []*Line {
|
|
|
|
return w.lines
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear clears the line history
|
|
|
|
func (w *LineWriter) Clear() {
|
|
|
|
w.lines = w.lines[:0]
|
|
|
|
}
|