Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 8270026

Browse files
authored
fbsql: add the --csv and --pset flags (#2342)
* fbsql: add the `--csv` flag for CSV output in non-interactive mode * fbsql: add support for the `--pset=VAR[=ARG]` flag
1 parent 8fe7314 commit 8270026

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

cli/cli.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func NewCommand(logdest logger.Logger) *Command {
110110
},
111111

112112
HistoryPath: "",
113+
114+
CSV: false,
113115
},
114116

115117
buffer: newBuffer(),
@@ -167,7 +169,8 @@ func (cmd *Command) run(ctx context.Context) error {
167169
// Check to see if Command needs to run in non-interactive mode.
168170
if len(cmd.Commands) > 0 ||
169171
len(cmd.Files) > 0 ||
170-
cmd.Config.KafkaConfig != "" {
172+
cmd.Config.KafkaConfig != "" ||
173+
cmd.Config.CSV {
171174
cmd.nonInteractiveMode = true
172175
}
173176

@@ -179,7 +182,12 @@ func (cmd *Command) run(ctx context.Context) error {
179182
if err := cmd.setupClient(); err != nil {
180183
return errors.Wrap(err, "setting up client")
181184
}
182-
cmd.printConnInfo()
185+
186+
// Print the connection info.
187+
if !cmd.nonInteractiveMode {
188+
cmd.printConnInfo()
189+
}
190+
183191
if err := cmd.connectToDatabase(cmd.database); err != nil {
184192
cmd.Errorf(errors.Wrap(err, "connecting to database").Error() + "\n")
185193
// We intentionally do not return err here.
@@ -379,9 +387,45 @@ func (cmd *Command) setupConfig() error {
379387

380388
cmd.historyPath = cmd.Config.HistoryPath
381389

390+
// Apply any pset flag arguments.
391+
for _, pset := range cmd.Config.PSets {
392+
if err := cmd.applyPSet(pset); err != nil {
393+
return errors.Wrapf(err, "applying pset: %s", pset)
394+
}
395+
}
396+
397+
// If running with the `--csv` flag, configure things to ensure the output
398+
// is correct (i.e. that it's just the csv).
399+
if cmd.Config.CSV {
400+
cmd.writeOptions.format = formatCSV
401+
}
402+
382403
return nil
383404
}
384405

406+
// applyPSet takes a pset string of the form `arg` or `arg=val` and applies it
407+
// as if the user had run `\pset arg val`. The only difference is that applying
408+
// pset here suppresses any output to stdout.
409+
func (cmd *Command) applyPSet(pset string) error {
410+
// We expect arg to be one of the folowing formats:
411+
// arg
412+
// arg=val
413+
args := strings.SplitN(pset, "=", 2)
414+
415+
// This is kind of hacky, but until we re-think the metaCommand interface to
416+
// take a printer interface somewhere (so we can pass in the nopPrinter
417+
// here), we're just going to discard stdout for the duration of this apply,
418+
// and then set stdout back to its previous writer after the apply.
419+
hold := cmd.stdout
420+
cmd.stdout = io.Discard
421+
defer func() {
422+
cmd.stdout = hold
423+
}()
424+
425+
_, err := newMetaPSet(args).execute(cmd)
426+
return err
427+
}
428+
385429
func (cmd *Command) executeAndWriteQuery(qry query) error {
386430
queryResponse, err := cmd.executeQuery(qry)
387431
if err != nil {

cli/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ type Config struct {
1515
KafkaConfig string `json:"kafka-config"`
1616

1717
HistoryPath string `json:"history-path"`
18+
19+
// CSV (Comma-Separated Values) table output mode.
20+
CSV bool `json:"csv"`
21+
22+
// PSet takes one or more pset arguments of the form: `--pset=VAR[=ARG]`.
23+
PSets []string `json:"pset"`
1824
}
1925

2026
type CloudAuthConfig struct {

cmd/fbsql/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func buildFlags(cmd *cobra.Command, cliCmd *cli.Command) {
5959
flags.StringVar(&cliCmd.Config.CloudAuth.Password, "password", cliCmd.Config.CloudAuth.Password, "Password for FeatureBase Cloud access.")
6060

6161
flags.StringVar(&cliCmd.Config.KafkaConfig, "kafka-config", cliCmd.Config.KafkaConfig, "Kafka configuration file to read from.")
62+
flags.BoolVar(&cliCmd.Config.CSV, "csv", cliCmd.Config.CSV, "CSV (Comma-Separated Values) table output mode.")
63+
flags.StringSliceVar(&cliCmd.Config.PSets, "pset", cliCmd.Config.PSets, "Set printing option VAR to ARG (see \\pset command). Use form: --pset=VAR[=ARG]")
6264

6365
flags.String("config", "", "Configuration file to read from.")
6466
}

0 commit comments

Comments
 (0)