From 267b267201ebfb8b2ed2d52106b14164794e5a86 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 14 Sep 2020 10:01:59 +0200 Subject: [PATCH] Progress functions can return a string, that can be used in the caller of progress.Run to display final result after progress display Signed-off-by: Guillaume Tardif --- aci/volumes.go | 5 ++--- cli/cmd/compose/down.go | 7 ++++--- cli/cmd/compose/up.go | 9 +++++---- cli/cmd/run/run.go | 7 +++---- cli/cmd/volume/acivolume.go | 11 ++++++----- progress/writer.go | 16 +++++++++++----- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/aci/volumes.go b/aci/volumes.go index 46d01c182..1109c88cf 100644 --- a/aci/volumes.go +++ b/aci/volumes.go @@ -212,13 +212,12 @@ func (cs *aciVolumeService) Delete(ctx context.Context, id string, options inter func toVolume(account storage.Account, fileShareName string) volumes.Volume { return volumes.Volume{ - ID: VolumeID(*account.Name, fileShareName), + ID: volumeID(*account.Name, fileShareName), Description: fmt.Sprintf("Fileshare %s in %s storage account", fileShareName, *account.Name), } } -// VolumeID generate volume ID from azure storage accoun & fileshare -func VolumeID(storageAccount string, fileShareName string) string { +func volumeID(storageAccount string, fileShareName string) string { return fmt.Sprintf("%s/%s", storageAccount, fileShareName) } diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index 44281fa30..0b01155b2 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -46,11 +46,12 @@ func runDown(ctx context.Context, opts composeOptions) error { return err } - return progress.Run(ctx, func(ctx context.Context) error { + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { projectName, err := opts.toProjectName() if err != nil { - return err + return "", err } - return c.ComposeService().Down(ctx, projectName) + return projectName, c.ComposeService().Down(ctx, projectName) }) + return err } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index b1914128f..c3e618c3e 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -49,16 +49,17 @@ func runUp(ctx context.Context, opts composeOptions) error { return err } - return progress.Run(ctx, func(ctx context.Context) error { + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { options, err := opts.toProjectOptions() if err != nil { - return err + return "", err } project, err := cli.ProjectFromOptions(options) if err != nil { - return err + return "", err } - return c.ComposeService().Up(ctx, project) + return "", c.ComposeService().Up(ctx, project) }) + return err } diff --git a/cli/cmd/run/run.go b/cli/cmd/run/run.go index a4283eba9..5eb398cdc 100644 --- a/cli/cmd/run/run.go +++ b/cli/cmd/run/run.go @@ -68,8 +68,8 @@ func runRun(ctx context.Context, image string, opts run.Opts) error { return err } - err = progress.Run(ctx, func(ctx context.Context) error { - return c.ContainerService().Run(ctx, containerConfig) + result, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + return containerConfig.ID, c.ContainerService().Run(ctx, containerConfig) }) if err != nil { return err @@ -94,7 +94,6 @@ func runRun(ctx context.Context, image string, opts run.Opts) error { return c.ContainerService().Logs(ctx, opts.Name, req) } - fmt.Println(opts.Name) - + fmt.Println(result) return nil } diff --git a/cli/cmd/volume/acivolume.go b/cli/cmd/volume/acivolume.go index 4216c36d2..f38cff206 100644 --- a/cli/cmd/volume/acivolume.go +++ b/cli/cmd/volume/acivolume.go @@ -57,16 +57,17 @@ func createVolume() *cobra.Command { if err != nil { return err } - err = progress.Run(ctx, func(ctx context.Context) error { - if _, err := c.VolumeService().Create(ctx, aciOpts); err != nil { - return err + result, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + volume, err := c.VolumeService().Create(ctx, aciOpts) + if err != nil { + return "", err } - return nil + return volume.ID, nil }) if err != nil { return err } - fmt.Println(aci.VolumeID(aciOpts.Account, aciOpts.Fileshare)) + fmt.Println(result) return nil }, } diff --git a/progress/writer.go b/progress/writer.go index 01d92caa3..5ca0e3e21 100644 --- a/progress/writer.go +++ b/progress/writer.go @@ -77,15 +77,16 @@ func ContextWriter(ctx context.Context) Writer { return s } -type progressFunc func(context.Context) error +type progressFunc func(context.Context) (string, error) // Run will run a writer and the progress function // in parallel -func Run(ctx context.Context, pf progressFunc) error { +func Run(ctx context.Context, pf progressFunc) (string, error) { eg, _ := errgroup.WithContext(ctx) w, err := NewWriter(os.Stderr) + var result string if err != nil { - return err + return "", err } eg.Go(func() error { return w.Start(context.Background()) @@ -95,10 +96,15 @@ func Run(ctx context.Context, pf progressFunc) error { eg.Go(func() error { defer w.Stop() - return pf(ctx) + s, err := pf(ctx) + if err == nil { + result = s + } + return err }) - return eg.Wait() + err = eg.Wait() + return result, err } // NewWriter returns a new multi-progress writer