Skip to content

Commit

Permalink
Only use a spinner on a tty (#409)
Browse files Browse the repository at this point in the history
Otherwise this command fails with errors about missing a tty when used
in scripts on CI.
  • Loading branch information
sj26 authored Dec 5, 2024
1 parent 870be22 commit b7388db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
21 changes: 21 additions & 0 deletions internal/io/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io

import (
"os"

"github.com/charmbracelet/huh/spinner"
"github.com/mattn/go-isatty"
)

func SpinWhile(name string, action func()) error {
if isatty.IsTerminal(os.Stdout.Fd()) {
action()

return nil
}

return spinner.New().
Title(name).
Action(action).
Run()
}
17 changes: 7 additions & 10 deletions pkg/cmd/pkg/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"os"

"github.com/MakeNowJust/heredoc"
bk_io "github.com/buildkite/cli/v3/internal/io"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/huh/spinner"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -74,15 +74,12 @@ func NewCmdPackagePush(f *factory.Factory) *cobra.Command {
}

var pkg buildkite.Package
spinErr := spinner.New().
Title("Pushing package").
Action(func() {
pkg, _, err = f.RestAPIClient.PackagesService.Create(cmd.Context(), f.Config.OrganizationSlug(), cfg.RegistrySlug, buildkite.CreatePackageInput{
Filename: packageName,
Package: from,
})
}).
Run()
spinErr := bk_io.SpinWhile("Pushing package", func() {
pkg, _, err = f.RestAPIClient.PackagesService.Create(cmd.Context(), f.Config.OrganizationSlug(), cfg.RegistrySlug, buildkite.CreatePackageInput{
Filename: packageName,
Package: from,
})
})
if spinErr != nil {
return spinErr
}
Expand Down

0 comments on commit b7388db

Please sign in to comment.