11extern crate clap;
22extern crate graph_store_postgres;
33
4- use clap:: { Command , arg } ;
4+ use clap:: { Parser , ValueEnum } ;
55use graph:: schema:: InputSchema ;
66use std:: collections:: BTreeSet ;
77use std:: process:: exit;
@@ -13,10 +13,32 @@ use graph_store_postgres::{
1313 layout_for_tests:: make_dummy_site,
1414} ;
1515
16- pub fn usage ( msg : & str ) -> ! {
17- println ! ( "layout: {}" , msg) ;
18- println ! ( "Try 'layout --help' for more information." ) ;
19- std:: process:: exit ( 1 ) ;
16+ /// The kind of SQL to generate from a GraphQL schema.
17+ #[ derive( Clone , Copy , Debug , Default , ValueEnum ) ]
18+ enum Kind {
19+ /// Generate the DDL to create the tables (the default).
20+ #[ default]
21+ Ddl ,
22+ /// Generate `delete` statements for all tables.
23+ Delete ,
24+ /// Generate `drop table` statements for all tables.
25+ Drop ,
26+ /// Generate Diesel `table!` definitions and Rust structs.
27+ Diesel ,
28+ }
29+
30+ /// Information about the database schema for a GraphQL schema
31+ #[ derive( Parser , Debug ) ]
32+ #[ command( name = "layout" ) ]
33+ struct Opts {
34+ /// What kind of SQL to generate.
35+ #[ arg( short, long, value_enum, default_value_t = Kind :: Ddl ) ]
36+ generate : Kind ,
37+ /// Path to the GraphQL schema file.
38+ schema : String ,
39+ /// The database schema (namespace) to use.
40+ #[ arg( default_value = "subgraphs" ) ]
41+ db_schema : String ,
2042}
2143
2244pub fn ensure < T , E : std:: fmt:: Display > ( res : Result < T , E > , msg : & str ) -> T {
@@ -127,28 +149,15 @@ fn print_diesel_tables(layout: &Layout) {
127149}
128150
129151pub fn main ( ) {
130- let args = Command :: new ( "layout" )
131- . version ( "1.0" )
132- . about ( "Information about the database schema for a GraphQL schema" )
133- . arg ( arg ! ( -g --generate <KIND > "what kind of SQL to generate. Can be ddl (the default), migrate, delete, or drop" ) )
134- . arg ( arg ! ( <schema>) )
135- . arg ( arg ! ( <db_schema>) )
136- . get_matches ( ) ;
137-
138- let schema = args. get_one :: < & str > ( "schema" ) . unwrap ( ) ;
139- let namespace = args. get_one :: < & str > ( "db_schema" ) . unwrap_or ( & "subgraphs" ) ;
140- let kind = args. get_one :: < & str > ( "generate" ) . unwrap_or ( & "ddl" ) ;
152+ let opts = Opts :: parse ( ) ;
141153
142154 let subgraph = DeploymentHash :: new ( "Qmasubgraph" ) . unwrap ( ) ;
143- let schema = ensure ( fs:: read_to_string ( schema) , "Can not read schema file" ) ;
155+ let schema = ensure ( fs:: read_to_string ( & opts . schema ) , "Can not read schema file" ) ;
144156 let schema = ensure (
145157 InputSchema :: parse_latest ( & schema, subgraph. clone ( ) ) ,
146158 "Failed to parse schema" ,
147159 ) ;
148- let namespace = ensure (
149- Namespace :: new ( namespace. to_string ( ) ) ,
150- "Invalid database schema" ,
151- ) ;
160+ let namespace = ensure ( Namespace :: new ( opts. db_schema ) , "Invalid database schema" ) ;
152161 let site = Arc :: new ( make_dummy_site ( subgraph, namespace, "anet" . to_string ( ) ) ) ;
153162 let catalog = ensure (
154163 Catalog :: for_tests ( site. clone ( ) , BTreeSet :: new ( ) ) ,
@@ -158,13 +167,10 @@ pub fn main() {
158167 Layout :: new ( site, & schema, catalog) ,
159168 "Failed to construct Mapping" ,
160169 ) ;
161- match * kind {
162- "drop" => print_drop ( & layout) ,
163- "delete" => print_delete_all ( & layout) ,
164- "ddl" => print_ddl ( & layout) ,
165- "diesel" => print_diesel_tables ( & layout) ,
166- _ => {
167- usage ( & format ! ( "illegal value {} for --generate" , kind) ) ;
168- }
170+ match opts. generate {
171+ Kind :: Drop => print_drop ( & layout) ,
172+ Kind :: Delete => print_delete_all ( & layout) ,
173+ Kind :: Ddl => print_ddl ( & layout) ,
174+ Kind :: Diesel => print_diesel_tables ( & layout) ,
169175 }
170176}
0 commit comments