11#! /usr/bin/env python3
22
33import asyncio
4+ import os
5+ from contextlib import suppress
46from pathlib import Path
7+ from pprint import pformat
58from typing import Optional
69
710import typer
8- from httpx import URL , AsyncClient
11+ from httpx import URL , AsyncClient , HTTPStatusError
912from pydantic .networks import EmailStr
1013from pydantic .types import SecretStr
1114
15+ EVERYONE_GROUP_ID = 1
16+
17+
18+ def create_human_readable_message (exc : HTTPStatusError ) -> str :
19+ msg = str (exc )
20+ with suppress (Exception ):
21+ error = exc .response .json ()["error" ]
22+ for err in error ["errors" ]:
23+ msg += "\n {code}: {message}" .format (** err )
24+ return msg
25+
1226
1327async def login_user (client : AsyncClient , email : EmailStr , password : SecretStr ):
1428 typer .secho (
@@ -100,7 +114,7 @@ async def import_project_as_template(
100114 password : SecretStr ,
101115 project_file : Path ,
102116 project_name : str ,
103- share_with_gid : int ,
117+ share_with_gid : int , # TODO: not used!?
104118) -> int :
105119 try :
106120 async with AsyncClient (base_url = endpoint .join ("v0" )) as client :
@@ -113,20 +127,26 @@ async def import_project_as_template(
113127 fg = typer .colors .BRIGHT_WHITE ,
114128 )
115129
130+ except HTTPStatusError as exc :
131+ typer .secho (create_human_readable_message (exc ), fg = typer .colors .RED , err = True )
132+ return os .EX_SOFTWARE
133+
116134 except Exception as exc : # pylint: disable=broad-except
117135 typer .secho (f"Unexpected issue: { exc } " , fg = typer .colors .RED , err = True )
118- return 1
136+ return os . EX_SOFTWARE
119137
120- return 0
138+ return os . EX_OK
121139
122140
123141def main (
124142 endpoint : str ,
125143 username : str ,
126- password : str ,
127144 project_file : Path ,
128145 project_name : Optional [str ] = None ,
129- share_with_gid : Optional [int ] = 1 ,
146+ share_with_gid : int = EVERYONE_GROUP_ID ,
147+ password : str = typer .Option (
148+ ..., prompt = True , confirmation_prompt = True , hide_input = True
149+ ),
130150) -> int :
131151
132152 if project_name is None :
0 commit comments