mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-07 17:15:56 +00:00
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.
34 lines
834 B
Go
34 lines
834 B
Go
// Copyright 2021 Molecula Corp. All rights reserved.
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/molecula/featurebase/v3/ctl"
|
|
"github.com/molecula/featurebase/v3/server"
|
|
)
|
|
|
|
var conf *ctl.ConfigCommand
|
|
|
|
func newConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
|
|
conf = ctl.NewConfigCommand(stdin, stdout, stderr)
|
|
Server := server.NewCommand(stdin, stdout, stderr)
|
|
confCmd := &cobra.Command{
|
|
Use: "config",
|
|
Short: "Print the current configuration.",
|
|
Long: `config prints the current configuration to stdout`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
conf.Config = Server.Config
|
|
return considerUsageError(cmd, conf.Run(context.Background()))
|
|
},
|
|
}
|
|
|
|
// Attach flags to the command.
|
|
ctl.BuildServerFlags(confCmd, Server)
|
|
|
|
return confCmd
|
|
}
|