8
8
9
9
from . import __version__ , operations , profiles
10
10
from .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
12
12
13
13
CONTEXT_SETTINGS = dict (help_option_names = ["-h" , "--help" ])
14
14
@@ -39,11 +39,7 @@ def main(ctx, profile_dir: Path) -> None:
39
39
@click .pass_context
40
40
def new (ctx , profile_name : str , launch : bool , foreground : bool , ** kwargs ):
41
41
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 )
47
43
48
44
49
45
@main .command ()
@@ -56,15 +52,12 @@ def new(ctx, profile_name: str, launch: bool, foreground: bool, **kwargs):
56
52
@click .pass_context
57
53
def from_session (
58
54
ctx ,
59
- launch : bool ,
60
- foreground : bool ,
55
+ session : str ,
56
+ profile_name : Optional [ str ] ,
61
57
** kwargs ,
62
58
):
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 )
68
61
69
62
70
63
@main .command ()
@@ -86,7 +79,7 @@ def desktop(
86
79
def launch (ctx , profile_name : str , ** kwargs ):
87
80
profile = Profile (profile_name , ctx .obj ["PROFILE_DIR" ])
88
81
# TODO qb args
89
- exit_with (operations .launch (profile , ** kwargs ))
82
+ exit_with (operations .launch (profile , qb_args = [], ** kwargs ))
90
83
91
84
92
85
@main .command ()
@@ -119,11 +112,36 @@ def list_(ctx):
119
112
print (profile .name )
120
113
121
114
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 )
127
145
128
146
129
147
def exit_with (result : bool ):
0 commit comments