Просмотр исходного кода

Adding error progress indication when errors

Signed-off-by: Guillame Tardif <[email protected]>
Guillame Tardif 4 лет назад
Родитель
Сommit
4d1f265c62
6 измененных файлов с 30 добавлено и 21 удалено
  1. 1 0
      aci/aci.go
  2. 10 6
      aci/volumes.go
  3. 1 1
      ecs/down.go
  4. 2 6
      ecs/wait.go
  5. 7 4
      local/compose.go
  6. 9 4
      progress/event.go

+ 1 - 0
aci/aci.go

@@ -116,6 +116,7 @@ func createOrUpdateACIContainers(ctx context.Context, aciContext store.AciContex
 		groupDefinition,
 	)
 	if err != nil {
+		w.Event(progress.ErrorEvent(groupDisplay))
 		return err
 	}
 

+ 10 - 6
aci/volumes.go

@@ -87,12 +87,14 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int
 	w.Event(progress.NewEvent(opts.Account, progress.Working, "Validating"))
 	accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
 	if err != nil {
+		w.Event(progress.ErrorEvent(opts.Account))
 		return volumes.Volume{}, err
 	}
 	account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "")
 	if err == nil {
 		w.Event(progress.NewEvent(opts.Account, progress.Done, "Use existing"))
 	} else if !account.HasHTTPStatus(http.StatusNotFound) {
+		w.Event(progress.ErrorEvent(opts.Account))
 		return volumes.Volume{}, err
 	} else {
 		result, err := accountClient.CheckNameAvailability(ctx, storage.AccountCheckNameAvailabilityParameters{
@@ -100,9 +102,11 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int
 			Type: to.StringPtr("Microsoft.Storage/storageAccounts"),
 		})
 		if err != nil {
+			w.Event(progress.ErrorEvent(opts.Account))
 			return volumes.Volume{}, err
 		}
 		if !*result.NameAvailable {
+			w.Event(progress.ErrorEvent(opts.Account))
 			return volumes.Volume{}, errors.New("error: " + *result.Message)
 		}
 		parameters := defaultStorageAccountParams(cs.aciContext)
@@ -111,16 +115,16 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int
 
 		future, err := accountClient.Create(ctx, cs.aciContext.ResourceGroup, opts.Account, parameters)
 		if err != nil {
-			w.Event(progress.ErrorEvent(opts.Account, "Error"))
+			w.Event(progress.ErrorEvent(opts.Account))
 			return volumes.Volume{}, err
 		}
 		if err := future.WaitForCompletionRef(ctx, accountClient.Client); err != nil {
-			w.Event(progress.ErrorEvent(opts.Account, "Error"))
+			w.Event(progress.ErrorEvent(opts.Account))
 			return volumes.Volume{}, err
 		}
 		account, err = future.Result(accountClient)
 		if err != nil {
-			w.Event(progress.ErrorEvent(opts.Account, "Error"))
+			w.Event(progress.ErrorEvent(opts.Account))
 			return volumes.Volume{}, err
 		}
 		w.Event(progress.CreatedEvent(opts.Account))
@@ -133,16 +137,16 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int
 
 	fileShare, err := fileShareClient.Get(ctx, cs.aciContext.ResourceGroup, *account.Name, name, "")
 	if err == nil {
-		w.Event(progress.ErrorEvent(name, "Error"))
+		w.Event(progress.ErrorEvent(name))
 		return volumes.Volume{}, errors.Wrapf(errdefs.ErrAlreadyExists, "Azure fileshare %q already exists", name)
 	}
 	if !fileShare.HasHTTPStatus(http.StatusNotFound) {
-		w.Event(progress.ErrorEvent(name, "Error"))
+		w.Event(progress.ErrorEvent(name))
 		return volumes.Volume{}, err
 	}
 	fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, name, storage.FileShare{})
 	if err != nil {
-		w.Event(progress.ErrorEvent(name, "Error"))
+		w.Event(progress.ErrorEvent(name))
 		return volumes.Volume{}, err
 	}
 	w.Event(progress.CreatedEvent(name))

+ 1 - 1
ecs/down.go

@@ -68,7 +68,7 @@ func doDelete(ctx context.Context, delete func(ctx context.Context, arn string)
 		w.Event(progress.RemovingEvent(r.LogicalID))
 		err := delete(ctx, r.ARN)
 		if err != nil {
-			w.Event(progress.ErrorEvent(r.LogicalID, "Error"))
+			w.Event(progress.ErrorEvent(r.LogicalID))
 			return err
 		}
 		w.Event(progress.RemovedEvent(r.LogicalID))

+ 2 - 6
ecs/wait.go

@@ -101,11 +101,7 @@ func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, op
 					}
 				}
 			}
-			w.Event(progress.Event{
-				ID:         resource,
-				Status:     progressStatus,
-				StatusText: fmt.Sprintf("%s %s", toCamelCase(status), reason),
-			})
+			w.Event(progress.NewEvent(resource, progressStatus, fmt.Sprintf("%s %s", toCamelCase(status), reason)))
 		}
 		if operation != stackCreate || stackErr != nil {
 			continue
@@ -116,7 +112,7 @@ func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, op
 			}
 			stackErr = err
 			operation = stackDelete
-			w.Event(progress.ErrorEvent(name, err.Error()))
+			w.Event(progress.ErrorMessageEvent(name, err.Error()))
 		}
 	}
 

+ 7 - 4
local/compose.go

@@ -120,11 +120,13 @@ func (s *composeService) Down(ctx context.Context, projectName string) error {
 			w.Event(progress.NewEvent(getContainerName(container), progress.Working, "Stopping"))
 			err := s.apiClient.ContainerStop(ctx, container.ID, nil)
 			if err != nil {
+				w.Event(progress.ErrorMessageEvent(getContainerName(container), "Error while Stopping"))
 				return err
 			}
 			w.Event(progress.RemovingEvent(getContainerName(container)))
 			err = s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
 			if err != nil {
+				w.Event(progress.ErrorMessageEvent(getContainerName(container), "Error while Removing"))
 				return err
 			}
 			w.Event(progress.RemovedEvent(getContainerName(container)))
@@ -583,6 +585,7 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi
 			w := progress.ContextWriter(ctx)
 			w.Event(progress.CreatingEvent(networkEventName))
 			if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
+				w.Event(progress.ErrorEvent(networkEventName))
 				return errors.Wrapf(err, "failed to create network %s", n.Name)
 			}
 			w.Event(progress.CreatedEvent(networkEventName))
@@ -599,9 +602,8 @@ func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string
 	w.Event(progress.RemovingEvent(eventName))
 
 	if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
-		msg := fmt.Sprintf("failed to create network %s", networkID)
-		w.Event(progress.ErrorEvent(eventName, "Error: "+msg))
-		return errors.Wrapf(err, msg)
+		w.Event(progress.ErrorEvent(eventName))
+		return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID))
 	}
 
 	w.Event(progress.RemovedEvent(eventName))
@@ -623,10 +625,11 @@ func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeCo
 				Driver:     volume.Driver,
 				DriverOpts: volume.DriverOpts,
 			})
-			w.Event(progress.CreatedEvent(eventName))
 			if err != nil {
+				w.Event(progress.ErrorEvent(eventName))
 				return err
 			}
+			w.Event(progress.CreatedEvent(eventName))
 		}
 		return err
 	}

+ 9 - 4
progress/event.go

@@ -42,11 +42,16 @@ type Event struct {
 	spinner   *spinner
 }
 
-// ErrorEvent creates a new Error Event
-func ErrorEvent(ID string, msg string) Event {
+// ErrorMessageEvent creates a new Error Event with message
+func ErrorMessageEvent(ID string, msg string) Event {
 	return NewEvent(ID, Error, msg)
 }
 
+// ErrorEvent creates a new Error Event
+func ErrorEvent(ID string) Event {
+	return NewEvent(ID, Error, "Error")
+}
+
 // CreatingEvent creates a new Create in progress Event
 func CreatingEvent(ID string) Event {
 	return NewEvent(ID, Working, "Creating...")
@@ -57,12 +62,12 @@ func CreatedEvent(ID string) Event {
 	return NewEvent(ID, Done, "Created")
 }
 
-// CreatingEvent creates a new Create in progress Event
+// RemovingEvent creates a new Removing in progress Event
 func RemovingEvent(ID string) Event {
 	return NewEvent(ID, Working, "Removing...")
 }
 
-// CreatedEvent creates a new Created (done) Event
+// RemovedEvent creates a new removed (done) Event
 func RemovedEvent(ID string) Event {
 	return NewEvent(ID, Done, "Removed")
 }