func WaitForOperation()

in internal/daemon/wait.go [42:72]


func WaitForOperation(ctx context.Context, op AsyncOperation, name string, opts ...OperationOption) error {
	o := &OperationOptions{}
	for _, opt := range opts {
		opt(o)
	}

	if o.Result != nil {
		return errors.New("cannot specify a result channel when waiting for an operation")
	}

	result := make(chan OperationResult)
	o.Result = result

	if err := op(ctx, name, o.ApplyAll); err != nil {
		return err
	}

	select {
	case <-ctx.Done():
		return fmt.Errorf("operation for daemon %s did not complete in time, result is unknown: %w", name, ctx.Err())
	case res := <-result:
		switch res {
		case Failed, Timeout:
			return fmt.Errorf("operation for daemon %s failed with result [%s]", name, res)
		case Done, Canceled, Dependency, Skipped:
			return nil
		}
	}

	return nil
}