sq/cli/cmd_inspect.go

155 lines
3.7 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package cli
import (
"github.com/neilotoole/sq/cli/flag"
2020-08-06 20:58:47 +03:00
"github.com/spf13/cobra"
"github.com/neilotoole/sq/libsq/core/errz"
2020-08-06 20:58:47 +03:00
"github.com/neilotoole/sq/libsq/source"
)
func newInspectCmd() *cobra.Command {
2020-08-06 20:58:47 +03:00
cmd := &cobra.Command{
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,
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.
Use the --verbose flag to see more detail.
If @HANDLE is not provided, the active data source is assumed.`,
2020-08-06 20:58:47 +03:00
Example: ` # inspect active data source
$ sq inspect
2020-08-06 20:58:47 +03:00
# inspect @pg1 data source
$ sq inspect @pg1
# inspect @pg1 data source, showing verbose output
$ sq inspect -v @pg1
# inspect 'actor' in @pg1 data source
$ sq inspect @pg1.actor
# inspect 'actor' in active data source
$ sq inspect .actor
2020-08-06 20:58:47 +03:00
# inspect piped data
$ cat data.xlsx | sq inspect`,
2020-08-06 20:58:47 +03:00
}
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
2020-08-06 20:58:47 +03:00
return cmd
2020-08-06 20:58:47 +03:00
}
func execInspect(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rc := RunContextFrom(ctx)
2020-08-06 20:58:47 +03:00
coll := rc.Config.Collection
2020-08-06 20:58:47 +03:00
var src *source.Source
var table string
var err error
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
src, err = checkStdinSource(ctx, rc)
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.
err = coll.Add(src)
2020-08-06 20:58:47 +03:00
if err != nil {
return err
}
// Collection 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.
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.
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:
// @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 == "" {
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 {
src, err = coll.Get(handle)
2020-08-06 20:58:47 +03:00
if err != nil {
return err
}
}
}
if err = applySourceOptions(cmd, src); err != nil {
return err
}
dbase, err := rc.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
tblMeta, err = dbase.TableMetadata(ctx, table)
2020-08-06 20:58:47 +03:00
if err != nil {
return err
}
2020-08-08 06:06:56 +03:00
return rc.writers.metaw.TableMetadata(tblMeta)
2020-08-06 20:58:47 +03:00
}
meta, 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)
}
// 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
if !cmdFlagTrue(cmd, flag.Verbose) {
2020-08-06 20:58:47 +03:00
meta.DBVars = nil
}
2020-08-08 06:06:56 +03:00
return rc.writers.metaw.SourceMetadata(meta)
2020-08-06 20:58:47 +03:00
}