github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/interactive/metaquery/utils.go (about)

     1  package metaquery
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jedib0t/go-pretty/v6/table"
     7  	"github.com/turbot/go-kit/helpers"
     8  )
     9  
    10  // IsMetaQuery returns whether the query is a metaquery
    11  func IsMetaQuery(query string) bool {
    12  	if !strings.HasPrefix(query, ".") {
    13  		return false
    14  	}
    15  
    16  	// try to look for the validator
    17  	cmd, _ := getCmdAndArgs(query)
    18  	_, foundHandler := metaQueryDefinitions[cmd]
    19  
    20  	return foundHandler
    21  }
    22  
    23  // extract the command and arguments from the query string
    24  func getCmdAndArgs(query string) (string, []string) {
    25  	query = strings.TrimSuffix(query, ";")
    26  	split := helpers.SplitByWhitespace(query)
    27  	cmd := split[0]
    28  	args := []string{}
    29  	if len(split) > 1 {
    30  		args = split[1:]
    31  	}
    32  	return cmd, args
    33  }
    34  
    35  // extract the arguments from the query string
    36  func getArguments(query string) []string {
    37  	_, args := getCmdAndArgs(query)
    38  	return args
    39  }
    40  
    41  // build a table from the provided row data
    42  func buildTable(rows [][]string, autoMerge bool) string {
    43  	t := table.NewWriter()
    44  	t.SetStyle(table.StyleDefault)
    45  	t.Style().Options = table.Options{
    46  		DrawBorder:      false,
    47  		SeparateColumns: false,
    48  		SeparateFooter:  false,
    49  		SeparateHeader:  false,
    50  		SeparateRows:    false,
    51  	}
    52  	t.Style().Box.PaddingLeft = ""
    53  
    54  	rowConfig := table.RowConfig{AutoMerge: autoMerge}
    55  
    56  	for _, row := range rows {
    57  		rowObj := table.Row{}
    58  		for _, col := range row {
    59  			rowObj = append(rowObj, col)
    60  		}
    61  		t.AppendRow(rowObj, rowConfig)
    62  	}
    63  	return t.Render()
    64  }