featurebase/cmd/dax.go
Seebs 3c05f1ff37 Distinguish between usage errors and other errors
Cobra automatically displays usage messages, and also a gratuitous
"Error: [...]" line in some cases, when any error at all occurs
running a command. To suppress the usage message, you have to set
cmd.SilenceUsage to true. But the code that would do this doesn't
have access to it. To address this, we introduce a category of
"usage error", implemented with stdlib error wrapping (%w) and
use errors.Is to check for it. There's also utility functions
to do this checking automatically, or indeed, to handle wrapping
of the ctl.SomethingCommand and handle running it with a suitable
context and everything.

In fact, several of the places we're checking for usage errors,
we can never actually report one, but we're checking consistently
so that if we want to report usage errors, we can.

For instance, server.Start and (dax)server.Start don't ever
return usage errors, right now, but we're checking their responses
anyway.

(cherry picked from commit c681642734)
2022-12-12 09:01:20 -08:00

32 lines
865 B
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package cmd
import (
"io"
"github.com/molecula/featurebase/v3/ctl"
"github.com/molecula/featurebase/v3/dax/server"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// newDAXCommand runs the FeatureBase CLI subcommand for ingesting bulk data.
func newDAXCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
server := server.NewCommand(stdin, stdout, stderr)
daxCmd := &cobra.Command{
Use: "dax",
Short: "Run a collection of DAX services",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if err := server.Start(); err != nil {
return considerUsageError(cmd, errors.Wrap(err, "running server"))
}
return errors.Wrap(server.Wait(), "waiting on Server")
},
}
// Attach flags to the command.
ctl.BuildDAXFlags(daxCmd, server)
return daxCmd
}