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

     1  package interactive
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/c-bata/go-prompt"
     8  	"github.com/turbot/go-kit/helpers"
     9  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
    10  	"golang.org/x/exp/maps"
    11  )
    12  
    13  func (c *InteractiveClient) initialiseSuggestionsLegacy() {
    14  	c.initialiseQuerySuggestionsLegacy()
    15  	c.initialiseTableSuggestionsLegacy()
    16  }
    17  
    18  func (c *InteractiveClient) initialiseQuerySuggestionsLegacy() {
    19  	var res []prompt.Suggest
    20  
    21  	workspaceModName := c.initData.Workspace.Mod.Name()
    22  	resourceFunc := func(item modconfig.HclResource) (continueWalking bool, err error) {
    23  		continueWalking = true
    24  
    25  		qp, ok := item.(modconfig.QueryProvider)
    26  		if !ok {
    27  			return
    28  		}
    29  		modTreeItem, ok := item.(modconfig.ModTreeItem)
    30  		if !ok {
    31  			return
    32  		}
    33  		if qp.GetQuery() == nil && qp.GetSQL() == nil {
    34  			return
    35  		}
    36  		rm := item.(modconfig.ResourceWithMetadata)
    37  		if rm.IsAnonymous() {
    38  			return
    39  		}
    40  		isLocal := modTreeItem.GetMod().Name() == workspaceModName
    41  		itemType := item.BlockType()
    42  		// only include global inputs
    43  		if itemType == modconfig.BlockTypeInput {
    44  			if _, ok := c.initData.Workspace.Mod.ResourceMaps.GlobalDashboardInputs[item.Name()]; !ok {
    45  				return
    46  			}
    47  		}
    48  		// special case for query
    49  		if itemType == modconfig.BlockTypeQuery {
    50  			itemType = "named query"
    51  		}
    52  		name := qp.Name()
    53  		if isLocal {
    54  			name = qp.GetUnqualifiedName()
    55  		}
    56  
    57  		res = append(res, c.newSuggestion(itemType, qp.GetDescription(), name))
    58  		return
    59  	}
    60  
    61  	c.workspace().GetResourceMaps().WalkResources(resourceFunc)
    62  
    63  	// sort the suggestions
    64  	sort.Slice(res, func(i, j int) bool {
    65  		return res[i].Text < res[j].Text
    66  	})
    67  	c.suggestions.unqualifiedQueries = res
    68  }
    69  
    70  // initialiseTableSuggestions build a list of schema and table querySuggestions
    71  func (c *InteractiveClient) initialiseTableSuggestionsLegacy() {
    72  
    73  	if c.schemaMetadata == nil {
    74  		return
    75  	}
    76  
    77  	// schema names
    78  	var schemasToAdd []string
    79  	// unqualified table names - initialise to the introspection table names
    80  	var unqualifiedTablesToAddMap = make(map[string]struct{})
    81  	var unqualifiedTablesToAdd []string
    82  
    83  	// keep track of which plugins we have added unqualified tables for
    84  	//pluginSchemaMap := map[string]bool{}
    85  
    86  	for schemaName, schemaDetails := range c.schemaMetadata.Schemas {
    87  		// fully qualified table names
    88  		var qualifiedTablesToAdd []string
    89  		isTemporarySchema := schemaName == c.schemaMetadata.TemporarySchemaName
    90  
    91  		// add the schema into the list of schema
    92  		if !isTemporarySchema {
    93  			schemasToAdd = append(schemasToAdd, schemaName)
    94  		}
    95  
    96  		// add qualified names of all tables
    97  		for tableName := range schemaDetails {
    98  			if !isTemporarySchema {
    99  
   100  				qualifiedTablesToAdd = append(qualifiedTablesToAdd, fmt.Sprintf("%s.%s", schemaName, sanitiseTableName(tableName)))
   101  
   102  				if helpers.StringSliceContains(c.client().GetRequiredSessionSearchPath(), schemaName) {
   103  					unqualifiedTablesToAddMap[tableName] = struct{}{}
   104  				}
   105  			}
   106  		}
   107  
   108  		sort.Strings(qualifiedTablesToAdd)
   109  		var tableSuggestions []prompt.Suggest
   110  		for _, t := range qualifiedTablesToAdd {
   111  			tableSuggestions = append(tableSuggestions, prompt.Suggest{Text: t, Description: "Table", Output: sanitiseTableName(t)})
   112  		}
   113  		c.suggestions.tablesBySchema[schemaName] = tableSuggestions
   114  	}
   115  
   116  	sort.Strings(schemasToAdd)
   117  	for _, schema := range schemasToAdd {
   118  		// we don't need to escape schema names, since schema names are derived from connection names
   119  		// which are validated so that we don't end up with names which need it
   120  		c.suggestions.schemas = append(c.suggestions.schemas, prompt.Suggest{Text: schema, Description: "Schema", Output: schema})
   121  	}
   122  
   123  	unqualifiedTablesToAdd = maps.Keys(unqualifiedTablesToAddMap)
   124  	sort.Strings(unqualifiedTablesToAdd)
   125  	for _, table := range unqualifiedTablesToAdd {
   126  		c.suggestions.unqualifiedTables = append(c.suggestions.unqualifiedTables, prompt.Suggest{Text: table, Description: "Table", Output: sanitiseTableName(table)})
   127  	}
   128  }