1+ from pathlib import Path
12from pprint import pprint
23
34import click
45
56import mu .config
6- from mu .config import Config , cli_load
7+ from mu .config import Config , default_env , load
78from mu .libs import auth , logs , sqs , sts , utils
89from mu .libs .lamb import Lambda
910from mu .libs .status import Status
1314
1415
1516@click .group ()
17+ @click .option (
18+ '--config' ,
19+ 'config_path' ,
20+ type = click .Path (path_type = Path ),
21+ help = 'Path to mu config file' ,
22+ envvar = 'MU_CONFIG_PATH' ,
23+ )
1624@logs .click_options
17- def cli (log_level : str ):
25+ @click .pass_context
26+ def cli (ctx : click .Context , log_level : str , config_path : Path | None ):
1827 logs .init_logging (log_level )
28+ ctx .ensure_object (dict )
29+
30+ def load_config (env : str | None = None ) -> Config :
31+ return load (Path .cwd (), env or default_env (), config_path )
32+
33+ ctx .obj ['load_config' ] = load_config
1934
2035
2136@cli .command ()
2237@click .argument ('target_env' , required = False )
23- def auth_check (target_env ):
38+ @click .pass_context
39+ def auth_check (ctx : click .Context , target_env ):
2440 """Check AWS auth by displaying account info"""
25- config : Config = cli_load (target_env )
41+ config : Config = ctx . obj [ 'load_config' ] (target_env )
2642 b3_sess = auth .b3_sess (config .aws_region )
2743 ident : str = sts .caller_identity (b3_sess )
2844 print ('Account:' , ident ['Account' ])
@@ -42,9 +58,10 @@ def auth_check(target_env):
4258@cli .command ()
4359@click .argument ('target_env' , required = False )
4460@click .option ('--resolve-env' , is_flag = True , help = 'Show env after resolution (e.g. secrets)' )
45- def config (target_env : str , resolve_env : bool ):
61+ @click .pass_context
62+ def config (ctx : click .Context , target_env : str , resolve_env : bool ):
4663 """Display mu config for active project"""
47- config : Config = cli_load (target_env )
64+ config : Config = ctx . obj [ 'load_config' ] (target_env )
4865
4966 sess = auth .b3_sess (config .aws_region )
5067 config .apply_sess (sess )
@@ -54,24 +71,25 @@ def config(target_env: str, resolve_env: bool):
5471
5572@cli .command ()
5673@click .argument ('envs' , nargs = - 1 )
57- def provision (envs : list [str ]):
74+ @click .pass_context
75+ def provision (ctx : click .Context , envs : list [str ]):
5876 """Provision lambda function in environment given (or default)"""
5977 envs = envs or [None ]
6078
6179 for env in envs :
62- lamb = Lambda (cli_load (env ))
80+ lamb = Lambda (ctx . obj [ 'load_config' ] (env ))
6381 lamb .provision ()
6482
6583
6684@cli .command ()
6785@click .argument ('envs' , nargs = - 1 )
6886@click .option ('--build' , is_flag = True )
6987@click .pass_context
70- def deploy (ctx , envs : list [str ], build : bool ):
88+ def deploy (ctx : click . Context , envs : list [str ], build : bool ):
7189 """Deploy local image to ecr, update lambda"""
7290 envs = envs or [mu .config .default_env ()]
7391
74- configs = [cli_load (env ) for env in envs ]
92+ configs = [ctx . obj [ 'load_config' ] (env ) for env in envs ]
7593
7694 if build :
7795 service_names = [config .compose_service for config in configs ]
@@ -85,18 +103,19 @@ def deploy(ctx, envs: list[str], build: bool):
85103@cli .command ()
86104@click .argument ('target_env' )
87105@click .option ('--force-repo' , is_flag = True )
88- def delete (target_env : str , force_repo : bool ):
106+ @click .pass_context
107+ def delete (ctx : click .Context , target_env : str , force_repo : bool ):
89108 """Delete lambda and optionally related infra"""
90- lamb = Lambda (cli_load (target_env ))
109+ lamb = Lambda (ctx . obj [ 'load_config' ] (target_env ))
91110 lamb .delete (target_env , force_repo = force_repo )
92111
93112
94113@cli .command ()
95114@click .argument ('target_env' , required = False )
96- def build (target_env : str ):
115+ @click .pass_context
116+ def build (ctx : click .Context , target_env : str ):
97117 """Build lambda container with docker compose"""
98-
99- conf = cli_load (target_env )
118+ conf = ctx .obj ['load_config' ](target_env )
100119 utils .compose_build (conf .compose_service )
101120
102121
@@ -107,10 +126,16 @@ def build(target_env: str):
107126@click .option ('--host' , default = 'localhost:8080' )
108127@click .option ('--local' , is_flag = True )
109128@click .pass_context
110- def invoke (ctx , target_env : str , action : str , host : str , action_args : list , local : bool ):
129+ def invoke (
130+ ctx : click .Context ,
131+ target_env : str ,
132+ action : str ,
133+ host : str ,
134+ action_args : list ,
135+ local : bool ,
136+ ):
111137 """Invoke lambda with diagnostics or given action"""
112-
113- lamb = Lambda (cli_load (target_env ))
138+ lamb = Lambda (ctx .obj ['load_config' ](target_env ))
114139 if local :
115140 result = lamb .invoke_rei (host , action , action_args )
116141 else :
@@ -138,7 +163,7 @@ def _logs(
138163 if not first and not last :
139164 last = 10 if streams else 25
140165
141- lamb = Lambda (cli_load (target_env ))
166+ lamb = Lambda (ctx . obj [ 'load_config' ] (target_env ))
142167 lamb .logs (first , last , streams )
143168
144169
@@ -164,11 +189,12 @@ def sqs_list(ctx: click.Context, verbose: bool, delete: bool, name_prefix=str):
164189@cli .command ()
165190@click .argument ('action' , type = click .Choice (('show' , 'provision' , 'delete' )))
166191@click .argument ('target_env' , required = False )
167- def domain_name (target_env : str , action : str ):
192+ @click .pass_context
193+ def domain_name (ctx : click .Context , target_env : str , action : str ):
168194 """Manage AWS config needed for domain name support"""
169195 from ..libs import gateway
170196
171- config : Config = cli_load ( target_env or mu . config . default_env () )
197+ config : Config = ctx . obj [ 'load_config' ]( target_env )
172198 assert config .domain_name
173199
174200 gw = gateway .Gateway (config )
@@ -189,8 +215,9 @@ def domain_name(target_env: str, action: str):
189215
190216@cli .command ()
191217@click .argument ('target_env' , required = False )
192- def status (target_env : str ):
218+ @click .pass_context
219+ def status (ctx : click .Context , target_env : str | None ):
193220 """Check status of all infrastructure components for the app"""
194- config = cli_load ( target_env or mu . config . default_env () )
221+ config = ctx . obj [ 'load_config' ]( target_env )
195222
196223 print (Status .fetch (config ))
0 commit comments