Skip to content

Commit

Permalink
Merge pull request #5 from nside/spec-cli
Browse files Browse the repository at this point in the history
Add spec command-line
  • Loading branch information
nside authored Jul 28, 2023
2 parents a95ca04 + 3c86361 commit 643d45b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pip install sqlite2rest
You can use SQLite2REST from the command line by providing the path to your SQLite database:

```
sqlite2rest /path/to/database.db
sqlite2rest serve /path/to/database.db
```


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

You can also generate an OpenAPI specification from an SQLite database:

```
sqlite2rest spec /path/to/database.db
```

## Contributing

Contributions are welcome! Please feel free to submit a pull request.
Expand Down
34 changes: 27 additions & 7 deletions sqlite2rest/__main__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import argparse
from .app import create_app
from .database import Database
from .openapi import get_openapi_spec

def main():
# Create the top-level parser
parser = argparse.ArgumentParser(description='SQLite2REST commands.')
subparsers = parser.add_subparsers(dest='command')

# Create the parser for the "serve" command
parser_serve = subparsers.add_parser('serve', help='Start a Flask server for an SQLite database.')
parser_serve.add_argument('database', help='The path to the SQLite database.')

# Create the parser for the "spec" command
parser_spec = subparsers.add_parser('spec', help='Generate OpenAPI spec from SQLite database.')
parser_spec.add_argument('database', help='The path to the SQLite database.')

# Parse command-line arguments
parser = argparse.ArgumentParser(description='Start a Flask server for an SQLite database.')
parser.add_argument('database', help='The path to the SQLite database.')
args = parser.parse_args()

# Create the Flask app
app = create_app(args.database)

# Run the Flask app
app.run()
# Execute the appropriate command
if args.command == 'spec':
# Create the Flask app
app = create_app(args.database)

# Create an application context
with app.app_context():
# Generate and print the OpenAPI spec
print(get_openapi_spec(Database(args.database)))
elif args.command == 'serve':
# Create and run the Flask app
app = create_app(args.database)
app.run()

if __name__ == '__main__':
main()

0 comments on commit 643d45b

Please sign in to comment.