@@ -31,36 +31,19 @@ struct Flags {
31
31
#[ clap( long, env = "SQLSONNET_THEME" ,
32
32
value_parser=clap:: builder:: PossibleValuesParser :: new( THEMES . iter( ) . map( |s| s. as_str( ) ) ) ) ]
33
33
theme : Option < String > ,
34
- #[ clap( subcommand) ]
35
- command : Command ,
36
34
/// Compact SQL representation
37
35
#[ clap( long, short) ]
38
36
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 > > ,
64
47
}
65
48
66
49
#[ derive( Clone ) ]
@@ -161,49 +144,45 @@ fn main_impl() -> Result<(), Error> {
161
144
)
162
145
} ) ) ?;
163
146
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) ?;
186
183
}
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) ?;
207
186
}
208
187
}
209
188
0 commit comments