2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-07-08 18:21:27 +03:00
|
|
|
"database/sql"
|
|
|
|
|
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"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
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-06-22 08:48:58 +03:00
|
|
|
If @HANDLE is not provided, the active data source is assumed.
|
2023-05-19 17:24:18 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
When flag --overview is true, only the source's' metadata is shown,
|
|
|
|
not the schema. The flag is disregarded when inspecting a table.
|
2023-04-08 21:09:27 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
When flag --dbprops is true, only the database properties for the source's
|
|
|
|
*underlying* database are shown. The flag is disregarded when inspecting a table.
|
|
|
|
|
|
|
|
Use --verbose with the --text format to see more detail. The --json and --yaml
|
|
|
|
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-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-19 03:05:48 +03:00
|
|
|
cmd.Flags().String(flag.ActiveSchema, "", flag.ActiveSchemaUsage)
|
|
|
|
panicOn(cmd.RegisterFlagCompletionFunc(flag.ActiveSchema,
|
|
|
|
activeSchemaCompleter{getActiveSourceViaArgs}.complete))
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
addTextFlags(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)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
cmd.MarkFlagsMutuallyExclusive(flag.InspectOverview, flag.InspectDBProps)
|
|
|
|
|
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-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
var (
|
|
|
|
coll = ru.Config.Collection
|
|
|
|
src *source.Source
|
|
|
|
table string
|
|
|
|
err error
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
if len(args) == 0 {
|
2020-08-06 20:58:47 +03:00
|
|
|
// 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
|
2023-05-19 17:24:18 +03:00
|
|
|
src, err = checkStdinSource(ctx, ru)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if src != nil {
|
|
|
|
// We have a valid source on stdin.
|
|
|
|
|
|
|
|
// Add the source to the set.
|
2023-04-19 08:28:09 +03:00
|
|
|
err = coll.Add(src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-18 04:28:11 +03:00
|
|
|
// Set the stdin pipe data source as the active source,
|
2020-08-06 20:58:47 +03:00
|
|
|
// as it's commonly the only data source the user is acting upon.
|
2023-04-19 08:28:09 +03:00
|
|
|
src, err = coll.SetActive(src.Handle, false)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No source on stdin. Let's see if there's an active source.
|
2023-04-19 08:28:09 +03:00
|
|
|
src = coll.Active()
|
2020-08-06 20:58:47 +03:00
|
|
|
if src == nil {
|
|
|
|
return errz.Errorf("no data source specified and no active data source")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We received an argument, which can be one of these forms:
|
2023-04-26 18:16:42 +03:00
|
|
|
// @sakila -- inspect the named source
|
|
|
|
// @sakila.actor -- inspect a table of the named source
|
|
|
|
// .actor -- inspect a table from the active source
|
2020-08-06 20:58:47 +03:00
|
|
|
var handle string
|
|
|
|
handle, table, err = source.ParseTableHandle(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return errz.Wrap(err, "invalid input")
|
|
|
|
}
|
|
|
|
|
|
|
|
if handle == "" {
|
2023-04-19 08:28:09 +03:00
|
|
|
src = coll.Active()
|
2020-08-06 20:58:47 +03:00
|
|
|
if src == nil {
|
|
|
|
return errz.Errorf("no data source specified and no active data source")
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-19 08:28:09 +03:00
|
|
|
src, err = coll.Get(handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
pool, err := ru.Pools.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 != "" {
|
|
|
|
var tblMeta *source.TableMetadata
|
2023-11-19 05:21:14 +03:00
|
|
|
tblMeta, err = pool.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-05-22 18:08:14 +03:00
|
|
|
if cmdFlagIsSetTrue(cmd, flag.InspectDBProps) {
|
2023-07-08 18:21:27 +03:00
|
|
|
var db *sql.DB
|
2023-11-19 05:21:14 +03:00
|
|
|
if db, err = pool.DB(ctx); err != nil {
|
2023-07-08 18:21:27 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-05-19 17:24:18 +03:00
|
|
|
var props map[string]any
|
2023-11-19 05:21:14 +03:00
|
|
|
sqlDrvr := pool.SQLDriver()
|
2023-07-08 18:21:27 +03:00
|
|
|
if props, err = sqlDrvr.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)
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
srcMeta, err := pool.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
|
|
|
}
|