@@ -2784,20 +2784,30 @@ fn run_main() -> Result<()> {
27842784async fn run_async ( ) -> Result < ( ) > {
27852785 CompleteEnv :: with_factory ( Cli :: command) . complete ( ) ;
27862786
2787- let ( cli, explicit_gateway) = {
2787+ let ( cli, explicit_gateway, explicit_endpoint ) = {
27882788 let mut args = Cli :: command ( ) . get_matches ( ) ;
27892789 let explicit_gateway =
27902790 args. value_source ( "gateway" ) == Some ( clap:: parser:: ValueSource :: CommandLine ) ;
2791- if explicit_gateway
2792- && args. value_source ( "gateway_endpoint" ) == Some ( clap:: parser:: ValueSource :: EnvVariable )
2793- {
2794- args. remove_one :: < String > ( "gateway_endpoint" ) ;
2795- }
2791+ let explicit_endpoint =
2792+ args. value_source ( "gateway_endpoint" ) == Some ( clap:: parser:: ValueSource :: CommandLine ) ;
27962793 (
27972794 Cli :: from_arg_matches_mut ( & mut args) . unwrap_or_else ( |err| err. exit ( ) ) ,
27982795 explicit_gateway,
2796+ explicit_endpoint,
27992797 )
28002798 } ;
2799+ let current_gateway = || {
2800+ if let ( Some ( name) , Some ( endpoint) ) = ( & cli. gateway , & cli. gateway_endpoint )
2801+ && explicit_gateway != explicit_endpoint
2802+ && !get_gateway_metadata ( name) . is_some_and ( |metadata| {
2803+ normalize_gateway_endpoint ( & metadata. gateway_endpoint )
2804+ == normalize_gateway_endpoint ( endpoint)
2805+ } )
2806+ {
2807+ return resolve_gateway ( & None , & cli. gateway_endpoint ) ;
2808+ }
2809+ resolve_gateway ( & cli. gateway , & cli. gateway_endpoint )
2810+ } ;
28012811
28022812 // Resolve colorization before anything is printed, so no command can emit
28032813 // escape sequences into a pipe.
@@ -2902,7 +2912,7 @@ async fn run_async() -> Result<()> {
29022912 run:: gateway_select ( name. as_deref ( ) , & cli. gateway ) ?;
29032913 }
29042914 GatewayCommands :: Info { output } => {
2905- if let Ok ( ctx) = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) {
2915+ if let Ok ( ctx) = current_gateway ( ) {
29062916 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
29072917 apply_auth ( & mut tls, & ctx. name ) ?;
29082918 run:: gateway_info ( & ctx. name , & ctx. endpoint , & tls, output. as_str ( ) ) . await ?;
@@ -2938,7 +2948,7 @@ async fn run_async() -> Result<()> {
29382948 // -----------------------------------------------------------
29392949 Some ( Commands :: Status { output } ) => {
29402950 // A gateway named on the command line must be registered.
2941- let resolved = match resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) {
2951+ let resolved = match current_gateway ( ) {
29422952 Err ( err) if cli. gateway . is_some ( ) => return Err ( err) ,
29432953 resolved => resolved,
29442954 } ;
@@ -2975,7 +2985,7 @@ async fn run_async() -> Result<()> {
29752985 // Top-level current identity
29762986 // -----------------------------------------------------------
29772987 Some ( Commands :: Whoami { output } ) => {
2978- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
2988+ let ctx = current_gateway ( ) ?;
29792989 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
29802990 apply_auth ( & mut tls, & ctx. name ) ?;
29812991 run:: whoami ( & ctx. endpoint , & tls, output. as_str ( ) ) . await ?;
@@ -3091,7 +3101,7 @@ async fn run_async() -> Result<()> {
30913101 target_host,
30923102 local,
30933103 } => {
3094- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3104+ let ctx = current_gateway ( ) ?;
30953105 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
30963106 apply_auth ( & mut tls, & ctx. name ) ?;
30973107 let name = resolve_sandbox_name ( name, & ctx. name , & cli. workspace ) ?;
@@ -3113,7 +3123,7 @@ async fn run_async() -> Result<()> {
31133123 background,
31143124 } => {
31153125 let spec = openshell_core:: forward:: ForwardSpec :: parse ( & port) ?;
3116- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3126+ let ctx = current_gateway ( ) ?;
31173127 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
31183128 apply_auth ( & mut tls, & ctx. name ) ?;
31193129 let name = resolve_sandbox_name ( name, & ctx. name , & cli. workspace ) ?;
@@ -3144,7 +3154,7 @@ async fn run_async() -> Result<()> {
31443154 Some ( Commands :: Service {
31453155 command : Some ( command) ,
31463156 } ) => {
3147- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3157+ let ctx = current_gateway ( ) ?;
31483158 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
31493159 apply_auth ( & mut tls, & ctx. name ) ?;
31503160 match command {
@@ -3208,7 +3218,7 @@ async fn run_async() -> Result<()> {
32083218 source,
32093219 level,
32103220 } ) => {
3211- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3221+ let ctx = current_gateway ( ) ?;
32123222 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
32133223 apply_auth ( & mut tls, & ctx. name ) ?;
32143224 let name = resolve_sandbox_name ( name, & ctx. name , & cli. workspace ) ?;
@@ -3235,7 +3245,7 @@ async fn run_async() -> Result<()> {
32353245 Some ( Commands :: Policy {
32363246 command : Some ( policy_cmd) ,
32373247 } ) => {
3238- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3248+ let ctx = current_gateway ( ) ?;
32393249 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
32403250 apply_auth ( & mut tls, & ctx. name ) ?;
32413251 match policy_cmd {
@@ -3397,7 +3407,7 @@ async fn run_async() -> Result<()> {
33973407 Some ( Commands :: Settings {
33983408 command : Some ( settings_cmd) ,
33993409 } ) => {
3400- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3410+ let ctx = current_gateway ( ) ?;
34013411 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
34023412 apply_auth ( & mut tls, & ctx. name ) ?;
34033413
@@ -3476,7 +3486,7 @@ async fn run_async() -> Result<()> {
34763486 Some ( Commands :: Rule {
34773487 command : Some ( draft_cmd) ,
34783488 } ) => {
3479- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3489+ let ctx = current_gateway ( ) ?;
34803490 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
34813491 apply_auth ( & mut tls, & ctx. name ) ?;
34823492 match draft_cmd {
@@ -3635,7 +3645,7 @@ async fn run_async() -> Result<()> {
36353645 || expose. is_some ( ) ;
36363646 let gpu_requirements: Option < GpuResourceRequirements > = gpu. map ( Into :: into) ;
36373647
3638- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3648+ let ctx = current_gateway ( ) ?;
36393649 let endpoint = & ctx. endpoint ;
36403650 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
36413651 apply_auth ( & mut tls, & ctx. name ) ?;
@@ -3685,7 +3695,7 @@ async fn run_async() -> Result<()> {
36853695 dest,
36863696 no_git_ignore,
36873697 } => {
3688- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3698+ let ctx = current_gateway ( ) ?;
36893699 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
36903700 apply_auth ( & mut tls, & ctx. name ) ?;
36913701 let local = std:: path:: Path :: new ( & local_path) ;
@@ -3705,7 +3715,7 @@ async fn run_async() -> Result<()> {
37053715 sandbox_path,
37063716 dest,
37073717 } => {
3708- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3718+ let ctx = current_gateway ( ) ?;
37093719 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
37103720 apply_auth ( & mut tls, & ctx. name ) ?;
37113721 let local_dest = dest. as_deref ( ) . unwrap_or ( "." ) ;
@@ -3722,7 +3732,7 @@ async fn run_async() -> Result<()> {
37223732 eprintln ! ( "{} Download complete" , "✓" . green( ) . bold( ) ) ;
37233733 }
37243734 other => {
3725- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
3735+ let ctx = current_gateway ( ) ?;
37263736 let endpoint = & ctx. endpoint ;
37273737 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
37283738 apply_auth ( & mut tls, & ctx. name ) ?;
@@ -4025,7 +4035,7 @@ async fn run_async() -> Result<()> {
40254035 Some ( Commands :: Workspace {
40264036 command : Some ( command) ,
40274037 } ) => {
4028- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
4038+ let ctx = current_gateway ( ) ?;
40294039 let endpoint = & ctx. endpoint ;
40304040 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
40314041 apply_auth ( & mut tls, & ctx. name ) ?;
@@ -4095,7 +4105,7 @@ async fn run_async() -> Result<()> {
40954105 Some ( Commands :: Provider {
40964106 command : Some ( command) ,
40974107 } ) => {
4098- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
4108+ let ctx = current_gateway ( ) ?;
40994109 let endpoint = & ctx. endpoint ;
41004110 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
41014111 apply_auth ( & mut tls, & ctx. name ) ?;
@@ -4289,14 +4299,14 @@ async fn run_async() -> Result<()> {
42894299 Some ( Commands :: Profile {
42904300 command : Some ( command) ,
42914301 } ) => {
4292- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
4302+ let ctx = current_gateway ( ) ?;
42934303 let endpoint = & ctx. endpoint ;
42944304 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
42954305 apply_auth ( & mut tls, & ctx. name ) ?;
42964306 command. run ( endpoint, & cli. workspace , & tls) . await ?;
42974307 }
42984308 Some ( Commands :: Term { theme } ) => {
4299- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
4309+ let ctx = current_gateway ( ) ?;
43004310 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
43014311 apply_auth ( & mut tls, & ctx. name ) ?;
43024312 let channel = openshell_cli:: tls:: build_channel ( & ctx. endpoint , & tls) . await ?;
@@ -4341,21 +4351,11 @@ async fn run_async() -> Result<()> {
43414351 } else {
43424352 // A saved profile resolves its own endpoint. A local --server
43434353 // can explicitly pair an endpoint with the chosen profile.
4344- let ( profile, endpoint) = if server. is_some ( ) {
4345- (
4346- gateway_name. or_else ( || cli. gateway . filter ( |_| explicit_gateway) ) ,
4347- server,
4348- )
4349- } else if gateway_name. is_some ( ) {
4350- ( gateway_name, None )
4351- } else if cli. gateway_endpoint . is_some ( ) {
4352- (
4353- cli. gateway . filter ( |_| explicit_gateway) ,
4354- cli. gateway_endpoint ,
4355- )
4356- } else {
4357- ( cli. gateway , cli. gateway_endpoint )
4358- } ;
4354+ let use_global_endpoint =
4355+ gateway_name. is_none ( ) && ( !explicit_gateway || explicit_endpoint) ;
4356+ let endpoint =
4357+ server. or_else ( || cli. gateway_endpoint . filter ( |_| use_global_endpoint) ) ;
4358+ let profile = gateway_name. or_else ( || cli. gateway . filter ( |_| explicit_gateway) ) ;
43594359 let ctx = resolve_gateway ( & profile, & endpoint) ?;
43604360 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
43614361 apply_auth ( & mut tls, & ctx. name ) ?;
@@ -4402,7 +4402,7 @@ async fn run_async() -> Result<()> {
44024402 Some ( Commands :: Secret {
44034403 command : Some ( command) ,
44044404 } ) => {
4405- let ctx = resolve_gateway ( & cli . gateway , & cli . gateway_endpoint ) ?;
4405+ let ctx = current_gateway ( ) ?;
44064406 let endpoint = & ctx. endpoint ;
44074407 let mut tls = tls. with_gateway_name ( & ctx. name ) ;
44084408 apply_auth ( & mut tls, & ctx. name ) ?;
0 commit comments