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

     1  package metaquery
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/turbot/steampipe/pkg/constants"
     7  	"github.com/turbot/steampipe/pkg/display"
     8  	"sort"
     9  	"strings"
    10  )
    11  
    12  // inspect
    13  func inspectLegacy(ctx context.Context, input *HandlerInput) error {
    14  	if len(input.args()) == 0 {
    15  		return listConnectionsLegacy(ctx, input)
    16  	}
    17  	tableOrConnection := input.args()[0]
    18  	if len(input.args()) > 0 {
    19  		// this should be one argument, but may have been split by the tokenizer
    20  		// because of the escape characters that autocomplete puts in
    21  		// join them up
    22  		tableOrConnection = strings.Join(input.args(), " ")
    23  	}
    24  
    25  	// remove all double quotes (if any)
    26  	tableOrConnection = strings.Join(
    27  		strings.Split(tableOrConnection, "\""),
    28  		"",
    29  	)
    30  
    31  	// arg can be one of <connection_name> or <connection_name>.<table_name>
    32  	tokens := strings.SplitN(tableOrConnection, ".", 2)
    33  
    34  	// here tokens could be schema.tablename
    35  	// or table.name
    36  	// or both
    37  
    38  	if len(tokens) > 0 {
    39  		// only a connection name (or maybe unqualified table name)
    40  		schemaFound := inspectConnectionLegacy(tableOrConnection, input)
    41  
    42  		// there was no schema
    43  		if !schemaFound {
    44  			// we couldn't find a schema with the name
    45  			// try a prefix search with the schema name
    46  			// for schema := range input.Schema.Schemas {
    47  			// 	if strings.HasPrefix(tableOrConnection, schema) {
    48  			// 		tableName := strings.TrimPrefix(tableOrConnection, fmt.Sprintf("%s.", schema))
    49  			// 		return inspectTable(schema, tableName, input)
    50  			// 	}
    51  			// }
    52  
    53  			// still here - the last sledge hammer is to go through
    54  			// the schema names one by one
    55  			searchPath := input.Client.GetRequiredSessionSearchPath()
    56  
    57  			// add the temporary schema to the search_path so that it becomes searchable
    58  			// for the next step
    59  			searchPath = append(searchPath, input.Schema.TemporarySchemaName)
    60  
    61  			// go through the searchPath one by one and try to find the table by this name
    62  			for _, schema := range searchPath {
    63  				tablesInThisSchema := input.Schema.GetTablesInSchema(schema)
    64  				// we have a table by this name here
    65  				if _, foundTable := tablesInThisSchema[tableOrConnection]; foundTable {
    66  					return inspectTableLegacy(schema, tableOrConnection, input)
    67  				}
    68  
    69  				// check against the fully qualified name of the table
    70  				for _, table := range input.Schema.Schemas[schema] {
    71  					if tableOrConnection == table.FullName {
    72  						return inspectTableLegacy(schema, table.Name, input)
    73  					}
    74  				}
    75  			}
    76  
    77  			return fmt.Errorf("could not find connection or table called '%s'. Is the plugin installed? Is the connection configured?", tableOrConnection)
    78  		}
    79  
    80  		fmt.Printf(`
    81  To get information about the columns in a table, run %s
    82  	
    83  `, constants.Bold(".inspect {connection}.{table}"))
    84  		return nil
    85  	}
    86  
    87  	// this is a fully qualified table name
    88  	return inspectTableLegacy(tokens[0], tokens[1], input)
    89  }
    90  
    91  func listConnectionsLegacy(ctx context.Context, input *HandlerInput) error {
    92  	header := []string{"connection", "plugin"}
    93  	var rows [][]string
    94  
    95  	for _, schema := range input.Schema.GetSchemas() {
    96  		if schema == input.Schema.TemporarySchemaName {
    97  			continue
    98  		}
    99  		rows = append(rows, []string{schema, ""})
   100  
   101  	}
   102  
   103  	// sort by connection name
   104  	sort.SliceStable(rows, func(i, j int) bool {
   105  		return rows[i][0] < rows[j][0]
   106  	})
   107  
   108  	display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
   109  
   110  	fmt.Printf(`
   111  To get information about the tables in a connection, run %s
   112  To get information about the columns in a table, run %s
   113  
   114  `, constants.Bold(".inspect {connection}"), constants.Bold(".inspect {connection}.{table}"))
   115  
   116  	return nil
   117  }
   118  
   119  func inspectConnectionLegacy(connectionName string, input *HandlerInput) bool {
   120  	header := []string{"table", "description"}
   121  	var rows [][]string
   122  
   123  	schema, found := input.Schema.Schemas[connectionName]
   124  
   125  	if !found {
   126  		return false
   127  	}
   128  
   129  	for _, tableSchema := range schema {
   130  		rows = append(rows, []string{tableSchema.Name, tableSchema.Description})
   131  	}
   132  
   133  	// sort by table name
   134  	sort.SliceStable(rows, func(i, j int) bool {
   135  		return rows[i][0] < rows[j][0]
   136  	})
   137  
   138  	display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
   139  
   140  	return true
   141  }
   142  
   143  func inspectTableLegacy(connectionName string, tableName string, input *HandlerInput) error {
   144  	header := []string{"column", "type", "description"}
   145  	rows := [][]string{}
   146  
   147  	schema, found := input.Schema.Schemas[connectionName]
   148  	if !found {
   149  		return fmt.Errorf("Could not find connection called '%s'", connectionName)
   150  	}
   151  	tableSchema, found := schema[tableName]
   152  	if !found {
   153  		return fmt.Errorf("Could not find table '%s' in '%s'", tableName, connectionName)
   154  	}
   155  
   156  	for _, columnSchema := range tableSchema.Columns {
   157  		rows = append(rows, []string{columnSchema.Name, columnSchema.Type, columnSchema.Description})
   158  	}
   159  
   160  	// sort by column name
   161  	sort.SliceStable(rows, func(i, j int) bool {
   162  		return rows[i][0] < rows[j][0]
   163  	})
   164  
   165  	display.ShowWrappedTable(header, rows, &display.ShowWrappedTableOptions{AutoMerge: false})
   166  
   167  	return nil
   168  }