3838logger = logging .getLogger (__name__ )
3939
4040
41+ def json_out (obj ):
42+ print (json .dumps (obj , indent = 4 ))
43+
44+
4145def examine_exeption (e : Exception ) -> None :
4246 msg = f"{ __app_name__ } : Operation failed"
4347 if isinstance (e , fc .ClientsCredentialsException ):
@@ -104,7 +108,7 @@ def systems(
104108 with console .pager ():
105109 console .print (json .dumps (result , indent = 4 ))
106110 else :
107- console . print ( json . dumps ( result , indent = 4 ) )
111+ json_out ( result )
108112
109113 except Exception as e :
110114 examine_exeption (e )
@@ -133,7 +137,7 @@ def get_nodes(
133137 with console .pager ():
134138 console .print (json .dumps (results , indent = 4 ))
135139
136- console . print ( json . dumps ( results , indent = 4 ) )
140+ json_out ( results )
137141 except Exception as e :
138142 examine_exeption (e )
139143 raise typer .Exit (code = 1 )
@@ -162,7 +166,7 @@ def get_reservations(
162166 console .print (json .dumps (results , indent = 4 ))
163167
164168 else :
165- console . print ( json . dumps ( results , indent = 4 ) )
169+ json_out ( results )
166170 except Exception as e :
167171 examine_exeption (e )
168172 raise typer .Exit (code = 1 )
@@ -190,7 +194,7 @@ def get_partitions(
190194 with console .pager ():
191195 console .print (json .dumps (results , indent = 4 ))
192196 else :
193- console . print ( json . dumps ( results , indent = 4 ) )
197+ json_out ( results )
194198
195199 except Exception as e :
196200 examine_exeption (e )
@@ -213,7 +217,7 @@ def id(
213217 try :
214218 result = asyncio .run (client .userinfo (system ))
215219 if raw :
216- console . print ( json . dumps ( result , indent = 4 ) )
220+ json_out ( result )
217221 else :
218222 user = f"{ result ['user' ]['id' ]} ({ result ['user' ]['name' ]} )"
219223 group = f"{ result ['group' ]['id' ]} ({ result ['group' ]['name' ]} )"
@@ -268,7 +272,8 @@ def ls(
268272 numeric_uid_gid ,
269273 dereference
270274 ))
271- console .print (json .dumps (result , indent = 4 ))
275+ json_out (result )
276+
272277
273278 except Exception as e :
274279 examine_exeption (e )
@@ -502,7 +507,7 @@ def stat(
502507 """Use the stat linux application to determine the status of a file on the system's filesystem"""
503508 try :
504509 result = asyncio .run (client .stat (system , path , deref ))
505- console . print ( json . dumps ( result , indent = 4 ) )
510+ json_out ( result )
506511
507512 except Exception as e :
508513 examine_exeption (e )
@@ -572,10 +577,7 @@ def checksum(
572577 """Calculate the SHA256 (256-bit) checksum"""
573578 try :
574579 result = asyncio .run (client .checksum (system , path ))
575- console .print (json .dumps (
576- result ,
577- indent = 4
578- ))
580+ json_out (result )
579581 except Exception as e :
580582 examine_exeption (e )
581583 raise typer .Exit (code = 1 )
@@ -637,12 +639,7 @@ def head(
637639 skip_ending
638640 ))
639641 if raw :
640- console .print (
641- json .dumps (
642- result ,
643- indent = 4
644- )
645- )
642+ json_out (result )
646643 else :
647644 console .print (result ["content" ], end = '' )
648645 except Exception as e :
@@ -707,12 +704,7 @@ def tail(
707704 ))
708705
709706 if raw :
710- console .print (
711- json .dumps (
712- result ,
713- indent = 4
714- )
715- )
707+ json_out (result )
716708 else :
717709 console .print (
718710 result ["content" ],
@@ -848,7 +840,7 @@ def is_remote_script(p: str) -> bool:
848840 account = account ,
849841 ))
850842
851- console . print ( json . dumps ( result , indent = 4 ) )
843+ json_out ( result )
852844 except Exception as e :
853845 examine_exeption (e )
854846 raise typer .Exit (code = 1 )
@@ -867,8 +859,7 @@ def job_info(
867859 """
868860 try :
869861 result = asyncio .run (client .job_info (system , jobid ))
870-
871- console .print (json .dumps (result , indent = 4 ))
862+ json_out (result )
872863 except Exception as e :
873864 examine_exeption (e )
874865 raise typer .Exit (code = 1 )
@@ -887,7 +878,7 @@ def wait_for_job(
887878 """
888879 try :
889880 result = asyncio .run (client .wait_for_job (system , jobid ))
890- console . print ( json . dumps ( result , indent = 4 ) )
881+ json_out ( result )
891882 except Exception as e :
892883 examine_exeption (e )
893884 raise typer .Exit (code = 1 )
@@ -906,7 +897,7 @@ def job_metadata(
906897 """
907898 try :
908899 result = asyncio .run (client .job_metadata (system , job ))
909- console . print ( json . dumps ( result , indent = 4 ) )
900+ json_out ( result )
910901 except Exception as e :
911902 examine_exeption (e )
912903 raise typer .Exit (code = 1 )
0 commit comments