github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_common/init_result.go (about)

     1  package db_common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/viper"
     8  	"github.com/turbot/steampipe/pkg/constants"
     9  	"github.com/turbot/steampipe/pkg/error_helpers"
    10  )
    11  
    12  type InitResult struct {
    13  	Error    error
    14  	Warnings []string
    15  	Messages []string
    16  
    17  	// allow overriding of the display functions
    18  	DisplayMessage func(ctx context.Context, m string)
    19  	DisplayWarning func(ctx context.Context, w string)
    20  }
    21  
    22  func (r *InitResult) AddMessage(message string) {
    23  	r.Messages = append(r.Messages, message)
    24  }
    25  
    26  func (r *InitResult) AddWarnings(warnings ...string) {
    27  	r.Warnings = append(r.Warnings, warnings...)
    28  }
    29  
    30  func (r *InitResult) HasMessages() bool {
    31  	return len(r.Warnings)+len(r.Messages) > 0
    32  }
    33  
    34  func (r *InitResult) DisplayMessages() {
    35  	if r.DisplayMessage == nil {
    36  		r.DisplayMessage = func(ctx context.Context, m string) {
    37  			fmt.Println(m)
    38  		}
    39  	}
    40  	if r.DisplayWarning == nil {
    41  		r.DisplayWarning = func(ctx context.Context, w string) {
    42  			error_helpers.ShowWarning(w)
    43  		}
    44  	}
    45  	// do not display message in json or csv output mode
    46  	output := viper.Get(constants.ArgOutput)
    47  	if output == constants.OutputFormatJSON || output == constants.OutputFormatCSV {
    48  		return
    49  	}
    50  	for _, w := range r.Warnings {
    51  		r.DisplayWarning(context.Background(), w)
    52  	}
    53  	for _, m := range r.Messages {
    54  		r.DisplayMessage(context.Background(), m)
    55  	}
    56  }