docs_cmd.py
1 import click 2 import http.server 3 import socketserver 4 import webbrowser 5 import os 6 from cli.utils import get_cli_root_dir, error_exit 7 8 class DocsHttpRequestHandler(http.server.SimpleHTTPRequestHandler): 9 def __init__(self, *args, **kwargs): 10 super().__init__(*args, directory=kwargs.pop('directory'), **kwargs) 11 12 def do_GET(self): 13 if self.path.startswith('/solace-agent-mesh'): 14 self.path = self.path[len('/solace-agent-mesh'):] or '/' 15 super().do_GET() 16 17 def send_error(self, code, message=None): 18 if code == 404: 19 self.send_response(302) 20 self.send_header('Location', '/solace-agent-mesh/docs/documentation/getting-started/introduction/') 21 self.end_headers() 22 else: 23 super().send_error(code, message) 24 25 @click.command(name="docs") 26 @click.option( 27 "-p", 28 "--port", 29 "port", 30 default=8585, 31 help="Port to run the documentation server on.", 32 type=int, 33 ) 34 def docs(port: int): 35 """ 36 Starts a web server to view the documentation. 37 """ 38 prod_docs_dir = os.path.join(get_cli_root_dir(), 'assets', 'docs') 39 dev_docs_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'docs', 'build') 40 41 if os.path.exists(prod_docs_dir): 42 docs_dir = prod_docs_dir 43 elif os.path.exists(dev_docs_dir): 44 docs_dir = dev_docs_dir 45 click.echo("Serving development documentation") 46 else: 47 docs_dir = None 48 error_exit("Documentation directory not found. Please build the documentation first.") 49 50 def handler(*args, **kwargs): 51 return DocsHttpRequestHandler(*args, directory=docs_dir, **kwargs) 52 53 with socketserver.TCPServer(("", port), handler) as httpd: 54 url = f"http://localhost:{port}/solace-agent-mesh/docs/documentation/getting-started/introduction/" 55 click.echo(f"Starting documentation server on {url}") 56 webbrowser.open_new_tab(url) 57 try: 58 httpd.serve_forever() 59 except KeyboardInterrupt: 60 click.echo("\nShutting down documentation server...")