88
99from . import __version__ , operations , profiles
1010from .profiles import Profile
11- from .utils import SUPPORTED_MENUS , default_profile_dir , error
11+ from .utils import SUPPORTED_MENUS , default_profile_dir , error , user_data_dir
1212
1313CONTEXT_SETTINGS = dict (help_option_names = ["-h" , "--help" ])
1414
@@ -39,11 +39,7 @@ def main(ctx, profile_dir: Path) -> None:
3939@click .pass_context
4040def new (ctx , profile_name : str , launch : bool , foreground : bool , ** kwargs ):
4141 profile = Profile (profile_name , ctx .obj ["PROFILE_DIR" ])
42- result = profiles .new_profile (profile , ** kwargs )
43- if result and launch :
44- # TODO args?
45- exit_with (then_launch (profile , foreground , []))
46- exit_with (result )
42+ then_launch (profiles .new_profile , profile , ** kwargs )
4743
4844
4945@main .command ()
@@ -56,15 +52,12 @@ def new(ctx, profile_name: str, launch: bool, foreground: bool, **kwargs):
5652@click .pass_context
5753def from_session (
5854 ctx ,
59- launch : bool ,
60- foreground : bool ,
55+ session : str ,
56+ profile_name : Optional [ str ] ,
6157 ** kwargs ,
6258):
63- profile = operations .from_session (profile_dir = ctx .obj ["PROFILE_DIR" ], ** kwargs )
64- if profile and launch :
65- # TODO args?
66- exit_with (then_launch (profile , foreground , []))
67- exit_with (bool (profile ))
59+ profile , session_path = session_info (session , profile_name , ctx .obj ["PROFILE_DIR" ])
60+ then_launch (operations .from_session , profile , session_path = session_path , ** kwargs )
6861
6962
7063@main .command ()
@@ -86,7 +79,7 @@ def desktop(
8679def launch (ctx , profile_name : str , ** kwargs ):
8780 profile = Profile (profile_name , ctx .obj ["PROFILE_DIR" ])
8881 # TODO qb args
89- exit_with (operations .launch (profile , ** kwargs ))
82+ exit_with (operations .launch (profile , qb_args = [], ** kwargs ))
9083
9184
9285@main .command ()
@@ -119,11 +112,36 @@ def list_(ctx):
119112 print (profile .name )
120113
121114
122- def then_launch (profile : Profile , foreground : bool , qb_args : list [str ]) -> bool :
123- result = False
124- if profile :
125- result = operations .launch (profile , False , foreground , qb_args )
126- return result
115+ def then_launch (
116+ operation : Callable [..., bool ],
117+ profile : Profile ,
118+ launch : bool ,
119+ foreground : bool ,
120+ qb_args : list [str ] = [],
121+ ** kwargs ,
122+ ) -> None :
123+ exit_with (
124+ operation (profile , ** kwargs )
125+ and ((not launch ) or operations .launch (profile , False , foreground , qb_args ))
126+ )
127+
128+
129+ def session_info (
130+ session : str , profile_name : Optional [str ], profile_dir : Path
131+ ) -> tuple [Profile , Path ]:
132+ user_session_dir = user_data_dir () / "sessions"
133+ session_paths = []
134+ if not "/" in session :
135+ session_paths .append (user_session_dir / (session + ".yml" ))
136+ session_paths .append (Path (session ))
137+ session_path = next (filter (lambda path : path .is_file (), session_paths ), None )
138+
139+ if not session_path :
140+ tried = ", " .join ([str (p .resolve ()) for p in session_paths ])
141+ error (f"could not find session at the following paths: { tried } " )
142+ exit (1 )
143+
144+ return (Profile (profile_name or session_path .stem , profile_dir ), session_path )
127145
128146
129147def exit_with (result : bool ):
0 commit comments