2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2021-02-22 10:37:00 +03:00
|
|
|
"context"
|
2023-12-06 23:53:24 +03:00
|
|
|
"os"
|
2023-11-19 03:05:48 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/run"
|
|
|
|
"github.com/neilotoole/sq/libsq/ast"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2023-04-30 17:18:56 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/drivertype"
|
2024-01-25 09:29:55 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/location"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// determineSources figures out what the active source is
|
|
|
|
// from any combination of stdin, flags or cfg. It will
|
2023-11-19 03:05:48 +03:00
|
|
|
// mutate ru.Config.Collection as necessary. If requireActive
|
|
|
|
// is true, an error is returned if there's no active source.
|
|
|
|
func determineSources(ctx context.Context, ru *run.Run, requireActive bool) error {
|
2023-05-19 17:24:18 +03:00
|
|
|
cmd, coll := ru.Cmd, ru.Config.Collection
|
2023-11-19 03:05:48 +03:00
|
|
|
activeSrc, err := activeSrcAndSchemaFromFlagsOrConfig(ru)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Note: ^ activeSrc could still be nil
|
|
|
|
|
|
|
|
// check if there's input on stdin
|
2023-05-19 17:24:18 +03:00
|
|
|
stdinSrc, err := checkStdinSource(ctx, ru)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stdinSrc != nil {
|
|
|
|
// We have a valid source on stdin.
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Add the stdin source to coll.
|
|
|
|
err = coll.Add(stdinSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
if !cmdFlagChanged(cmd, flag.ActiveSrc) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// If the user has not explicitly set an active
|
|
|
|
// source via flag, then we set the stdin pipe data
|
|
|
|
// source as the active source.
|
|
|
|
// We do this because the @stdin src is commonly the
|
|
|
|
// only data source the user cares about in a pipe
|
|
|
|
// situation.
|
2023-04-19 08:28:09 +03:00
|
|
|
_, err = coll.SetActive(stdinSrc.Handle, false)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
activeSrc = stdinSrc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
if activeSrc == nil && requireActive {
|
2020-08-06 20:58:47 +03:00
|
|
|
return errz.New(msgNoActiveSrc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// activeSrcAndSchemaFromFlagsOrConfig gets the active source, either
|
2024-02-09 19:08:39 +03:00
|
|
|
// from [flag.ActiveSrc] or from srcs.Active. An error is returned
|
2020-08-06 20:58:47 +03:00
|
|
|
// if the flag src is not found: if the flag src is found,
|
2023-04-19 08:28:09 +03:00
|
|
|
// it is set as the active src on coll. If the flag was not
|
|
|
|
// set and there is no active src in coll, (nil, nil) is
|
2020-08-06 20:58:47 +03:00
|
|
|
// returned.
|
2023-11-19 03:05:48 +03:00
|
|
|
//
|
2024-02-09 19:08:39 +03:00
|
|
|
// This source also checks [flag.ActiveSchema], and changes the catalog/schema
|
2023-11-19 03:05:48 +03:00
|
|
|
// of the source if the flag is set.
|
2024-02-09 19:08:39 +03:00
|
|
|
//
|
|
|
|
// See also: processFlagActiveSchema, verifySourceCatalogSchema.
|
2023-11-19 03:05:48 +03:00
|
|
|
func activeSrcAndSchemaFromFlagsOrConfig(ru *run.Run) (*source.Source, error) {
|
|
|
|
cmd, coll := ru.Cmd, ru.Config.Collection
|
2020-08-06 20:58:47 +03:00
|
|
|
var activeSrc *source.Source
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
if cmdFlagChanged(cmd, flag.ActiveSrc) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// The user explicitly wants to set an active source
|
|
|
|
// just for this query.
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
handle, _ := cmd.Flags().GetString(flag.ActiveSrc)
|
|
|
|
s, err := coll.Get(handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-04-19 08:28:09 +03:00
|
|
|
return nil, errz.Wrapf(err, "flag --%s", flag.ActiveSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
activeSrc, err = coll.SetActive(s.Handle, false)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-19 08:28:09 +03:00
|
|
|
activeSrc = coll.Active()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-11-19 03:05:48 +03:00
|
|
|
|
2024-02-09 19:08:39 +03:00
|
|
|
srcModified, err := processFlagActiveSchema(cmd, activeSrc)
|
|
|
|
if err != nil {
|
2023-11-19 03:05:48 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-09 19:08:39 +03:00
|
|
|
if srcModified {
|
|
|
|
if err = verifySourceCatalogSchema(ru.Cmd.Context(), ru, activeSrc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
return activeSrc, nil
|
|
|
|
}
|
|
|
|
|
2024-02-09 19:08:39 +03:00
|
|
|
// verifySourceCatalogSchema verifies that src's non-empty [source.Source.Catalog]
|
|
|
|
// and [source.Source.Schema] are valid, in that they are referenceable in src's
|
|
|
|
// DB. If both fields are empty, this is a no-op. This function is typically
|
|
|
|
// used when the source's catalog or schema are modified, e.g. via [flag.ActiveSchema].
|
|
|
|
//
|
|
|
|
// See also: processFlagActiveSchema.
|
|
|
|
func verifySourceCatalogSchema(ctx context.Context, ru *run.Run, src *source.Source) error {
|
|
|
|
if src.Catalog == "" && src.Schema == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
db, drvr, err := ru.DB(ctx, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var exists bool
|
|
|
|
if src.Catalog != "" {
|
|
|
|
if exists, err = drvr.CatalogExists(ctx, db, src.Catalog); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return errz.Errorf("%s: catalog {%s} doesn't exist or not referenceable", src.Handle, src.Catalog)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if src.Schema != "" {
|
|
|
|
if exists, err = drvr.SchemaExists(ctx, db, src.Schema); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return errz.Errorf("%s: schema {%s} doesn't exist or not referenceable", src.Handle, src.Schema)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCmdSource gets the source specified in args[0], or if args is empty, the
|
|
|
|
// active source, and calls applySourceOptions. If [flag.ActiveSchema] is set,
|
|
|
|
// the [source.Source.Catalog] and [source.Source.Schema] fields are configured
|
|
|
|
// (and validated) as appropriate.
|
|
|
|
//
|
|
|
|
// See: applySourceOptions, processFlagActiveSchema, verifySourceCatalogSchema.
|
|
|
|
func getCmdSource(cmd *cobra.Command, args []string) (*source.Source, error) {
|
|
|
|
ru := run.FromContext(cmd.Context())
|
|
|
|
|
|
|
|
var src *source.Source
|
|
|
|
var err error
|
|
|
|
if len(args) == 0 {
|
|
|
|
if src = ru.Config.Collection.Active(); src == nil {
|
|
|
|
return nil, errz.New("no active source")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
src, err = ru.Config.Collection.Get(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmdFlagChanged(cmd, flag.ActiveSchema) {
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle flag.ActiveSchema (--src.schema=CATALOG.SCHEMA). This func may
|
|
|
|
// mutate src's Catalog and Schema fields if appropriate.
|
|
|
|
var srcModified bool
|
|
|
|
if srcModified, err = processFlagActiveSchema(cmd, src); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = applySourceOptions(cmd, src); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if srcModified {
|
|
|
|
if err = verifySourceCatalogSchema(cmd.Context(), ru, src); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processFlagActiveSchema processes the --src.schema flag, setting appropriate
|
|
|
|
// [source.Source.Catalog] and [source.Source.Schema] values on activeSrc. If
|
|
|
|
// the src is modified by this function, modified returns true. If
|
|
|
|
// [flag.ActiveSchema] is not set, this is no-op. If activeSrc is nil, an error
|
|
|
|
// is returned.
|
|
|
|
//
|
|
|
|
// See also: verifySourceCatalogSchema.
|
|
|
|
func processFlagActiveSchema(cmd *cobra.Command, activeSrc *source.Source) (modified bool, err error) {
|
2023-11-19 03:05:48 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
|
|
|
if !cmdFlagChanged(cmd, flag.ActiveSchema) {
|
|
|
|
// Nothing to do here
|
2024-02-09 19:08:39 +03:00
|
|
|
return false, nil
|
2023-11-19 03:05:48 +03:00
|
|
|
}
|
|
|
|
if activeSrc == nil {
|
2024-02-09 19:08:39 +03:00
|
|
|
return false, errz.Errorf("active catalog/schema specified via --%s, but active source is nil",
|
2023-11-19 03:05:48 +03:00
|
|
|
flag.ActiveSchema)
|
|
|
|
}
|
|
|
|
|
|
|
|
val, _ := cmd.Flags().GetString(flag.ActiveSchema)
|
|
|
|
if val = strings.TrimSpace(val); val == "" {
|
2024-02-09 19:08:39 +03:00
|
|
|
return false, errz.Errorf("active catalog/schema specified via --%s, but schema is empty",
|
2023-11-19 03:05:48 +03:00
|
|
|
flag.ActiveSchema)
|
|
|
|
}
|
|
|
|
|
|
|
|
catalog, schema, err := ast.ParseCatalogSchema(val)
|
|
|
|
if err != nil {
|
2024-02-09 19:08:39 +03:00
|
|
|
return false, errz.Wrapf(err, "invalid active schema specified via --%s",
|
2023-11-19 03:05:48 +03:00
|
|
|
flag.ActiveSchema)
|
|
|
|
}
|
|
|
|
|
2024-02-09 19:08:39 +03:00
|
|
|
if catalog != activeSrc.Catalog || schema != activeSrc.Schema {
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
drvr, err := ru.DriverRegistry.SQLDriverFor(activeSrc.Type)
|
|
|
|
if err != nil {
|
2024-02-09 19:08:39 +03:00
|
|
|
return false, err
|
2023-11-19 03:05:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if catalog != "" {
|
|
|
|
if !drvr.Dialect().Catalog {
|
2024-02-09 19:08:39 +03:00
|
|
|
return false, errz.Errorf("driver {%s} does not support catalog", activeSrc.Type)
|
2023-11-19 03:05:48 +03:00
|
|
|
}
|
2024-02-09 19:08:39 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
activeSrc.Catalog = catalog
|
|
|
|
}
|
|
|
|
|
|
|
|
if schema != "" {
|
|
|
|
activeSrc.Schema = schema
|
|
|
|
}
|
|
|
|
|
2024-02-09 19:08:39 +03:00
|
|
|
return modified, nil
|
2023-11-19 03:05:48 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// checkStdinSource checks if there's stdin data (on pipe/redirect).
|
|
|
|
// If there is, that pipe is inspected, and if it has recognizable
|
|
|
|
// input, a new source instance with handle @stdin is constructed
|
2022-12-18 03:51:33 +03:00
|
|
|
// and returned. If the pipe has no data (size is zero),
|
|
|
|
// then (nil,nil) is returned.
|
2023-05-19 17:24:18 +03:00
|
|
|
func checkStdinSource(ctx context.Context, ru *run.Run) (*source.Source, error) {
|
|
|
|
f := ru.Stdin
|
2023-12-06 23:53:24 +03:00
|
|
|
fi, err := f.Stat()
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Wrap(err, "failed to get stat on stdin")
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
mode := fi.Mode()
|
|
|
|
log := lg.FromContext(ctx).With(lga.File, fi.Name(), lga.Size, fi.Size(), "mode", mode.String())
|
2023-12-06 23:53:24 +03:00
|
|
|
switch {
|
2024-01-15 04:45:34 +03:00
|
|
|
case os.ModeNamedPipe&mode > 0:
|
2023-12-06 23:53:24 +03:00
|
|
|
log.Info("Detected stdin pipe via os.ModeNamedPipe")
|
|
|
|
case fi.Size() > 0:
|
2024-01-15 04:45:34 +03:00
|
|
|
log.Info("Detected stdin redirect via size > 0")
|
2023-12-06 23:53:24 +03:00
|
|
|
default:
|
|
|
|
log.Info("No stdin data detected")
|
2022-12-18 03:51:33 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// If we got this far, we have input from pipe or redirect.
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
typ := drivertype.None
|
2024-01-15 04:45:34 +03:00
|
|
|
if ru.Cmd.Flags().Changed(flag.IngestDriver) {
|
|
|
|
val, _ := ru.Cmd.Flags().GetString(flag.IngestDriver)
|
2023-11-21 00:42:38 +03:00
|
|
|
typ = drivertype.Type(val)
|
2023-05-19 17:24:18 +03:00
|
|
|
if ru.DriverRegistry.ProviderFor(typ) == nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, errz.Errorf("unknown driver type: %s", typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
if err = ru.Files.AddStdin(ctx, f); err != nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
if typ == drivertype.None {
|
2024-01-15 04:45:34 +03:00
|
|
|
if typ, err = ru.Files.DetectStdinType(ctx); err != nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-21 00:42:38 +03:00
|
|
|
if typ == drivertype.None {
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, errz.New("unable to detect type of stdin: use flag --driver")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
return newSource(
|
|
|
|
ctx,
|
2023-05-19 17:24:18 +03:00
|
|
|
ru.DriverRegistry,
|
2023-05-03 15:36:10 +03:00
|
|
|
typ,
|
|
|
|
source.StdinHandle,
|
|
|
|
source.StdinHandle,
|
|
|
|
options.Options{},
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newSource creates a new Source instance where the
|
|
|
|
// driver type is known. Opts may be nil.
|
2023-11-21 00:42:38 +03:00
|
|
|
func newSource(ctx context.Context, dp driver.Provider, typ drivertype.Type, handle, loc string,
|
2022-12-18 11:35:59 +03:00
|
|
|
opts options.Options,
|
|
|
|
) (*source.Source, error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2023-04-30 17:18:56 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
if opts == nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
log.Debug("Create new data source",
|
|
|
|
lga.Handle, handle,
|
|
|
|
lga.Driver, typ,
|
2024-01-25 09:29:55 +03:00
|
|
|
lga.Loc, location.Redact(loc),
|
2023-04-02 22:49:45 +03:00
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
} else {
|
2023-04-02 22:49:45 +03:00
|
|
|
log.Debug("Create new data source with opts",
|
|
|
|
lga.Handle, handle,
|
|
|
|
lga.Driver, typ,
|
2024-01-25 09:29:55 +03:00
|
|
|
lga.Loc, location.Redact(loc),
|
2023-04-02 22:49:45 +03:00
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
err := source.ValidHandle(handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
drvr, err := dp.DriverFor(typ)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-25 07:04:18 +03:00
|
|
|
src := &source.Source{Handle: handle, Location: loc, Type: typ, Options: opts}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
canonicalSrc, err := drvr.ValidateSource(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return canonicalSrc, nil
|
|
|
|
}
|