diff --git a/internal/io/spinner.go b/internal/io/spinner.go new file mode 100644 index 00000000..715db927 --- /dev/null +++ b/internal/io/spinner.go @@ -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() +} diff --git a/pkg/cmd/pkg/push.go b/pkg/cmd/pkg/push.go index 934befdc..491f7879 100644 --- a/pkg/cmd/pkg/push.go +++ b/pkg/cmd/pkg/push.go @@ -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" ) @@ -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 }