|
1 | 1 | import argparse
|
2 | 2 | from .app import create_app
|
| 3 | +from .database import Database |
| 4 | +from .openapi import get_openapi_spec |
3 | 5 |
|
4 | 6 | def main():
|
| 7 | + # Create the top-level parser |
| 8 | + parser = argparse.ArgumentParser(description='SQLite2REST commands.') |
| 9 | + subparsers = parser.add_subparsers(dest='command') |
| 10 | + |
| 11 | + # Create the parser for the "serve" command |
| 12 | + parser_serve = subparsers.add_parser('serve', help='Start a Flask server for an SQLite database.') |
| 13 | + parser_serve.add_argument('database', help='The path to the SQLite database.') |
| 14 | + |
| 15 | + # Create the parser for the "spec" command |
| 16 | + parser_spec = subparsers.add_parser('spec', help='Generate OpenAPI spec from SQLite database.') |
| 17 | + parser_spec.add_argument('database', help='The path to the SQLite database.') |
| 18 | + |
5 | 19 | # Parse command-line arguments
|
6 |
| - parser = argparse.ArgumentParser(description='Start a Flask server for an SQLite database.') |
7 |
| - parser.add_argument('database', help='The path to the SQLite database.') |
8 | 20 | args = parser.parse_args()
|
9 | 21 |
|
10 |
| - # Create the Flask app |
11 |
| - app = create_app(args.database) |
12 |
| - |
13 |
| - # Run the Flask app |
14 |
| - app.run() |
| 22 | + # Execute the appropriate command |
| 23 | + if args.command == 'spec': |
| 24 | + # Create the Flask app |
| 25 | + app = create_app(args.database) |
| 26 | + |
| 27 | + # Create an application context |
| 28 | + with app.app_context(): |
| 29 | + # Generate and print the OpenAPI spec |
| 30 | + print(get_openapi_spec(Database(args.database))) |
| 31 | + elif args.command == 'serve': |
| 32 | + # Create and run the Flask app |
| 33 | + app = create_app(args.database) |
| 34 | + app.run() |
15 | 35 |
|
16 | 36 | if __name__ == '__main__':
|
17 | 37 | main()
|
0 commit comments