Skip to content

Commit

Permalink
fix(cli): wait for runDevContainer command to print logs before closing
Browse files Browse the repository at this point in the history
writer
  • Loading branch information
pascalbreuninger committed Jan 29, 2025
1 parent 7996f43 commit 6093dd2
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/driver/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"strings"
"time"

"github.com/loft-sh/devpod/pkg/agent"
"github.com/loft-sh/devpod/pkg/binaries"
Expand All @@ -16,6 +17,7 @@ import (
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/types"
"github.com/loft-sh/log"
"github.com/loft-sh/log/scanner"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -202,9 +204,16 @@ func (c *customDriver) RunDevContainer(ctx context.Context, workspaceId string,
return fmt.Errorf("marshal run options: %w", err)
}

// create a log writer
writer := c.log.Writer(logrus.InfoLevel, false)
done := make(chan struct{})
reader, writer := io.Pipe()
defer writer.Close()
go func() {
scan := scanner.NewScanner(reader)
for scan.Scan() {
c.log.Info(scan.Text())
}
done <- struct{}{}
}()

// run command
err = c.runCommand(
Expand All @@ -221,6 +230,13 @@ func (c *customDriver) RunDevContainer(ctx context.Context, workspaceId string,
c.log,
)
if err != nil {
// close writer, wait for logging to flush and shut down
writer.Close()
select {
case <-done:
// forcibly shut down after 1 second
case <-time.After(1 * time.Second):
}
return fmt.Errorf("error running devcontainer: %w", err)
}

Expand Down

0 comments on commit 6093dd2

Please sign in to comment.