5
5
from typing_extensions import Annotated
6
6
import logging
7
7
import requests
8
+ from rich .console import Console
9
+ from rich .markdown import Markdown
8
10
import odtp .mongodb .db as db
9
11
import odtp .mongodb .utils as mongodb_utils
10
12
import odtp .helpers .parse as odtp_parse
13
15
import traceback
14
16
15
17
app = typer .Typer ()
18
+ console = Console ()
16
19
17
20
log = logging .getLogger (__name__ )
18
21
@@ -23,8 +26,31 @@ def user_entry(
23
26
github : str = typer .Option (..., "--github" , help = "Specify the github" ),
24
27
):
25
28
"""Add new user in the MongoDB"""
26
- user_id = db .add_user (name = name , github = github , email = email )
27
- log .info (f"A user has been added { user_id } " )
29
+ try :
30
+ if name is None or email is None or github is None :
31
+ console .print ("[bold red]❌ ERROR:[/bold red] Please provide either --name or --email and --github" )
32
+ raise typer .Exit (code = 1 )
33
+
34
+ if not validation_helpers .validate_user_name_unique (name ):
35
+ console .print ("[bold red]❌ ERROR:[/bold red] --name must be unique." )
36
+ raise typer .Exit (code = 1 )
37
+
38
+ if not validation_helpers .validate_github_user_name (github ):
39
+ console .print ("[bold red]❌ ERROR:[/bold red] --github must be a valid github user name." )
40
+ raise typer .Exit (code = 1 )
41
+
42
+ user_id = db .add_user (name = name , github = github , email = email )
43
+
44
+ except typer .Exit :
45
+ pass
46
+
47
+ except Exception as e :
48
+ log .exception ("Unexpected error in user_entry" )
49
+ console .print (f"[bold red]❌ ERROR:[/bold red] Failed to add user. Details: { str (e )} " )
50
+ raise typer .Exit (code = 1 )
51
+ else :
52
+ success_message = f"[bold green]✅ SUCCESS: User with User Id { user_id } has been added![/bold green]"
53
+ console .print (success_message )
28
54
29
55
30
56
@app .command ()
@@ -53,28 +79,28 @@ def odtp_component_entry(
53
79
54
80
except git_helpers .OdtpGithubException as e :
55
81
typer .echo (f"Error: An error occurred while fetching information from GitHub: { e } " )
56
- raise typer .Exit (code = 2 )
82
+ raise typer .Exit (code = 1 )
57
83
58
84
except mongodb_utils .OdtpDbMongoDBValidationException as e :
59
85
typer .echo (f"Error: Component version could not be added: { e } " )
60
- raise typer .Exit (code = 3 )
86
+ raise typer .Exit (code = 1 )
61
87
62
88
except validation_helpers .OdtpYmlException as e :
63
89
log .error (f"Validation error occurred when parsing odtp.yml" )
64
90
typer .echo (f"Error: Validation of odtp.yml failed: { e } " )
65
- raise typer .Exit (code = 4 )
91
+ raise typer .Exit (code = 1 )
66
92
67
93
except requests .RequestException as e :
68
94
log .error (f"Network error: { e } " )
69
95
typer .echo (f"Error: A network error occurred while communicating with GitHub.: { e } " )
70
- raise typer .Exit (code = 5 )
96
+ raise typer .Exit (code = 1 )
71
97
72
98
except Exception as e :
73
99
error_message = f"Error: An unexpected error occurred: { e } "
74
100
if debug :
75
101
error_message += f"\n { traceback .format_exc ()} "
76
102
typer .echo (error_message , err = True )
77
- raise typer .Exit (code = 99 )
103
+ raise typer .Exit (code = 1 )
78
104
79
105
else :
80
106
if component_id and version_id :
@@ -94,14 +120,39 @@ def digital_twin_entry(
94
120
user_email : str = typer .Option (None , "--user-email" , help = "Specify the email" ),
95
121
name : str = typer .Option (..., "--name" , help = "Specify the digital twin name" ),
96
122
):
97
- if user_id is None and user_email is None :
98
- raise typer .Exit ("Please provide either --user-id or --user-email" )
123
+ try :
124
+ if user_id is None and user_email is None :
125
+ console .print ("[bold red]❌ ERROR:[/bold red] Please provide either --user-id or --user-email" )
126
+ raise typer .Exit (code = 1 )
127
+
128
+ if user_email :
129
+ user_id = db .get_document_id_by_field_value ("email" , user_email , "users" )
130
+
131
+ if not user_id :
132
+ console .print ("[bold red]❌ ERROR:[/bold red] User does not exist. Please add the user first." )
133
+ raise typer .Exit (code = 1 )
134
+
135
+ if not validation_helpers .validate_digital_twin_name_unique (digital_twin_name = name , user_id = user_id ):
136
+ console .print ("[bold red]❌ ERROR:[/bold red] Digital Twin name must be unique and 6 characters long. Choose a different name." )
137
+ raise typer .Exit (code = 1 )
138
+
139
+ dt_id = db .add_digital_twin (userRef = user_id , name = name )
99
140
100
- if user_email :
101
- user_id = db .get_document_id_by_field_value ("email" , user_email , "users" )
141
+ if not dt_id :
142
+ console .print ("[bold red]❌ ERROR:[/bold red] Failed to create Digital Twin due to a database issue." )
143
+ raise typer .Exit (code = 1 )
102
144
103
- dt_id = db .add_digital_twin (userRef = user_id , name = name )
104
- log .info (f"Digital Twin added with ID { dt_id } " )
145
+ except typer .Exit :
146
+ pass
147
+
148
+ except Exception as e :
149
+ console .print (f"[bold red]❌ ERROR:[/bold red] An unexpected error occurred: { str (e )} " )
150
+ log .exception ("Unexpected error in digital_twin_entry" )
151
+ raise typer .Exit (code = 1 )
152
+
153
+ else :
154
+ success_message = f"[bold green]✅ SUCCESS: Digital Twin with id { dt_id } has been added![/bold green]"
155
+ console .print (success_message )
105
156
106
157
107
158
@app .command ()
@@ -116,7 +167,8 @@ def workflow_entry(
116
167
):
117
168
try :
118
169
if component_tags is None and component_versions is None :
119
- raise typer .Exit ("Please provide either --component-tags or --component-versions" )
170
+ console .print ("[bold red]❌ ERROR:[/bold red] Please provide either --component-tags or --component-versions" )
171
+ raise typer .Exit (code = 1 )
120
172
if component_tags :
121
173
component_versions = "," .join (odtp_parse .parse_component_tags (component_tags ))
122
174
versions = odtp_parse .parse_versions (component_versions )
@@ -125,14 +177,14 @@ def workflow_entry(
125
177
workflow = versions ,
126
178
)
127
179
except Exception as e :
128
- log .error (f"ERROR: { e } " )
129
- traceback .print_exc ()
180
+ console .print (f"[bold red]❌ ERROR:[/bold red] An unexpected error occurred: { str (e )} " )
181
+ log .exception ("Unexpected error in workflow_entry" )
182
+ raise typer .Exit (code = 1 )
130
183
success_message = (
131
184
f"SUCCESS: Workflow has been added:\n "
132
185
f" - Workflow ID: { workflow_id } \n "
133
186
)
134
- log .info (success_message )
135
- typer .echo (f"✅ { success_message } " )
187
+ console .print (f"✅ { success_message } " )
136
188
137
189
138
190
@app .command ()
@@ -141,6 +193,9 @@ def execution_entry(
141
193
wf_id : str = typer .Option (
142
194
None , "--workflow-id" , help = "Specify the workflow ID"
143
195
),
196
+ wf_name : str = typer .Option (
197
+ None , "--workflow-name" , help = "Specify the workflow name"
198
+ ),
144
199
component_tags : str = typer .Option (
145
200
None , "--component-tags" , help = "Specify the components-tags (component-name:version) separated by commas"
146
201
),
@@ -157,11 +212,11 @@ def execution_entry(
157
212
):
158
213
try :
159
214
if dt_name is None and dt_id is None :
160
- raise typer . Exit ("Please provide either --digital-twin-name or --digital-twin-id" )
161
-
215
+ print ("Please provide either --digital-twin-name or --digital-twin-id" )
216
+ raise typer . Exit ( code = 1 )
162
217
if wf_id is None and component_tags is None :
163
- raise typer . Exit ("Please provide either --workflow-id or --component-tags" )
164
-
218
+ print ("Please provide either --workflow-id or --component-tags" )
219
+ raise typer . Exit ( code = 1 )
165
220
if dt_name :
166
221
dt_id = db .get_document_id_by_field_value ("name" , dt_name , "digitalTwins" )
167
222
@@ -171,7 +226,7 @@ def execution_entry(
171
226
if component_tags :
172
227
component_versions = "," .join (odtp_parse .parse_component_tags (component_tags ))
173
228
versions = odtp_parse .parse_versions (component_versions )
174
- workflow = db .get_or_create_workflow_by_versions (execution_name , versions )
229
+ workflow = db .get_workflow_or_create_by_versions (execution_name , versions )
175
230
step_count = len (workflow .get ("versions" ))
176
231
if parameter_files is None :
177
232
parameters = db .get_default_parameters_for_workflow (workflow ["versions" ])
@@ -195,13 +250,14 @@ def execution_entry(
195
250
except Exception as e :
196
251
log .error (f"ERROR: { e } " )
197
252
traceback .print_exc ()
198
- success_message = (
199
- f"SUCCESS: execution has been added: see above for the details.\n "
200
- f" - execution id: { execution_id } \n "
201
- f" - step_ids: { step_ids } \n "
202
- )
203
- log .info (success_message )
204
- typer .echo (f"✅ { success_message } " )
253
+ else :
254
+ success_message = (
255
+ f"SUCCESS: execution has been added: see above for the details.\n "
256
+ f" - execution id: { execution_id } \n "
257
+ f" - step_ids: { step_ids } \n "
258
+ )
259
+ log .info (success_message )
260
+ typer .echo (f"✅ { success_message } " )
205
261
206
262
207
263
if __name__ == "__main__" :
0 commit comments