Skip to content

Commit

Permalink
fix: build logs ux (#975)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic authored Aug 28, 2024
1 parent 01c79b8 commit a839cf7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/daytona_build_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ daytona build logs [flags]
### Options

```
-f, --follow Follow logs
--continue-on-completed Continue streaming logs after the build is completed
-f, --follow Follow logs
```

### Options inherited from parent commands
Expand Down
3 changes: 3 additions & 0 deletions hack/docs/daytona_build_logs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: daytona build logs
synopsis: View logs for build
usage: daytona build logs [flags]
options:
- name: continue-on-completed
default_value: "false"
usage: Continue streaming logs after the build is completed
- name: follow
shorthand: f
default_value: "false"
Expand Down
3 changes: 2 additions & 1 deletion pkg/build/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"sync"

"github.com/charmbracelet/lipgloss"
"github.com/daytonaio/daytona/pkg/containerregistry"
"github.com/daytonaio/daytona/pkg/docker"
"github.com/daytonaio/daytona/pkg/git"
Expand Down Expand Up @@ -283,7 +284,7 @@ func (r *BuildRunner) RunBuildProcess(config BuildProcessConfig) {
config.BuildLogger.Write([]byte(errMsg + "\n"))
}

config.BuildLogger.Write([]byte("\nBuild completed successfully\n"))
config.BuildLogger.Write([]byte("\n \n" + lipgloss.NewStyle().Bold(true).Render("Build completed successfully")))

if r.telemetryEnabled {
r.logTelemetry(context.Background(), *config.Build, err)
Expand Down
45 changes: 44 additions & 1 deletion pkg/cmd/build/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ package build

import (
"context"
"fmt"
"log"
"slices"
"time"

"github.com/daytonaio/daytona/cmd/daytona/config"
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/pkg/apiclient"
"github.com/daytonaio/daytona/pkg/views/workspace/selection"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,12 +61,51 @@ var buildLogsCmd = &cobra.Command{
}

stopLogs := false
apiclient_util.ReadBuildLogs(activeProfile, buildId, query, &stopLogs)
go apiclient_util.ReadBuildLogs(activeProfile, buildId, query, &stopLogs)

if !continueOnCompletedFlag {
err = waitForBuildToComplete(buildId, apiClient)
if err != nil {
log.Fatal(err)
}
stopLogs = true
} else {
// Sleep indefinitely
select {}
}

// Make sure the terminal cursor is reset
fmt.Print("\033[?25h")
},
}

func waitForBuildToComplete(buildId string, apiClient *apiclient.APIClient) error {
for {
build, res, err := apiClient.BuildAPI.GetBuild(context.Background(), buildId).Execute()
if err != nil {
return apiclient_util.HandleErrorResponse(res, err)
}

completedStates := []apiclient.BuildBuildState{
apiclient.BuildStatePublished,
apiclient.BuildStateError,
apiclient.BuildStateDeleting,
}

if slices.Contains(completedStates, build.State) {
// Allow the logs to be printed before exiting
time.Sleep(time.Second)
return nil
}

time.Sleep(time.Second)
}
}

var followFlag bool
var continueOnCompletedFlag bool

func init() {
buildLogsCmd.Flags().BoolVarP(&followFlag, "follow", "f", false, "Follow logs")
buildLogsCmd.Flags().BoolVar(&continueOnCompletedFlag, "continue-on-completed", false, "Continue streaming logs after the build is completed")
}

0 comments on commit a839cf7

Please sign in to comment.