Skip to content

Commit

Permalink
Merge pull request #16 from Matts966/feature/cincout
Browse files Browse the repository at this point in the history
Support std::cin to std::cout
  • Loading branch information
Matts966 authored Apr 18, 2022
2 parents 5988095 + 85c8432 commit 682bcb4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,42 @@ wget https://github.com/Matts966/zetasql-formatter/releases/latest/download/zeta
```

```bash
# To apply formatter
zetasql-formatter [paths]
# To apply formatter for files
$ zetasql-formatter [files and directories]

# Format stdin
$ echo "select * from test" | zetasql-formatter
SELECT
*
FROM
test;

$ zetasql-formatter
select * from ok;
-- CTRL-D
SELECT
*
FROM
ok;
-- CTRL-D
```
## Integration with [efm-langserver](https://github.com/mattn/efm-langserver)
- Install efm-langserver
- Locate [`config.yaml`](https://github.com/mattn/efm-langserver#example-for-configyaml) like below
```yaml
version: 2
tools:
zetasql-formatter: &zetasql-formatter
format-command: zetasql-formatter
format-stdin: true
languages:
sql:
- <<: *zetasql-formatter
sql-bigquery:
- <<: *zetasql-formatter
```
## License
Expand Down
11 changes: 10 additions & 1 deletion zetasql/tools/zetasql-formatter/format.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <fstream>
Expand Down Expand Up @@ -43,7 +44,15 @@ int main(int argc, char* argv[]) {
gflags::SetVersionString(ZSQL_FMT_VERSION_STRING);
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (argc <= 1) {
std::cerr << kUsage << std::endl;
std::istreambuf_iterator<char> begin(std::cin), end;
std::string sql(begin, end);
std::string formatted;
const absl::Status status = zetasql::FormatSql(sql, &formatted);
if (status.ok()) {
std::cout << formatted;
return 0;
}
std::cerr << "ERROR: " << status << std::endl;
return 1;
}
std::vector<std::string> remaining_args(argv + 1, argv + argc);
Expand Down

0 comments on commit 682bcb4

Please sign in to comment.