2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/run"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
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-05-19 17:24:18 +03:00
|
|
|
The --dbprops flag shows the database properties for a source's *underlying*
|
|
|
|
database. The flag is disregarded when inspecting a table.
|
|
|
|
|
|
|
|
Use the --verbose flag to see more detail in some output formats.
|
2023-04-08 21:09:27 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
If @HANDLE is not provided, the active data source is assumed.`,
|
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
|
|
|
|
|
|
|
|
# Show DB properties for @pg1.
|
2023-05-19 17:24:18 +03:00
|
|
|
$ sq inspect --dbprops @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-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-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-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
|
|
|
|
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-05-19 17:24:18 +03:00
|
|
|
coll := ru.Config.Collection
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
var src *source.Source
|
|
|
|
var table string
|
|
|
|
var err error
|
|
|
|
|
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-04-26 18:16:42 +03:00
|
|
|
if err = applySourceOptions(cmd, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
dbase, err := ru.Databases.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
|
2021-02-22 10:37:00 +03:00
|
|
|
tblMeta, err = dbase.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-05-19 17:24:18 +03:00
|
|
|
sqlDrvr := dbase.SQLDriver()
|
|
|
|
var props map[string]any
|
|
|
|
if props, err = sqlDrvr.DBProperties(ctx, dbase.DB()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ru.Writers.Metadata.DBProperties(props)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
srcMeta, err := dbase.SourceMetadata(ctx)
|
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-05-19 17:24:18 +03:00
|
|
|
return ru.Writers.Metadata.SourceMetadata(srcMeta)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|