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