-
Notifications
You must be signed in to change notification settings - Fork 265
/
handler_inspect.go
297 lines (244 loc) · 8.79 KB
/
handler_inspect.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package metaquery
import (
"context"
"fmt"
"github.com/turbot/steampipe/pkg/db/db_common"
"github.com/turbot/steampipe/pkg/error_helpers"
"github.com/turbot/steampipe/pkg/statushooks"
"log"
"regexp"
"sort"
"strings"
"time"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/display"
"github.com/turbot/steampipe/pkg/steampipeconfig"
)
// inspect
func inspect(ctx context.Context, input *HandlerInput) error {
// load connection state and put into input
connectionState, err := getConnectionState(ctx, input.Client)
if err != nil {
log.Printf("[TRACE] failed to load connection state - are we connected to a server running a previous steampipe version?")
// call legacy inspect
return inspectLegacy(ctx, input)
}
input.ConnectionState = connectionState
// if no args were provided just list connections
if len(input.args()) == 0 {
return listConnections(ctx, input)
}
// so there were args, try to determine what the args are
tableOrConnection := input.args()[0]
if len(input.args()) > 0 {
// this should be one argument, but may have been split by the tokenizer
// because of the escape characters that autocomplete puts in
// join them up
tableOrConnection = strings.Join(input.args(), " ")
}
// remove all double quotes (if any)
tableOrConnection = strings.Join(
strings.Split(tableOrConnection, "\""),
"",
)
// arg can be one of <connection_name> or <connection_name>.<table_name>
tokens := strings.SplitN(tableOrConnection, ".", 2)
// here tokens could be schema.tableName or tableName
if len(tokens) == 1 {
// only a connection name (or maybe unqualified table name)
return inspectSchemaOrUnqualifiedTable(tableOrConnection, input)
}
// this is a fully qualified table name
return inspectQualifiedTable(tokens[0], tokens[1], input)
}
func inspectSchemaOrUnqualifiedTable(tableOrConnection string, input *HandlerInput) error {
// only a connection name (or maybe unqualified table name)
if inspectConnection(tableOrConnection, input) {
return nil
}
// there was no schema
// add the temporary schema to the search_path so that it becomes searchable
// for the next step
searchPath := append(input.SearchPath, input.Schema.TemporarySchemaName)
// go through the searchPath one by one and try to find the table by this name
for _, schema := range searchPath {
tablesInThisSchema := input.Schema.GetTablesInSchema(schema)
// we have a table by this name here
if _, gotTable := tablesInThisSchema[tableOrConnection]; gotTable {
return inspectQualifiedTable(schema, tableOrConnection, input)
}
// check against the fully qualified name of the table
for _, table := range input.Schema.Schemas[schema] {
if tableOrConnection == table.FullName {
return inspectQualifiedTable(schema, table.Name, input)
}
}
}
return fmt.Errorf("could not find connection or table called '%s'. Is the plugin installed? Is the connection configured?", tableOrConnection)
fmt.Printf(`
To get information about the columns in a table, run %s
`, constants.Bold(".inspect {connection}.{table}"))
return nil
}
// helper function to acquire db connection and retrieve connection state
func getConnectionState(ctx context.Context, client db_common.Client) (steampipeconfig.ConnectionStateMap, error) {
statushooks.Show(ctx)
defer statushooks.Done(ctx)
statushooks.SetStatus(ctx, "Loading connection state...")
conn, err := client.AcquireConnection(ctx)
if err != nil {
return nil, err
}
defer conn.Release()
return steampipeconfig.LoadConnectionState(ctx, conn.Conn(), steampipeconfig.WithWaitUntilLoading())
}
// list all the tables in the schema
func listTables(_ context.Context, input *HandlerInput) error {
if len(input.args()) == 0 {
schemas := input.Schema.GetSchemas()
for _, schema := range schemas {
if schema == input.Schema.TemporarySchemaName {
continue
}
fmt.Printf(" ==> %s\n", schema)
inspectConnection(schema, input)
}
fmt.Printf(`
To get information about the columns in a table, run %s
`, constants.Bold(".inspect {connection}.{table}"))
} else {
// could be one of connectionName and {string}*
arg := input.args()[0]
if !strings.HasSuffix(arg, "*") {
inspectConnection(arg, input)
fmt.Println()
return nil
}
// treat this as a wild card
r, err := regexp.Compile(arg)
if err != nil {
return fmt.Errorf("invalid search string %s", arg)
}
header := []string{"Table", "Schema"}
var rows [][]string
for schemaName, schemaDetails := range input.Schema.Schemas {
var tables [][]string
for tableName := range schemaDetails {
if r.MatchString(tableName) {
tables = append(tables, []string{tableName, schemaName})
}
}
sort.SliceStable(tables, func(i, j int) bool {
return tables[i][0] < tables[j][0]
})
rows = append(rows, tables...)
}
display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: true})
}
return nil
}
func listConnections(_ context.Context, input *HandlerInput) error {
header := []string{"connection", "plugin", "state"}
showStateSummary := input.ConnectionState.ConnectionsInState(
constants.ConnectionStateUpdating,
constants.ConnectionStateDeleting,
constants.ConnectionStateError)
var rows [][]string
for connectionName, state := range input.ConnectionState {
// skip disabled connections
if state.Disabled() {
continue
}
row := []string{connectionName, state.Plugin, state.State}
rows = append(rows, row)
}
// sort by connection name
sort.SliceStable(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
if showStateSummary {
showStateSummaryTable(input.ConnectionState)
}
fmt.Printf(`
To get information about the tables in a connection, run %s
To get information about the columns in a table, run %s
`, constants.Bold(".inspect {connection}"), constants.Bold(".inspect {connection}.{table}"))
return nil
}
func showStateSummaryTable(connectionState steampipeconfig.ConnectionStateMap) {
header := []string{"Connection state", "Count"}
var rows [][]string
stateSummary := connectionState.GetSummary()
for _, state := range constants.ConnectionStates {
if connectionsInState := stateSummary[state]; connectionsInState > 0 {
rows = append(rows, []string{state, fmt.Sprintf("%d", connectionsInState)})
}
}
display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
}
func inspectQualifiedTable(connectionName string, tableName string, input *HandlerInput) error {
header := []string{"column", "type", "description"}
var rows [][]string
if input.ConnectionState[connectionName].Disabled() {
error_helpers.ShowWarning(fmt.Sprintf("connection '%s' has schema import disabled", connectionName))
return nil
}
schema, found := input.Schema.Schemas[connectionName]
if !found {
return fmt.Errorf("could not find connection called '%s'. Is the plugin installed? Is the connection configured?\n", connectionName)
}
tableSchema, found := schema[tableName]
if !found {
return fmt.Errorf("could not find table '%s' in '%s'", tableName, connectionName)
}
for _, columnSchema := range tableSchema.Columns {
rows = append(rows, []string{columnSchema.Name, columnSchema.Type, columnSchema.Description})
}
// sort by column name
sort.SliceStable(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
return nil
}
// inspect the connection with the given name
// return whether connectionName was identified as an existing connection
func inspectConnection(connectionName string, input *HandlerInput) bool {
connectionState, connectionFoundInState := input.ConnectionState[connectionName]
if !connectionFoundInState {
return false
}
if connectionState.Disabled() {
error_helpers.ShowWarning(fmt.Sprintf("connection '%s' has schema import disabled", connectionName))
return true
}
// have we loaded the schema for this connection yet?
schema, found := input.Schema.Schemas[connectionName]
var rows [][]string
var header []string
if found {
header = []string{"table", "description"}
for _, tableSchema := range schema {
rows = append(rows, []string{tableSchema.Name, tableSchema.Description})
}
} else {
// just display the connection state
header = []string{"connection", "plugin", "schema mode", "state", "error", "state updated"}
rows = [][]string{{
connectionName,
connectionState.Plugin,
connectionState.SchemaMode,
connectionState.State,
connectionState.Error(),
connectionState.ConnectionModTime.Format(time.RFC3339),
},
}
}
// sort by table name
sort.SliceStable(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
return true
}