2020-08-06 20:58:47 +03:00
|
|
|
// Package cli implements sq's CLI. The spf13/cobra library
|
2023-04-19 08:28:09 +03:00
|
|
|
// is used, with some notable modifications.
|
|
|
|
//
|
|
|
|
// Although cobra provides excellent functionality, it has some issues.
|
2020-08-06 20:58:47 +03:00
|
|
|
// Most prominently, its documentation suggests reliance
|
|
|
|
// upon package-level constructs for initializing the
|
2020-12-30 22:18:22 +03:00
|
|
|
// command tree (bad for testing).
|
2020-08-06 20:58:47 +03:00
|
|
|
//
|
|
|
|
// Thus, this cmd package deviates from cobra's suggested
|
|
|
|
// usage pattern by eliminating all pkg-level constructs
|
2023-04-19 08:28:09 +03:00
|
|
|
// (which makes testing easier).
|
2020-12-30 22:18:22 +03:00
|
|
|
//
|
2020-08-06 20:58:47 +03:00
|
|
|
// The entry point to this pkg is the Execute function.
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-02-22 10:37:00 +03:00
|
|
|
"sync"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
|
|
|
|
|
|
|
"golang.org/x/exp/slog"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/config"
|
|
|
|
"github.com/neilotoole/sq/cli/output/jsonw"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/cleanup"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2022-12-18 09:07:38 +03:00
|
|
|
func init() { //nolint:gochecknoinits
|
2021-02-22 10:37:00 +03:00
|
|
|
cobra.EnableCommandSorting = false
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// errNoMsg is a sentinel error indicating that a command
|
|
|
|
// has failed, but that no error message should be printed.
|
|
|
|
// This is useful in the case where any error information may
|
|
|
|
// already have been printed as part of the command output.
|
|
|
|
var errNoMsg = errors.New("")
|
|
|
|
|
|
|
|
// Execute builds a RunContext using ctx and default
|
|
|
|
// settings, and invokes ExecuteWith.
|
|
|
|
func Execute(ctx context.Context, stdin *os.File, stdout, stderr io.Writer, args []string) error {
|
2023-04-19 08:28:09 +03:00
|
|
|
rc, err := newDefaultRunContext(ctx, stdin, stdout, stderr, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
printError(rc, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rc.Close() // ok to call rc.Close on nil rc
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
ctx = lg.NewContext(ctx, rc.Log)
|
2021-02-22 10:37:00 +03:00
|
|
|
return ExecuteWith(ctx, rc, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteWith invokes the cobra CLI framework, ultimately
|
|
|
|
// resulting in a command being executed. The caller must
|
|
|
|
// invoke rc.Close.
|
2021-02-22 10:37:00 +03:00
|
|
|
func ExecuteWith(ctx context.Context, rc *RunContext, args []string) error {
|
2023-04-02 22:49:45 +03:00
|
|
|
log := lg.FromContext(ctx)
|
|
|
|
log.Debug("EXECUTE", "args", strings.Join(args, " "))
|
|
|
|
log.Debug("Build info", "build", buildinfo.Info())
|
|
|
|
log.Debug("Config",
|
|
|
|
lga.Version, rc.Config.Version,
|
|
|
|
lga.Path, rc.ConfigStore.Location(),
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
ctx = WithRunContext(ctx, rc)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
rootCmd := newCommandTree(rc)
|
2021-02-22 10:37:00 +03:00
|
|
|
var err error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-12-30 22:18:22 +03:00
|
|
|
// The following is a workaround for the fact that cobra doesn't
|
2023-04-19 08:28:09 +03:00
|
|
|
// currently (as of 2017, so yeah, "currently") support executing
|
|
|
|
// the root command with arbitrary args. That is to say, if you execute:
|
2020-08-06 20:58:47 +03:00
|
|
|
//
|
2021-02-22 10:37:00 +03:00
|
|
|
// $ sq @sakila_sl3.actor
|
2020-08-06 20:58:47 +03:00
|
|
|
//
|
2021-02-22 10:37:00 +03:00
|
|
|
// then cobra will look for a command named "@sakila_sl3.actor",
|
|
|
|
// and when it doesn't find such a command, it returns
|
|
|
|
// an "unknown command" error.
|
|
|
|
//
|
|
|
|
// NOTE: This entire mechanism is ancient. Perhaps cobra
|
|
|
|
// now handles this situation?
|
|
|
|
|
|
|
|
// We need to perform handling for autocomplete
|
|
|
|
if len(args) > 0 && args[0] == "__complete" {
|
|
|
|
if hasMatchingChildCommand(rootCmd, args[1]) {
|
|
|
|
// If there is a matching child command, we let rootCmd
|
|
|
|
// handle it, as per normal.
|
|
|
|
rootCmd.SetArgs(args)
|
|
|
|
} else {
|
|
|
|
// There's no command matching the first argument to __complete.
|
|
|
|
// Therefore, we assume that we want to perform completion
|
|
|
|
// for the "slq" command (which is the pseudo-root command).
|
|
|
|
effectiveArgs := append([]string{"__complete", "slq"}, args[1:]...)
|
|
|
|
rootCmd.SetArgs(effectiveArgs)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-22 10:37:00 +03:00
|
|
|
var cmd *cobra.Command
|
|
|
|
cmd, _, err = rootCmd.Find(args)
|
|
|
|
if err != nil {
|
|
|
|
// This err will be the "unknown command" error.
|
|
|
|
// cobra still returns cmd though. It should be
|
|
|
|
// the root cmd.
|
|
|
|
if cmd == nil || cmd.Name() != rootCmd.Name() {
|
|
|
|
// Not sure if this can happen anymore? Can prob delete?
|
|
|
|
panic(fmt.Sprintf("bad cobra cmd state: %v", cmd))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have args [sq, arg1, arg2] then we redirect
|
|
|
|
// to the "slq" command by modifying args to
|
|
|
|
// look like: [query, arg1, arg2] -- noting that SetArgs
|
|
|
|
// doesn't want the first args element.
|
|
|
|
effectiveArgs := append([]string{"slq"}, args...)
|
2023-04-07 11:00:49 +03:00
|
|
|
if effectiveArgs, err = preprocessFlagArgVars(effectiveArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 10:37:00 +03:00
|
|
|
rootCmd.SetArgs(effectiveArgs)
|
2020-08-06 20:58:47 +03:00
|
|
|
} else {
|
2021-02-22 10:37:00 +03:00
|
|
|
if cmd.Name() == rootCmd.Name() {
|
|
|
|
// Not sure why we have two paths to this, but it appears
|
|
|
|
// that we've found the root cmd again, so again
|
|
|
|
// we redirect to "slq" cmd.
|
2023-04-07 11:00:49 +03:00
|
|
|
effectiveArgs := append([]string{"slq"}, args...)
|
|
|
|
if effectiveArgs, err = preprocessFlagArgVars(effectiveArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rootCmd.SetArgs(effectiveArgs)
|
2021-02-22 10:37:00 +03:00
|
|
|
} else {
|
|
|
|
// It's just a normal command like "sq ls" or such.
|
|
|
|
|
|
|
|
// Explicitly set the args on rootCmd as this makes
|
|
|
|
// cobra happy when this func is executed via tests.
|
|
|
|
// Haven't explored the reason why.
|
|
|
|
rootCmd.SetArgs(args)
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
// Execute rootCmd; cobra will find the appropriate
|
2020-08-06 20:58:47 +03:00
|
|
|
// sub-command, and ultimately execute that command.
|
2021-02-22 10:37:00 +03:00
|
|
|
err = rootCmd.ExecuteContext(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
printError(rc, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
// cobraMu exists because cobra relies upon package-level
|
|
|
|
// constructs. This does not sit well with parallel tests.
|
|
|
|
var cobraMu sync.Mutex
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// newCommandTree builds sq's command tree, returning
|
|
|
|
// the root cobra command.
|
|
|
|
func newCommandTree(rc *RunContext) (rootCmd *cobra.Command) {
|
2021-02-22 10:37:00 +03:00
|
|
|
cobraMu.Lock()
|
|
|
|
defer cobraMu.Unlock()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
rootCmd = newRootCmd()
|
2022-12-30 05:02:10 +03:00
|
|
|
rootCmd.DisableAutoGenTag = true
|
2020-08-06 20:58:47 +03:00
|
|
|
rootCmd.SetOut(rc.Out)
|
|
|
|
rootCmd.SetErr(rc.ErrOut)
|
2022-12-17 07:59:42 +03:00
|
|
|
rootCmd.Flags().SortFlags = false
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// The --help flag must be explicitly added to rootCmd,
|
|
|
|
// or else cobra tries to do its own (unwanted) thing.
|
|
|
|
// The behavior of cobra in this regard seems to have
|
|
|
|
// changed? This particular incantation currently does the trick.
|
2023-04-19 08:28:09 +03:00
|
|
|
rootCmd.Flags().Bool(flag.Help, false, "Show sq help")
|
2022-12-17 06:46:37 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
helpCmd := addCmd(rc, rootCmd, newHelpCmd())
|
2020-08-06 20:58:47 +03:00
|
|
|
rootCmd.SetHelpCommand(helpCmd)
|
|
|
|
|
2022-12-17 07:59:42 +03:00
|
|
|
// From the end user's perspective, slqCmd is *effectively* the
|
|
|
|
// root cmd. We need to perform some trickery to make it output help
|
|
|
|
// such that "sq help" and "sq --help" output the same thing.
|
|
|
|
slqCmd := newSLQCmd()
|
|
|
|
slqCmd.SetHelpFunc(func(command *cobra.Command, i []string) {
|
2022-12-18 07:31:06 +03:00
|
|
|
panicOn(rootCmd.Help())
|
2022-12-17 07:59:42 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
addCmd(rc, rootCmd, slqCmd)
|
2021-02-22 10:37:00 +03:00
|
|
|
|
|
|
|
addCmd(rc, rootCmd, newSrcAddCmd())
|
2023-04-16 01:28:51 +03:00
|
|
|
addCmd(rc, rootCmd, newSrcCommand())
|
|
|
|
addCmd(rc, rootCmd, newGroupCommand())
|
|
|
|
addCmd(rc, rootCmd, newListCmd())
|
|
|
|
addCmd(rc, rootCmd, newMoveCmd())
|
|
|
|
addCmd(rc, rootCmd, newRemoveCmd())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
addCmd(rc, rootCmd, newInspectCmd())
|
|
|
|
addCmd(rc, rootCmd, newPingCmd())
|
2023-04-16 01:28:51 +03:00
|
|
|
addCmd(rc, rootCmd, newSQLCmd())
|
|
|
|
addCmd(rc, rootCmd, newScratchCmd())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
tblCmd := addCmd(rc, rootCmd, newTblCmd())
|
|
|
|
addCmd(rc, tblCmd, newTblCopyCmd())
|
|
|
|
addCmd(rc, tblCmd, newTblTruncateCmd())
|
|
|
|
addCmd(rc, tblCmd, newTblDropCmd())
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
driverCmd := addCmd(rc, rootCmd, newDriverCmd())
|
|
|
|
addCmd(rc, driverCmd, newDriverListCmd())
|
|
|
|
|
|
|
|
configCmd := addCmd(rc, rootCmd, newConfigCmd())
|
|
|
|
addCmd(rc, configCmd, newConfigGetCmd())
|
|
|
|
addCmd(rc, configCmd, newConfigLocationCmd())
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
addCmd(rc, rootCmd, newCompletionCmd())
|
2023-04-16 01:28:51 +03:00
|
|
|
addCmd(rc, rootCmd, newVersionCmd())
|
2022-12-30 05:02:10 +03:00
|
|
|
addCmd(rc, rootCmd, newManCmd())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
return rootCmd
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
// hasMatchingChildCommand returns true if s is a full or prefix
|
|
|
|
// match for any of cmd's children. For example, if cmd has
|
|
|
|
// children [inspect, ls, rm], then "insp" or "ls" would return true.
|
|
|
|
func hasMatchingChildCommand(cmd *cobra.Command, s string) bool {
|
|
|
|
for _, child := range cmd.Commands() {
|
|
|
|
if strings.HasPrefix(child.Name(), s) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// addCmd adds the command returned by cmdFn to parentCmd.
|
2021-02-22 10:37:00 +03:00
|
|
|
func addCmd(rc *RunContext, parentCmd, cmd *cobra.Command) *cobra.Command {
|
|
|
|
cmd.Flags().SortFlags = false
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
if cmd.Name() != "help" {
|
|
|
|
// Don't add the --help flag to the help command.
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().Bool(flag.Help, false, "help for "+cmd.Name())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2022-12-30 05:02:10 +03:00
|
|
|
cmd.DisableAutoGenTag = true
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
|
|
|
|
rc.Cmd = cmd
|
|
|
|
rc.Args = args
|
2021-02-22 10:37:00 +03:00
|
|
|
err := rc.init()
|
2020-08-06 20:58:47 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
runE := cmd.RunE
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-04-19 08:28:09 +03:00
|
|
|
if cmd.Flags().Changed(flag.Version) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// Bit of a hack: flag --version on any command
|
|
|
|
// results in execVersion being invoked
|
2021-02-22 10:37:00 +03:00
|
|
|
return execVersion(cmd, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
return runE(cmd, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We handle the errors ourselves (rather than let cobra do it)
|
|
|
|
cmd.SilenceErrors = true
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
parentCmd.AddCommand(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultLogging returns a log (and its associated closer) if
|
|
|
|
// logging has been enabled via envars.
|
2023-04-02 22:49:45 +03:00
|
|
|
func defaultLogging() (*slog.Logger, *cleanup.Cleanup, error) {
|
2023-04-19 08:28:09 +03:00
|
|
|
truncate, _ := strconv.ParseBool(os.Getenv(config.EnvarLogTruncate))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
logFilePath, ok := os.LookupEnv(config.EnvarLogPath)
|
2020-08-06 20:58:47 +03:00
|
|
|
if !ok || logFilePath == "" || strings.TrimSpace(logFilePath) == "" {
|
|
|
|
return lg.Discard(), nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's try to create the dir holding the logfile... if it already exists,
|
|
|
|
// then os.MkdirAll will just no-op
|
|
|
|
parent := filepath.Dir(logFilePath)
|
2022-12-18 11:35:59 +03:00
|
|
|
err := os.MkdirAll(parent, 0o750)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return lg.Discard(), nil, errz.Wrapf(err, "failed to create parent dir of log file %s", logFilePath)
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
fileFlag := os.O_APPEND
|
2020-08-06 20:58:47 +03:00
|
|
|
if truncate {
|
2023-04-19 08:28:09 +03:00
|
|
|
fileFlag = os.O_TRUNC
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|fileFlag, 0o600)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
return lg.Discard(), nil, errz.Wrapf(err, "unable to open log file: %s", logFilePath)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
clnup := cleanup.New().AddE(logFile.Close)
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
replace := func(groups []string, a slog.Attr) slog.Attr {
|
|
|
|
// We want source to be "pkg/file.go".
|
|
|
|
if a.Key == slog.SourceKey {
|
|
|
|
fp := a.Value.String()
|
|
|
|
a.Value = slog.StringValue(filepath.Join(filepath.Base(filepath.Dir(fp)), filepath.Base(fp)))
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := slog.HandlerOptions{
|
|
|
|
AddSource: true,
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
ReplaceAttr: replace,
|
|
|
|
}
|
|
|
|
|
|
|
|
log := slog.New(opts.NewJSONHandler(logFile))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
return log, clnup, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printError is the centralized function for printing
|
|
|
|
// and logging errors. This func has a lot of (possibly needless)
|
|
|
|
// redundancy; ultimately err will print if non-nil (even if
|
|
|
|
// rc or any of its fields are nil).
|
|
|
|
func printError(rc *RunContext, err error) {
|
|
|
|
log := lg.Discard()
|
|
|
|
if rc != nil && rc.Log != nil {
|
|
|
|
log = rc.Log
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
log.Warn("printError called with nil error")
|
2020-08-06 20:58:47 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-18 07:31:06 +03:00
|
|
|
if errors.Is(err, errNoMsg) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// errNoMsg is a sentinel err that sq doesn't want to print
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-18 07:31:06 +03:00
|
|
|
switch {
|
2020-08-06 20:58:47 +03:00
|
|
|
default:
|
2022-12-18 07:31:06 +03:00
|
|
|
case errors.Is(err, context.Canceled):
|
2023-01-01 09:00:07 +03:00
|
|
|
err = errz.New("canceled")
|
2022-12-18 07:31:06 +03:00
|
|
|
case errors.Is(err, context.DeadlineExceeded):
|
2020-08-06 20:58:47 +03:00
|
|
|
err = errz.New("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd *cobra.Command
|
|
|
|
if rc != nil {
|
|
|
|
cmd = rc.Cmd
|
|
|
|
|
|
|
|
cmdName := "unknown"
|
|
|
|
if cmd != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
cmdName = cmd.Name()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
|
|
|
|
lg.Error(log, "nil command", err, lga.Cmd, cmdName)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-08-08 06:06:56 +03:00
|
|
|
wrtrs := rc.writers
|
2020-08-06 20:58:47 +03:00
|
|
|
if wrtrs != nil && wrtrs.errw != nil {
|
|
|
|
// If we have an errorWriter, we print to it
|
|
|
|
// and return.
|
|
|
|
wrtrs.errw.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else we don't have an errorWriter, so we fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get this far, something went badly wrong in bootstrap
|
|
|
|
// (probably the config is corrupt).
|
|
|
|
// At this point, we could just print err to os.Stderr and be done.
|
|
|
|
// However, our philosophy is to always provide the ability
|
|
|
|
// to output errors in json if possible. So, even though cobra
|
|
|
|
// may not have initialized and our own config may be borked, we
|
|
|
|
// will still try to determine if the user wants the error
|
|
|
|
// in json, specified via flags (by directly using the pflag
|
|
|
|
// package) or via sq config's default output format.
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
var opts *config.Options
|
|
|
|
if rc != nil && rc.Config != nil {
|
|
|
|
var zero config.Options
|
|
|
|
if rc.Config.Options != zero {
|
|
|
|
opts = &rc.Config.Options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// getWriterFormatting works even if cmd is nil
|
2023-04-19 08:28:09 +03:00
|
|
|
fm, _, errOut := getWriterFormatting(cmd, opts, os.Stdout, os.Stderr)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
if bootstrapIsFormatJSON(rc) {
|
|
|
|
// The user wants JSON, either via defaults or flags.
|
|
|
|
jw := jsonw.NewErrorWriter(log, errOut, fm)
|
|
|
|
jw.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user didn't want JSON, so we just print to stderr.
|
|
|
|
if isColorTerminal(os.Stderr) {
|
|
|
|
fm.Error.Fprintln(os.Stderr, "sq: "+err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, "sq: "+err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdFlagChanged returns true if cmd is non-nil and
|
|
|
|
// has the named flag and that flag been changed.
|
|
|
|
func cmdFlagChanged(cmd *cobra.Command, name string) bool {
|
|
|
|
if cmd == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
f := cmd.Flag(name)
|
|
|
|
if f == nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
return f.Changed
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2022-12-25 07:04:18 +03:00
|
|
|
// cmdFlagTrue returns true if flag name has been changed
|
|
|
|
// and the flag value is true.
|
|
|
|
func cmdFlagTrue(cmd *cobra.Command, name string) bool {
|
|
|
|
if !cmdFlagChanged(cmd, name) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := cmd.Flags().GetBool(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// bootstrapIsFormatJSON is a last-gasp attempt to check if the user
|
|
|
|
// supplied --json=true on the command line, to determine if a
|
|
|
|
// bootstrap error (hopefully rare) should be output in JSON.
|
|
|
|
func bootstrapIsFormatJSON(rc *RunContext) bool {
|
|
|
|
// If no RunContext, assume false
|
|
|
|
if rc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultFormat := config.FormatTable
|
|
|
|
if rc.Config != nil {
|
2023-04-19 08:28:09 +03:00
|
|
|
defaultFormat = rc.Config.Options.Format
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If args were provided, create a new flag set and check
|
|
|
|
// for the --json flag.
|
|
|
|
if len(rc.Args) > 0 {
|
2023-04-19 08:28:09 +03:00
|
|
|
flagSet := pflag.NewFlagSet("bootstrap", pflag.ContinueOnError)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
jsonFlag := flagSet.BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
|
|
|
err := flagSet.Parse(rc.Args)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// No --json flag, return true if the config file default is JSON
|
|
|
|
if jsonFlag == nil {
|
|
|
|
return defaultFormat == config.FormatJSON
|
|
|
|
}
|
|
|
|
|
|
|
|
return *jsonFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
// No args, return true if the config file default is JSON
|
|
|
|
return defaultFormat == config.FormatJSON
|
|
|
|
}
|
2022-12-18 07:31:06 +03:00
|
|
|
|
|
|
|
func panicOn(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|