Skip to content

Commit 643d45b

Browse files
authored
Merge pull request #5 from nside/spec-cli
Add spec command-line
2 parents a95ca04 + 3c86361 commit 643d45b

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pip install sqlite2rest
1919
You can use SQLite2REST from the command line by providing the path to your SQLite database:
2020

2121
```
22-
sqlite2rest /path/to/database.db
22+
sqlite2rest serve /path/to/database.db
2323
```
2424

2525

@@ -37,6 +37,12 @@ For each table in the database, the following endpoints are available:
3737
- `PUT /<table>/<id>`: Update an existing record in the table. The data for the record should be provided as JSON in the request body.
3838
- `DELETE /<table>/<id>`: Delete an existing record from the table.
3939

40+
You can also generate an OpenAPI specification from an SQLite database:
41+
42+
```
43+
sqlite2rest spec /path/to/database.db
44+
```
45+
4046
## Contributing
4147

4248
Contributions are welcome! Please feel free to submit a pull request.

sqlite2rest/__main__.py

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
11
import argparse
22
from .app import create_app
3+
from .database import Database
4+
from .openapi import get_openapi_spec
35

46
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+
519
# 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.')
820
args = parser.parse_args()
921

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()
1535

1636
if __name__ == '__main__':
1737
main()

0 commit comments

Comments
 (0)