Skip to content

Commit 722b7d1

Browse files
committed
hostagent: sync every (io.Writer).Write
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 9e611f0 commit 722b7d1

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

cmd/limactl/hostagent.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"io"
45
"os"
56
"os/signal"
67
"strconv"
@@ -45,12 +46,31 @@ func hostagentAction(clicontext *cli.Context) error {
4546
sigintCh := make(chan os.Signal, 1)
4647
signal.Notify(sigintCh, os.Interrupt)
4748

48-
stdout := clicontext.App.Writer
49-
stderr := clicontext.App.ErrWriter
49+
stdout := &syncWriter{w: clicontext.App.Writer}
50+
stderr := &syncWriter{w: clicontext.App.ErrWriter}
5051

5152
ha, err := hostagent.New(instName, stdout, stderr, sigintCh)
5253
if err != nil {
5354
return err
5455
}
5556
return ha.Run(clicontext.Context)
5657
}
58+
59+
// syncer is implemented by *os.File
60+
type syncer interface {
61+
Sync() error
62+
}
63+
64+
type syncWriter struct {
65+
w io.Writer
66+
}
67+
68+
func (w *syncWriter) Write(p []byte) (int, error) {
69+
written, err := w.w.Write(p)
70+
if err == nil {
71+
if s, ok := w.w.(syncer); ok {
72+
_ = s.Sync()
73+
}
74+
}
75+
return written, err
76+
}

0 commit comments

Comments
 (0)