github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/refresh_connections_result.go (about)

     1  package steampipeconfig
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/turbot/steampipe/pkg/error_helpers"
     8  	"github.com/turbot/steampipe/pkg/utils"
     9  )
    10  
    11  // RefreshConnectionResult is a structure used to contain the result of either a RefreshConnections or a NewLocalClient operation
    12  type RefreshConnectionResult struct {
    13  	error_helpers.ErrorAndWarnings
    14  	UpdatedConnections bool
    15  	FailedConnections  map[string]string
    16  }
    17  
    18  func NewErrorRefreshConnectionResult(err error) *RefreshConnectionResult {
    19  	return &RefreshConnectionResult{ErrorAndWarnings: error_helpers.NewErrorsAndWarning(err)}
    20  }
    21  
    22  func (r *RefreshConnectionResult) Merge(other *RefreshConnectionResult) {
    23  	if other == nil {
    24  		return
    25  	}
    26  	if other.UpdatedConnections {
    27  		r.UpdatedConnections = other.UpdatedConnections
    28  	}
    29  	if other.Error != nil {
    30  		r.Error = other.Error
    31  	}
    32  	r.Warnings = append(r.Warnings, other.Warnings...)
    33  	for c, err := range other.FailedConnections {
    34  		if _, ok := r.FailedConnections[c]; !ok {
    35  			r.AddFailedConnection(c, err)
    36  		}
    37  	}
    38  }
    39  
    40  func (r *RefreshConnectionResult) String() string {
    41  	var op strings.Builder
    42  	if len(r.Warnings) > 0 {
    43  		op.WriteString(fmt.Sprintf("%s:\n\t%s\n", utils.Pluralize("Warning", len(r.Warnings)), strings.Join(r.Warnings, "\n\t")))
    44  	}
    45  	if r.Error != nil {
    46  		op.WriteString(fmt.Sprintf("%s\n", r.Error.Error()))
    47  	}
    48  	op.WriteString(fmt.Sprintf("UpdatedConnections: %v\n", r.UpdatedConnections))
    49  	return op.String()
    50  }
    51  
    52  func (r *RefreshConnectionResult) AddFailedConnection(c string, failure string) {
    53  	if r.FailedConnections == nil {
    54  		r.FailedConnections = make(map[string]string)
    55  	}
    56  
    57  	r.FailedConnections[c] = failure
    58  }