diff --git a/README.md b/README.md index 56192cff..73751933 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/zetasql/tools/zetasql-formatter/format.cc b/zetasql/tools/zetasql-formatter/format.cc index 8e94a252..0e3cc2d5 100644 --- a/zetasql/tools/zetasql-formatter/format.cc +++ b/zetasql/tools/zetasql-formatter/format.cc @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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 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 remaining_args(argv + 1, argv + argc);