Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bk build download --logs command #271

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
dist/
buildkite.yaml
.bk.yaml
build-logs-*
5 changes: 3 additions & 2 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ func NewCmdBuild(f *factory.Factory) *cobra.Command {
},
}

cmd.AddCommand(NewCmdBuildCancel(f))
cmd.AddCommand(NewCmdBuildDownload(f))
cmd.AddCommand(NewCmdBuildNew(f))
cmd.AddCommand(NewCmdBuildView(f))
cmd.AddCommand(NewCmdBuildRebuild(f))
cmd.AddCommand(NewCmdBuildCancel(f))
cmd.AddCommand(NewCmdBuildView(f))

return &cmd
}
Expand Down
97 changes: 97 additions & 0 deletions pkg/cmd/build/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package build

import (
"fmt"
"os"
"path/filepath"

"github.com/buildkite/cli/v3/internal/build"
buildResolver "github.com/buildkite/cli/v3/internal/build/resolver"
pipelineResolver "github.com/buildkite/cli/v3/internal/pipeline/resolver"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/charmbracelet/huh/spinner"
"github.com/spf13/cobra"
)

func NewCmdBuildDownload(f *factory.Factory) *cobra.Command {
cmd := cobra.Command{
DisableFlagsInUseLine: true,
Use: "download [number [pipeline]] [flags]",
Short: "Download job logs for a build",
Long: "Download allows you to download resources for a build.",
RunE: func(cmd *cobra.Command, args []string) error {
pipelineRes := pipelineResolver.NewAggregateResolver(
pipelineResolver.ResolveFromPositionalArgument(args, 1, f.Config),
pipelineResolver.ResolveFromConfig(f.Config, pipelineResolver.PickOne),
pipelineResolver.ResolveFromRepository(f, pipelineResolver.CachedPicker(f.Config, pipelineResolver.PickOne)),
)

buildRes := buildResolver.NewAggregateResolver(
buildResolver.ResolveFromPositionalArgument(args, 0, pipelineRes.Resolve, f.Config),
buildResolver.ResolveBuildFromCurrentBranch(f.GitRepository, pipelineRes.Resolve, f),
)

bld, err := buildRes.Resolve(cmd.Context())
if err != nil {
return err
}
if bld == nil {
return fmt.Errorf("could not resolve a build")
}

spinErr := spinner.New().
Title("Downloading logs").
Action(func() {
err = download(*bld, f)
}).
Run()
if spinErr != nil {
return spinErr
}

return err
},
}

return &cmd
}

func download(build build.Build, f *factory.Factory) error {
b, _, err := f.RestAPIClient.Builds.Get(build.Organization, build.Pipeline, fmt.Sprint(build.BuildNumber), nil)
if err != nil {
return err
}
if b == nil {
return fmt.Errorf("could not find build for %s #%d", build.Pipeline, build.BuildNumber)
}

directory := fmt.Sprintf("build-logs-%s", *b.ID)
err = os.MkdirAll(directory, os.ModePerm)
if err != nil {
return err
}

for _, job := range b.Jobs {
// only script (command) jobs will have logs
if job == nil || *job.Type != "script" {
continue
}

log, _, err := f.RestAPIClient.Jobs.GetJobLog(build.Organization, build.Pipeline, *b.ID, *job.ID)
if err != nil {
return err
}
if log == nil {
return fmt.Errorf("could not get logs for job %s", *job.ID)
}

err = os.WriteFile(filepath.Join(directory, *job.ID), []byte(*log.Content), 0644)
if err != nil {
return err
}
}

fmt.Printf("\nDownloaded logs to: %s\n", directory)

return nil
}