1- use std:: { path:: Path , sync:: Arc } ;
1+ use std:: { collections :: HashMap , path:: Path , sync:: Arc } ;
22
33use miette:: Diagnostic ;
44
@@ -17,11 +17,24 @@ pub enum InputError {
1717 Serde ( #[ from] serde_json:: Error ) ,
1818}
1919
20+ #[ derive( Debug , Diagnostic , thiserror:: Error ) ]
21+ pub enum ContextError {
22+ #[ error( "attempted to deploy to the wrong cluster (expected `{expected}`, got `{actual}`)" ) ]
23+ Mismatch { expected : String , actual : String } ,
24+ #[ error( "no active kubernetes context found" ) ]
25+ Missing ,
26+ #[ error( "failed to read kubeconfig" ) ]
27+ KubeConfig ( #[ from] kube:: config:: KubeconfigError ) ,
28+ }
29+
2030#[ derive( Debug , Diagnostic , thiserror:: Error ) ]
2131pub enum Error {
2232 #[ error( "failed to read input file" ) ]
2333 #[ diagnostic( transparent) ]
2434 Input ( #[ from] InputError ) ,
35+ #[ error( "failed verify kubernetes context" ) ]
36+ #[ diagnostic( transparent) ]
37+ Context ( #[ from] ContextError ) ,
2538 #[ error( "failed to deploy" ) ]
2639 #[ diagnostic( transparent) ]
2740 Deploy ( #[ from] DeployError ) ,
@@ -35,7 +48,29 @@ async fn read_input(path: impl AsRef<Path>) -> Result<Output, InputError> {
3548 Ok ( serde_json:: from_slice ( & content) ?)
3649}
3750
38- pub async fn run ( config : Config , input_file : & Path ) -> Result < ( ) , Error > {
51+ async fn ensure_context < ' a > (
52+ profile : Option < & str > ,
53+ mappings : & ' a HashMap < String , String > ,
54+ ) -> Result < Option < & ' a str > , ContextError > {
55+ let Some ( expected) = profile. and_then ( |profile| mappings. get ( profile) ) else {
56+ return Ok ( None ) ;
57+ } ;
58+
59+ let kubeconfig = kube:: config:: Kubeconfig :: read ( ) ?;
60+ let current_context = kubeconfig. current_context . ok_or ( ContextError :: Missing ) ?;
61+ if current_context. ne ( expected) {
62+ return Err ( ContextError :: Mismatch {
63+ expected : expected. to_string ( ) ,
64+ actual : current_context. to_string ( ) ,
65+ } ) ;
66+ }
67+
68+ Ok ( Some ( expected. as_str ( ) ) )
69+ }
70+
71+ pub async fn run ( profile : Option < & str > , config : Config , input_file : & Path ) -> Result < ( ) , Error > {
72+ ensure_context ( profile, & config. context_mappings ) . await ?;
73+
3974 let input = read_input ( input_file) . await ?;
4075 let root = progress:: tree ( ) ;
4176 let handle = progress:: setup_line_renderer ( & root) ;
0 commit comments