featurebase/ctl/cli.go
Travis Turner 393721c0ce
Add viper (for env variable) support to CLI (#2251)
* Add viper (for env variable) support to CLI

* Move the "featurebase cli" sub-command to its own "fbsql" command

I don't know if this is the final name, but putting it here as a
placeholder for now.

* Handle single `--command` flags.

This also adds a printer interface so we can opt NOT to print setup
information in non-interactive mode.

* Add support for multiple `--command` flags in the same call

* Add support for multiple `--file` flags

* Move members related to Config into a separate struct

* Make sure non-interactive mode can connect to a database

* comment fix

* support control-C on readline

* Prevent connection message from printing in non-interactive mode

* Return errors (instead of printing them) in non-interactive mode
2023-02-16 20:41:39 -06:00

39 lines
2 KiB
Go

package ctl
import (
"github.com/featurebasedb/featurebase/v3/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// BuildCLIFlags attaches a set of flags to the command for a cli instance.
func BuildCLIFlags(cmd *cobra.Command, cliCmd *cli.Command) {
flags := cmd.Flags()
// Base struct flags.
flags.StringSliceVarP(&cliCmd.Commands, "command", "c", cliCmd.Commands, "Command to run in non-interactive mode. Provide multiple flags to execute more than one command. All `--command` flags run before all `--file` flags.")
flags.StringSliceVarP(&cliCmd.Files, "file", "f", cliCmd.Files, "File to run in non-interactive mode. Provide multiple flags to execute more than one file. All `--command` flags run before all `--file` flags.")
// Config flags.
flags.AddFlagSet(cliConfigFlagSet(cliCmd.Config))
}
// cliConfigFlagSet returns a pflag.FlagSet for the CLI Config struct.
func cliConfigFlagSet(cfg *cli.Config) *pflag.FlagSet {
flags := pflag.NewFlagSet("cli", pflag.ExitOnError)
flags.StringVarP(&cfg.Host, "host", "", cfg.Host, "hostname of FeatureBase.")
flags.StringVarP(&cfg.Port, "port", "", cfg.Port, "port of FeatureBase.")
flags.StringVar(&cfg.HistoryPath, "history-path", cfg.HistoryPath, "path for history files.")
flags.StringVar(&cfg.OrganizationID, "org-id", cfg.OrganizationID, "OrganizationID.")
flags.StringVar(&cfg.Database, "db", cfg.Database, "Name of the database to connect to.")
flags.StringVar(&cfg.CloudAuth.ClientID, "client-id", cfg.CloudAuth.ClientID, "Cognito Client ID for FeatureBase Cloud access.")
flags.StringVar(&cfg.CloudAuth.Region, "region", cfg.CloudAuth.Region, "Cloud region for FeatureBase Cloud access (e.g. us-east-2).")
flags.StringVar(&cfg.CloudAuth.Email, "email", cfg.CloudAuth.Email, "Email address for FeatureBase Cloud access.")
flags.StringVar(&cfg.CloudAuth.Password, "password", cfg.CloudAuth.Password, "Password for FeatureBase Cloud access.")
flags.String("config", "", "Configuration file to read from.")
return flags
}