|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +print("****************") |
| 4 | +print("TimeTracker CLI") |
| 5 | +print("****************") |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from flask import json |
| 10 | +from time_tracker_api import create_app |
| 11 | +from flask_script import Manager |
| 12 | +from time_tracker_api.api import api |
| 13 | + |
| 14 | +app = create_app() |
| 15 | +cli_manager = Manager(app) |
| 16 | + |
| 17 | + |
| 18 | +@cli_manager.command |
| 19 | +@cli_manager.option('-f', '--filename', help='Path of the swagger file. By default swagger.json') |
| 20 | +def gen_swagger_json(filename='swagger.json'): |
| 21 | + """ Exports swagger specifications in json format """ |
| 22 | + schema_json_data = json.dumps(api.__schema__) |
| 23 | + save_data(schema_json_data, filename) |
| 24 | + |
| 25 | + |
| 26 | +@cli_manager.command |
| 27 | +@cli_manager.option('-f', '--filename', dest='filename', help='Path of the postman collection file.' |
| 28 | + 'By default it is timetracker-api-postman-collection.json') |
| 29 | +def gen_postman_collection(filename='timetracker-api-postman-collection.json'): |
| 30 | + """ Generates a Postman collection for the API """ |
| 31 | + data = api.as_postman(urlvars=False, swagger=True) |
| 32 | + postman_collection_json_data = json.dumps(data) |
| 33 | + parsed_json = postman_collection_json_data.replace("http://localhost", '{{timetracker_api_host}}') |
| 34 | + save_data(parsed_json, filename) |
| 35 | + |
| 36 | + |
| 37 | +def save_data(data, filename): |
| 38 | + """ Save text content to a file """ |
| 39 | + if filename: |
| 40 | + try: |
| 41 | + real_path = os.path.expanduser(filename) |
| 42 | + with open(real_path, "w") as f: |
| 43 | + f.write(data) |
| 44 | + print("%s was generated successfully" % real_path) |
| 45 | + except OSError as err: |
| 46 | + print("Error while generating '%s': %s" % filename, err) |
| 47 | + else: |
| 48 | + print(data) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + cli_manager.run() |
0 commit comments