Преглед на файлове

Merge pull request #613 from docker/progress_result

Progress functions can return a string, that can be used in the caller of progress.Run() to display final result after progress display
Guillaume Tardif преди 5 години
родител
ревизия
317659be8e
променени са 6 файла, в които са добавени 31 реда и са изтрити 24 реда
  1. 2 3
      aci/volumes.go
  2. 4 3
      cli/cmd/compose/down.go
  3. 5 4
      cli/cmd/compose/up.go
  4. 3 4
      cli/cmd/run/run.go
  5. 6 5
      cli/cmd/volume/acivolume.go
  6. 11 5
      progress/writer.go

+ 2 - 3
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)
 }
 

+ 4 - 3
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
 }

+ 5 - 4
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
 }

+ 3 - 4
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
 }

+ 6 - 5
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
 		},
 	}

+ 11 - 5
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