darold / pgFormatter

A PostgreSQL SQL syntax beautifier that can work as a console program or as a CGI. On-line demo site at http://sqlformat.darold.net/
PostgreSQL License
1.66k stars 100 forks source link

Can't use -i with globs #293

Closed yolken-airplane closed 2 years ago

yolken-airplane commented 2 years ago

With the latest release, we're having trouble using the -i flag with glob inputs:

Benjamin-Yolkens-MacBook-Pro:airport benjaminyolken$ pg_format -i db/migrations/*.sql
Error: can not use -i | --inplace with an output file (-o | --output) .

Usage: pg_format [options] file.sql

    PostgreSQL SQL queries and PL/PGSQL code beautifier.
...

This worked fine in the previous version. Is there any workaround for this? I've tried setting -o to an empty string and a few other things (e.g., -) without luck. Thanks!

darold commented 2 years ago

Are you using v5.3? This is the last release and I'm not able to reproduce the issue.

yolken-airplane commented 2 years ago

Thanks for the quick reply. Yes, I'm using v5.3 on a M1 Mac. Here's a printout of my various versions:

pg_format

$ pg_format --version
pg_format version 5.3

perl

$ perl --version

This is perl 5, version 30, subversion 3 (v5.30.3) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)

Copyright 1987-2020, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

And here's a quick way to reproduce (on my system, at least):

$ mkdir sql
$ echo 'select * from my_table' >> sql/test1.sql
$ echo 'select * from my_table' >> sql/test2.sql
$ pg_format -i sql/*.sql
Error: can not use -i | --inplace with an output file (-o | --output) .

...

I also tested in an Ubuntu Docker container and got the same error.

In each case, it does seem to format the first file in-place correctly, but it throws that error when processing the second one. If the glob only expands out to a single file, then it's also ok.

darold commented 2 years ago

You can not use multiple file as input, here how to achieve what you want to do:

ls sql/*.sql | xargs -i perl pg_format -i {}

but I agree that the error message is not clear, I will fix that.

darold commented 2 years ago

Commit 73247ab adds the possibility to format inplace multiple files. You can now use pg_format -i sql/*.sql