Skip to content

Commit cc95793

Browse files
committed
Simplfiy command line arguments
1 parent 5d441bb commit cc95793

File tree

3 files changed

+75
-118
lines changed

3 files changed

+75
-118
lines changed

Makefile.toml

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ script = '''
1616
set -euo pipefail
1717
COMMAND="cargo run -q -r --bin sqlsonnet --"
1818
eval $COMMAND --help
19-
eval $COMMAND to-sql --help
20-
eval $COMMAND from-sql --help
2119
'''
2220

2321
[tasks.cross]

README.md

+27-47
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,36 @@ See also the official [Jsonnet Tools page](https://jsonnet.org/learning/tools.ht
6060
The `sqlsonnet` command line interface converts Jsonnet statements to (and to a lesser extent from) SQL.
6161

6262
```
63-
Usage: sqlsonnet [OPTIONS] <COMMAND>
64-
65-
Commands:
66-
from-sql Convert SQL to Jsonnet
67-
to-sql Convert Jsonnet to SQL
68-
help Print this message or the help of the given subcommand(s)
69-
70-
Options:
71-
--theme <THEME> Color theme for syntax highlighting [env: SQLSONNET_THEME=Nord] [possible values: 1337, Coldark-Cold, Coldark-Dark, DarkNeon, Dracula, GitHub, "Monokai Extended", "Monokai Extended Bright", "Monokai Extended Light", "Monokai Extended Origin", Nord, OneHalfDark, OneHalfLight, "Solarized (dark)", "Solarized (light)", "Sublime Snazzy", TwoDark, "Visual Studio Dark+", ansi, base16, base16-256, gruvbox-dark, gruvbox-light, zenburn]
72-
-c, --compact Compact SQL representation
73-
-h, --help Print help
74-
-V, --version Print version
75-
```
76-
77-
#### Jsonnet to SQL (`to-sql`)
78-
79-
```text
80-
Usage: sqlsonnet to-sql [OPTIONS] <INPUT>
63+
Usage: sqlsonnet [OPTIONS] <INPUT>
8164
8265
Arguments:
8366
<INPUT> Input file (path or - for stdin)
8467
8568
Options:
69+
--theme <THEME>
70+
Color theme for syntax highlighting [env: SQLSONNET_THEME=Nord] [possible values: 1337, Coldark-Cold, Coldark-Dark, DarkNeon, Dracula, GitHub, "Monokai Extended", "Monokai Extended Bright", "Monokai Extended Light", "Monokai Extended Origin", Nord, OneHalfDark, OneHalfLight, "Solarized (dark)", "Solarized (light)", "Sublime Snazzy", TwoDark, "Visual Studio Dark+", ansi, base16, base16-256, gruvbox-dark, gruvbox-light, zenburn]
71+
-c, --compact
72+
Compact SQL representation
73+
-f, --from-sql
74+
Convert an SQL file into Jsonnet
75+
--diff
76+
With --from-sql: Convert back to SQL and print the differences with the original, if any
8677
--display-format <DISPLAY_FORMAT>
87-
Display the converted SQL, the intermediary Json, or the original Jsonnet [default: sql] [possible values: sql, jsonnet, json]
78+
[possible values: sql, jsonnet, json]
79+
-h, --help
80+
Print help
81+
-V, --version
82+
Print version
83+
```
84+
85+
#### Jsonnet to SQL
86+
87+
```console
88+
$ sqlsonnet test.jsonnet
89+
$ # stdin input is also supported
90+
$ cat test.jsonnet | sqlsonnet -
91+
$ # Piping into clickhouse client
92+
$ sqlsonnet test.jsonnet | clickhouse client -f PrettyMonoBlock --multiquery --host ... --user ...
8893
```
8994

9095
The input should represent a list of queries, e.g.
@@ -99,42 +104,17 @@ The [embedded utility functions](sqlsonnet/utils.libsonnet) are automatically im
99104
local u = import "sqlsonnet.libsonnet";
100105
```
101106

102-
_Examples_
107+
#### SQL to Jsonnet (`from-sql`)
103108

104109
```console
105-
$ sqlsonnet to-sql test.jsonnet
106-
$ # stdin input is also supported
107-
$ cat test.jsonnet | sqlsonnet to-sql -
108-
$ # Piping into clickhouse client
109-
$ sqlsonnet to-sql test.jsonnet | clickhouse client -f PrettyMonoBlock --multiquery --host ... --user ...
110+
$ sqlsonnet --from-sql test.sql
111+
$ cat test.sql | sqlsonnet --from-sql -
110112
```
111113

112-
#### SQL to Jsonnet (`from-sql`)
113-
114114
This mode is useful to discover the sqlsonnet syntax from SQL queries.
115115

116116
The parser is far from perfect. Expressions are parsed as long as subqueries are encountered; then they are simply represented as strings. The results do not use the [embedded utility functions](sqlsonnet/utils.libsonnet), which can significantly simplify expressions.
117117

118-
```
119-
Usage: sqlsonnet from-sql [OPTIONS] <INPUT>
120-
121-
Arguments:
122-
<INPUT> Input file (path or - for stdin)
123-
124-
Options:
125-
--display-format <DISPLAY_FORMAT>
126-
Display the converted Jsonnet output and/or the SQL roundtrip [default: jsonnet] [possible values: sql, jsonnet, json]
127-
--diff
128-
Convert back to SQL and print the differences with the original, if any
129-
```
130-
131-
_Examples_
132-
133-
```console
134-
$ sqlsonnet from-sql test.sql
135-
$ cat test.sql| sqlsonnet from-sql -
136-
```
137-
138118
## Syntax
139119

140120
```jsonnet

sqlsonnet-cli/src/sqlsonnet.rs

+48-69
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,19 @@ struct Flags {
3131
#[clap(long, env = "SQLSONNET_THEME",
3232
value_parser=clap::builder::PossibleValuesParser::new(THEMES.iter().map(|s| s.as_str())))]
3333
theme: Option<String>,
34-
#[clap(subcommand)]
35-
command: Command,
3634
/// Compact SQL representation
3735
#[clap(long, short)]
3836
compact: bool,
39-
}
40-
41-
#[derive(Parser)]
42-
enum Command {
43-
/// Convert SQL to Jsonnet
44-
#[clap(alias = "f")]
45-
FromSql {
46-
/// Input file (path or - for stdin).
47-
input: Input,
48-
#[clap(long, value_delimiter = ',', default_value = "jsonnet")]
49-
/// Display the converted Jsonnet output and/or the SQL roundtrip
50-
display_format: Vec<Language>,
51-
/// Convert back to SQL and print the differences with the original, if any
52-
#[clap(long)]
53-
diff: bool,
54-
},
55-
/// Convert Jsonnet to SQL
56-
#[clap(alias = "t")]
57-
ToSql {
58-
/// Input file (path or - for stdin).
59-
input: Input,
60-
#[clap(long, value_delimiter = ',', default_value = "sql")]
61-
/// Display the converted SQL, the intermediary Json, or the original Jsonnet.
62-
display_format: Vec<Language>,
63-
},
37+
/// Input file (path or - for stdin).
38+
input: Input,
39+
/// Convert an SQL file into Jsonnet.
40+
#[clap(long, short)]
41+
from_sql: bool,
42+
/// With --from-sql: Convert back to SQL and print the differences with the original, if any.
43+
#[clap(long, requires = "from_sql")]
44+
diff: bool,
45+
#[clap(long, value_delimiter = ',')]
46+
display_format: Option<Vec<Language>>,
6447
}
6548

6649
#[derive(Clone)]
@@ -161,49 +144,45 @@ fn main_impl() -> Result<(), Error> {
161144
)
162145
}))?;
163146

164-
match &args.command {
165-
Command::ToSql {
166-
input,
167-
display_format,
168-
} => {
169-
let filename = input.filename();
170-
let contents = input.contents()?;
171-
let contents = sqlsonnet::import_utils() + &contents;
172-
info!("Converting Jsonnet file {} to SQL", filename);
173-
174-
// TODO: Support passing a single query.
175-
let queries = Queries::from_jsonnet(&contents, input.resolver())?;
176-
177-
let has = |l| display_format.iter().any(|l2| l2 == &l);
178-
// Display queries
179-
debug!("{:#?}", queries);
180-
if has(Language::Jsonnet) {
181-
highlight(&contents, Language::Jsonnet, &args)?;
182-
}
183-
if has(Language::Sql) {
184-
highlight(queries.to_sql(args.compact), Language::Sql, &args)?;
185-
}
147+
let display_format = args.display_format.clone().unwrap_or_else(|| {
148+
vec![if args.from_sql {
149+
Language::Jsonnet
150+
} else {
151+
Language::Sql
152+
}]
153+
});
154+
let filename = args.input.filename();
155+
let input = args.input.contents()?;
156+
if args.from_sql {
157+
info!("Converting SQL file {}", filename);
158+
let queries = Queries::from_sql(&input)?;
159+
let has = |l| display_format.iter().any(|l2| l2 == &l);
160+
let sql = queries.to_sql(args.compact);
161+
if has(Language::Sql) {
162+
highlight(&sql, Language::Sql, &args)?;
163+
}
164+
if has(Language::Jsonnet) {
165+
let jsonnet = queries.as_jsonnet();
166+
highlight(jsonnet, Language::Jsonnet, &args)?;
167+
}
168+
if args.diff && input != sql {
169+
println!("{}", pretty_assertions::StrComparison::new(&input, &sql));
170+
}
171+
} else {
172+
let contents = sqlsonnet::import_utils() + &input;
173+
info!("Converting Jsonnet file {} to SQL", filename);
174+
175+
// TODO: Support passing a single query.
176+
let queries = Queries::from_jsonnet(&contents, args.input.resolver())?;
177+
178+
let has = |l| display_format.iter().any(|l2| l2 == &l);
179+
// Display queries
180+
debug!("{:#?}", queries);
181+
if has(Language::Jsonnet) {
182+
highlight(&contents, Language::Jsonnet, &args)?;
186183
}
187-
Command::FromSql {
188-
input,
189-
display_format,
190-
diff,
191-
} => {
192-
info!("Converting SQL file {}", input.filename());
193-
let input = input.contents()?;
194-
let queries = Queries::from_sql(&input)?;
195-
let has = |l| display_format.iter().any(|l2| l2 == &l);
196-
let sql = queries.to_sql(args.compact);
197-
if has(Language::Sql) {
198-
highlight(&sql, Language::Sql, &args)?;
199-
}
200-
if has(Language::Jsonnet) {
201-
let jsonnet = queries.as_jsonnet();
202-
highlight(jsonnet, Language::Jsonnet, &args)?;
203-
}
204-
if *diff && input != sql {
205-
println!("{}", pretty_assertions::StrComparison::new(&input, &sql));
206-
}
184+
if has(Language::Sql) {
185+
highlight(queries.to_sql(args.compact), Language::Sql, &args)?;
207186
}
208187
}
209188

0 commit comments

Comments
 (0)