1+ import click
2+ from api import api_create_alert , api_list_alerts , api_show_alert
3+ from utils import display_paginated_results , handle_api_error , format_item_details
4+
5+ @click .group ()
6+ def alerts ():
7+ """Commands for managing PagerTree alerts."""
8+ pass
9+
10+ @alerts .command (name = "create" )
11+ @click .option ("--title" , required = True , help = "Title of the alert" )
12+ @click .option ("--description" , help = "Description of the alert" )
13+ def create_alert_cmd (title , description ):
14+ """Create a new alert in PagerTree."""
15+ try :
16+ result = api_create_alert (title , description or "No description provided" )
17+ click .echo (f"Alert created successfully: { result .get ('id' )} " )
18+ except Exception as e :
19+ click .echo (f"Error creating alert: { str (e )} " , err = True )
20+
21+ @alerts .command (name = "list" )
22+ @click .option ("--limit" , default = 10 , type = click .IntRange (1 , 100 ), help = "Number of alerts per page" )
23+ @click .option ("--offset" , default = 0 , type = click .IntRange (0 ), help = "Starting point for pagination" )
24+ def list_alerts_cmd (limit , offset ):
25+ """List alerts in PagerTree with pagination."""
26+ try :
27+ result = api_list_alerts (limit = limit , offset = offset )
28+ alerts_list = result ["data" ]
29+ total = result ["total" ]
30+ # Prepare table data
31+ headers = ["ID" , "Title" , "Status" ]
32+ table_data = [[alert .get ("tiny_id" ), alert .get ("title" ), alert .get ("status" )] for alert in alerts_list ]
33+ display_paginated_results (alerts_list , total , limit , offset , "alert" , headers , table_data )
34+ except Exception as e :
35+ handle_api_error (e , action = "listing alerts" )
36+
37+ @alerts .command (name = "show" )
38+ @click .argument ("alert_id" , required = True )
39+ def show_alert_cmd (alert_id ):
40+ """Show details of a specific alert in PagerTree."""
41+ try :
42+ alert = api_show_alert (alert_id )
43+ fields = {
44+ "id" : "Alert ID" ,
45+ "title" : "Title" ,
46+ "description" : "Description" ,
47+ "status" : "Status" ,
48+ "created_at" : "Created At"
49+ }
50+ format_item_details (alert , fields )
51+ except requests .exceptions .HTTPError as e :
52+ click .echo (f"Error fetching alert: { e .response .status_code } - { e .response .reason } " , err = True )
53+ except Exception as e :
54+ handle_api_error (e , "showing alert" )
0 commit comments