Skip to content

Commit

Permalink
fixup! Move CLI UI to ui/console package
Browse files Browse the repository at this point in the history
Resolves #2787 (comment)

Also fixes the issue with printing the cloud progress bar conditionally,
and removes the ModifyAndPrintBar method.
  • Loading branch information
Ivan Mirić committed Dec 12, 2022
1 parent 904db87 commit 6ca990a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
37 changes: 21 additions & 16 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ func (c *cmdCloud) preRun(cmd *cobra.Command, args []string) error {
// TODO: split apart some more
//nolint:funlen,gocognit,cyclop
func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
if !c.gs.flags.quiet {
c.gs.console.Printf("\n%s\n\n", c.gs.console.Banner())
}
maybePrintBanner(c.gs)

progressBar := pb.New(
pb.WithConstLeft("Init"),
pb.WithConstProgress(0, "Loading test script..."),
)
c.gs.console.PrintBar(progressBar)
maybePrintBar(c.gs, progressBar)

test, err := loadAndConfigureTest(c.gs, cmd, args, getPartialConfig)
if err != nil {
Expand All @@ -92,7 +90,8 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
// TODO: validate for externally controlled executor (i.e. executors that aren't distributable)
// TODO: move those validations to a separate function and reuse validateConfig()?

c.gs.console.ModifyAndPrintBar(progressBar, pb.WithConstProgress(0, "Building the archive..."))
progressBar.Modify(pb.WithConstProgress(0, "Building the archive..."))
maybePrintBar(c.gs, progressBar)
arc := testRunState.Runner.MakeArchive()

// TODO: Fix this
Expand Down Expand Up @@ -153,14 +152,16 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
logger := c.gs.logger

// Start cloud test run
c.gs.console.ModifyAndPrintBar(progressBar, pb.WithConstProgress(0, "Validating script options"))
progressBar.Modify(pb.WithConstProgress(0, "Validating script options"))
maybePrintBar(c.gs, progressBar)
client := cloudapi.NewClient(
logger, cloudConfig.Token.String, cloudConfig.Host.String, consts.Version, cloudConfig.Timeout.TimeDuration())
if err = client.ValidateOptions(arc.Options); err != nil {
return err
}

c.gs.console.ModifyAndPrintBar(progressBar, pb.WithConstProgress(0, "Uploading archive"))
progressBar.Modify(pb.WithConstProgress(0, "Uploading archive"))
maybePrintBar(c.gs, progressBar)
refID, err := client.StartCloudTestRun(name, cloudConfig.ProjectID.Int64, arc)
if err != nil {
return err
Expand Down Expand Up @@ -204,19 +205,23 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
c.gs.console.Print(execDesc)
}

c.gs.console.ModifyAndPrintBar(
progressBar, pb.WithConstLeft("Run "), pb.WithConstProgress(0, "Initializing the cloud test"),
progressBar.Modify(
pb.WithConstLeft("Run "),
pb.WithConstProgress(0, "Initializing the cloud test"),
)
maybePrintBar(c.gs, progressBar)

progressCtx, progressCancel := context.WithCancel(globalCtx)
progressBarWG := &sync.WaitGroup{}
progressBarWG.Add(1)
defer progressBarWG.Wait()
defer progressCancel()
go func() {
c.gs.console.ShowProgress(progressCtx, []*pb.ProgressBar{progressBar})
progressBarWG.Done()
}()
if !c.gs.flags.quiet {
progressBarWG := &sync.WaitGroup{}
progressBarWG.Add(1)
defer progressBarWG.Wait()
go func() {
c.gs.console.ShowProgress(progressCtx, []*pb.ProgressBar{progressBar})
progressBarWG.Done()
}()
}

var (
startTime time.Time
Expand Down
4 changes: 1 addition & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type cmdRun struct {
//
//nolint:funlen,gocognit,gocyclo,cyclop
func (c *cmdRun) run(cmd *cobra.Command, args []string) error {
if !c.gs.flags.quiet {
c.gs.console.Printf("\n%s\n\n", c.gs.console.Banner())
}
maybePrintBanner(c.gs)

test, err := loadAndConfigureTest(c.gs, cmd, args, getConfig)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions cmd/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import "go.k6.io/k6/ui/console/pb"

func maybePrintBanner(gs *globalState) {
if !gs.flags.quiet {
gs.console.Printf("\n%s\n\n", gs.console.Banner())
}
}

func maybePrintBar(gs *globalState, bar *pb.ProgressBar) {
if !gs.flags.quiet {
gs.console.PrintBar(bar)
}
}
6 changes: 0 additions & 6 deletions ui/console/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ func (c *Console) PrintBar(bar *pb.ProgressBar) {
c.Print(rendered.String() + end)
}

// ModifyAndPrintBar modifies bar with the givenoptions and prints it to stdout.
func (c *Console) ModifyAndPrintBar(bar *pb.ProgressBar, options ...pb.ProgressBarOption) {
bar.Modify(options...)
c.PrintBar(bar)
}

// ShowProgress renders the given progressbars in a loop to stdout, handling
// dynamic resizing depending on the current terminal window size. The resizing
// is most responsive on Linux where terminal windows implement the WINCH
Expand Down

0 comments on commit 6ca990a

Please sign in to comment.