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 <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-09-14 10:01:59 +02:00
parent 9c07a7b3ad
commit 267b267201
6 changed files with 31 additions and 24 deletions

View File

@ -212,13 +212,12 @@ func (cs *aciVolumeService) Delete(ctx context.Context, id string, options inter
func toVolume(account storage.Account, fileShareName string) volumes.Volume { func toVolume(account storage.Account, fileShareName string) volumes.Volume {
return 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), 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) return fmt.Sprintf("%s/%s", storageAccount, fileShareName)
} }

View File

@ -46,11 +46,12 @@ func runDown(ctx context.Context, opts composeOptions) error {
return err 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() projectName, err := opts.toProjectName()
if err != nil { if err != nil {
return err return "", err
} }
return c.ComposeService().Down(ctx, projectName) return projectName, c.ComposeService().Down(ctx, projectName)
}) })
return err
} }

View File

@ -49,16 +49,17 @@ func runUp(ctx context.Context, opts composeOptions) error {
return err 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() options, err := opts.toProjectOptions()
if err != nil { if err != nil {
return err return "", err
} }
project, err := cli.ProjectFromOptions(options) project, err := cli.ProjectFromOptions(options)
if err != nil { if err != nil {
return err return "", err
} }
return c.ComposeService().Up(ctx, project) return "", c.ComposeService().Up(ctx, project)
}) })
return err
} }

View File

@ -68,8 +68,8 @@ func runRun(ctx context.Context, image string, opts run.Opts) error {
return err return err
} }
err = progress.Run(ctx, func(ctx context.Context) error { result, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
return c.ContainerService().Run(ctx, containerConfig) return containerConfig.ID, c.ContainerService().Run(ctx, containerConfig)
}) })
if err != nil { if err != nil {
return err 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) return c.ContainerService().Logs(ctx, opts.Name, req)
} }
fmt.Println(opts.Name) fmt.Println(result)
return nil return nil
} }

View File

@ -57,16 +57,17 @@ func createVolume() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
err = progress.Run(ctx, func(ctx context.Context) error { result, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
if _, err := c.VolumeService().Create(ctx, aciOpts); err != nil { volume, err := c.VolumeService().Create(ctx, aciOpts)
return err if err != nil {
return "", err
} }
return nil return volume.ID, nil
}) })
if err != nil { if err != nil {
return err return err
} }
fmt.Println(aci.VolumeID(aciOpts.Account, aciOpts.Fileshare)) fmt.Println(result)
return nil return nil
}, },
} }

View File

@ -77,15 +77,16 @@ func ContextWriter(ctx context.Context) Writer {
return s return s
} }
type progressFunc func(context.Context) error type progressFunc func(context.Context) (string, error)
// Run will run a writer and the progress function // Run will run a writer and the progress function
// in parallel // in parallel
func Run(ctx context.Context, pf progressFunc) error { func Run(ctx context.Context, pf progressFunc) (string, error) {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
w, err := NewWriter(os.Stderr) w, err := NewWriter(os.Stderr)
var result string
if err != nil { if err != nil {
return err return "", err
} }
eg.Go(func() error { eg.Go(func() error {
return w.Start(context.Background()) return w.Start(context.Background())
@ -95,10 +96,15 @@ func Run(ctx context.Context, pf progressFunc) error {
eg.Go(func() error { eg.Go(func() error {
defer w.Stop() 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 // NewWriter returns a new multi-progress writer