@@ -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+
385429func (cmd * Command ) executeAndWriteQuery (qry query ) error {
386430 queryResponse , err := cmd .executeQuery (qry )
387431 if err != nil {
0 commit comments