2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-11-21 00:42:38 +03:00
|
|
|
"context"
|
2023-07-08 18:21:27 +03:00
|
|
|
"database/sql"
|
2023-11-21 00:42:38 +03:00
|
|
|
"slices"
|
2023-07-08 18:21:27 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
|
|
|
"github.com/neilotoole/sq/cli/run"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgm"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/metadata"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newInspectCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
2021-02-22 10:37:00 +03:00
|
|
|
Use: "inspect [@HANDLE|@HANDLE.TABLE|.TABLE]",
|
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
ValidArgsFunction: (&handleTableCompleter{
|
|
|
|
max: 1,
|
|
|
|
}).complete,
|
|
|
|
RunE: execInspect,
|
|
|
|
Short: "Inspect data source schema and stats",
|
|
|
|
Long: `Inspect a data source, or a particular table in a source,
|
2023-04-08 21:09:27 +03:00
|
|
|
listing table details such as column names and row counts, etc.
|
|
|
|
|
|
|
|
NOTE: If a schema is large, it may take some time for the command to complete.
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
If @HANDLE is not provided, the active data source is assumed. If @HANDLE.TABLE
|
|
|
|
is provided, table inspection is performed (as opposed to source inspection).
|
2023-05-19 17:24:18 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
There are several modes of operation, controlled by flags. Each of the following
|
|
|
|
modes apply only to source inspection, not table inspection. When no mode flag
|
|
|
|
is supplied, the default is to show the source metadata and schema.
|
2023-04-08 21:09:27 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
--overview: Displays the source's metadata, but not the schema.
|
2023-06-22 08:48:58 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
--dbprops: Displays database properties for the sources' *underlying* database.
|
|
|
|
|
|
|
|
--catalogs: List the catalogs (databases) available via the source.
|
|
|
|
|
|
|
|
--schemata: List the schemas available in the source's active catalog.
|
|
|
|
|
|
|
|
Use --verbose with --text format to see more detail. The --json and --yaml
|
2023-06-22 08:48:58 +03:00
|
|
|
formats both show extensive detail.`,
|
2023-05-26 06:58:43 +03:00
|
|
|
Example: ` # Inspect active data source.
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq inspect
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
# Inspect @pg1 data source.
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq inspect @pg1
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
# Inspect @pg1 data source, showing verbose output.
|
2022-12-17 06:46:37 +03:00
|
|
|
$ sq inspect -v @pg1
|
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
# Show output in JSON (useful for piping to jq).
|
|
|
|
$ sq inspect --json @pg1
|
|
|
|
|
|
|
|
# Show output in YAML.
|
|
|
|
$ sq inspect --yaml @pg1
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
# Show only the DB properties for @pg1.
|
2023-05-19 17:24:18 +03:00
|
|
|
$ sq inspect --dbprops @pg1
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
# Show only the source metadata (and not schema details).
|
|
|
|
$ sq inspect --overview @pg1
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
# List the schemas in @pg1.
|
|
|
|
$ sq inspect --schemata @pg1
|
|
|
|
|
|
|
|
# List the catalogs in @pg1.
|
|
|
|
$ sq inspect --catalogs @pg1
|
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
# Inspect table "actor" in @pg1 data source.
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq inspect @pg1.actor
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
# Inspect "actor" in active data source.
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq inspect .actor
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
# Inspect a non-default schema in source @my1.
|
|
|
|
$ sq inspect @my1 --src.schema information_schema
|
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
# Inspect piped data.
|
2021-02-22 10:37:00 +03:00
|
|
|
$ cat data.xlsx | sq inspect`,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
addTextFormatFlags(cmd)
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
2023-05-05 20:41:22 +03:00
|
|
|
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
|
2023-05-22 18:08:14 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
cmd.Flags().BoolP(flag.InspectOverview, flag.InspectOverviewShort, false, flag.InspectOverviewUsage)
|
2023-05-19 17:24:18 +03:00
|
|
|
cmd.Flags().BoolP(flag.InspectDBProps, flag.InspectDBPropsShort, false, flag.InspectDBPropsUsage)
|
2023-11-22 21:36:34 +03:00
|
|
|
cmd.Flags().BoolP(flag.InspectCatalogs, flag.InspectCatalogsShort, false, flag.InspectCatalogsUsage)
|
|
|
|
cmd.Flags().BoolP(flag.InspectSchemata, flag.InspectSchemataShort, false, flag.InspectSchemataUsage)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
cmd.MarkFlagsMutuallyExclusive(flag.InspectOverview, flag.InspectDBProps, flag.InspectCatalogs, flag.InspectSchemata)
|
|
|
|
|
|
|
|
cmd.Flags().String(flag.ActiveSchema, "", flag.ActiveSchemaUsage)
|
|
|
|
panicOn(cmd.RegisterFlagCompletionFunc(flag.ActiveSchema,
|
|
|
|
activeSchemaCompleter{getActiveSourceViaArgs}.complete))
|
2024-01-15 04:45:34 +03:00
|
|
|
addOptionFlag(cmd.Flags(), driver.OptIngestCache)
|
2024-01-29 00:55:51 +03:00
|
|
|
|
|
|
|
cmd.Flags().StringP(flag.Input, flag.InputShort, "", flag.InputUsage)
|
|
|
|
panicOn(cmd.Flags().MarkHidden(flag.Input)) // Hide for now; this is mostly used for testing.
|
2021-02-22 10:37:00 +03:00
|
|
|
return cmd
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func execInspect(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := cmd.Context()
|
2023-11-21 00:42:38 +03:00
|
|
|
ru, log := run.FromContext(ctx), lg.FromContext(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
src, table, err := determineInspectTarget(ctx, ru, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// Handle flag.ActiveSchema (--src.schema=SCHEMA). This func will mutate
|
|
|
|
// src's Catalog and Schema fields if appropriate.
|
|
|
|
if err = processFlagActiveSchema(cmd, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
if err = applySourceOptions(cmd, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
grip, err := ru.Grips.Open(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "failed to inspect %s", src.Handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
if table != "" {
|
2023-11-21 00:42:38 +03:00
|
|
|
if flagName, changed := cmdFlagAnyChanged(
|
|
|
|
cmd,
|
|
|
|
flag.InspectCatalogs,
|
|
|
|
flag.InspectSchemata,
|
|
|
|
flag.InspectDBProps,
|
|
|
|
flag.InspectOverview,
|
|
|
|
); changed {
|
|
|
|
return errz.Errorf("flag --%s is not valid when inspecting a table", flagName)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tblMeta *metadata.Table
|
2024-01-15 04:45:34 +03:00
|
|
|
tblMeta, err = grip.TableMetadata(ctx, table)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return ru.Writers.Metadata.TableMetadata(tblMeta)
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
if cmdFlagIsSetTrue(cmd, flag.InspectCatalogs) {
|
|
|
|
var db *sql.DB
|
2024-01-15 04:45:34 +03:00
|
|
|
if db, err = grip.DB(ctx); err != nil {
|
2023-11-21 00:42:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var catalogs []string
|
2024-01-15 04:45:34 +03:00
|
|
|
if catalogs, err = grip.SQLDriver().ListCatalogs(ctx, db); err != nil {
|
2023-11-21 00:42:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentCatalog string
|
|
|
|
if len(catalogs) > 0 {
|
|
|
|
currentCatalog = catalogs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.Sort(catalogs)
|
|
|
|
return ru.Writers.Metadata.Catalogs(currentCatalog, catalogs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmdFlagIsSetTrue(cmd, flag.InspectSchemata) {
|
|
|
|
var db *sql.DB
|
2024-01-15 04:45:34 +03:00
|
|
|
if db, err = grip.DB(ctx); err != nil {
|
2023-11-21 00:42:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var schemas []*metadata.Schema
|
2024-01-15 04:45:34 +03:00
|
|
|
if schemas, err = grip.SQLDriver().ListSchemaMetadata(ctx, db); err != nil {
|
2023-11-21 00:42:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentSchema string
|
2024-01-15 04:45:34 +03:00
|
|
|
if currentSchema, err = grip.SQLDriver().CurrentSchema(ctx, db); err != nil {
|
2023-11-21 00:42:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ru.Writers.Metadata.Schemata(currentSchema, schemas)
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
if cmdFlagIsSetTrue(cmd, flag.InspectDBProps) {
|
2023-07-08 18:21:27 +03:00
|
|
|
var db *sql.DB
|
2024-01-15 04:45:34 +03:00
|
|
|
if db, err = grip.DB(ctx); err != nil {
|
2023-07-08 18:21:27 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-11-21 00:42:38 +03:00
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseDB, db)
|
2023-05-19 17:24:18 +03:00
|
|
|
var props map[string]any
|
2024-01-15 04:45:34 +03:00
|
|
|
if props, err = grip.SQLDriver().DBProperties(ctx, db); err != nil {
|
2023-05-19 17:24:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ru.Writers.Metadata.DBProperties(props)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
overviewOnly := cmdFlagIsSetTrue(cmd, flag.InspectOverview)
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
srcMeta, err := grip.SourceMetadata(ctx, overviewOnly)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "failed to read %s source metadata", src.Handle)
|
|
|
|
}
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
// This is a bit hacky, but it works... if not "--verbose", then just zap
|
2020-08-06 20:58:47 +03:00
|
|
|
// the DBVars, as we usually don't want to see those
|
2023-05-22 18:08:14 +03:00
|
|
|
if !cmdFlagIsSetTrue(cmd, flag.Verbose) {
|
2023-05-19 17:24:18 +03:00
|
|
|
srcMeta.DBProperties = nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
return ru.Writers.Metadata.SourceMetadata(srcMeta, !overviewOnly)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-11-21 00:42:38 +03:00
|
|
|
|
|
|
|
// determineInspectTarget determines the source (and, optionally, table)
|
|
|
|
// to inspect.
|
|
|
|
func determineInspectTarget(ctx context.Context, ru *run.Run, args []string) (
|
|
|
|
src *source.Source, table string, err error,
|
|
|
|
) {
|
|
|
|
coll := ru.Config.Collection
|
|
|
|
if len(args) == 0 {
|
|
|
|
// No args supplied.
|
|
|
|
|
|
|
|
// There are two paths from here:
|
|
|
|
// - There's input on stdin, which we'll inspect, or
|
|
|
|
// - We're inspecting the active src
|
|
|
|
|
|
|
|
// check if there's input on stdin
|
|
|
|
src, err = checkStdinSource(ctx, ru)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if src != nil {
|
|
|
|
// We have a valid source on stdin.
|
|
|
|
|
|
|
|
// Add the source to the set.
|
|
|
|
err = coll.Add(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the stdin pipe data source as the active source,
|
|
|
|
// as it's commonly the only data source the user is acting upon.
|
|
|
|
src, err = coll.SetActive(src.Handle, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No source on stdin. Let's see if there's an active source.
|
|
|
|
src = coll.Active()
|
|
|
|
if src == nil {
|
|
|
|
return nil, "", errz.Errorf("no data source specified and no active data source")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else, we received an argument, which can be one of these forms:
|
|
|
|
// @sakila -- inspect the named source
|
|
|
|
// @sakila.actor -- inspect a table of the named source
|
|
|
|
// .actor -- inspect a table from the active source
|
|
|
|
var handle string
|
|
|
|
handle, table, err = source.ParseTableHandle(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errz.Wrap(err, "invalid input")
|
|
|
|
}
|
|
|
|
|
|
|
|
if handle == "" {
|
|
|
|
src = coll.Active()
|
|
|
|
if src == nil {
|
|
|
|
return nil, "", errz.Errorf("no data source specified and no active data source")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
src, err = coll.Get(handle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src, table, nil
|
|
|
|
}
|