@@ -51,7 +51,7 @@ use strum::{EnumMessage, VariantArray};
5151use tokio:: io:: { AsyncBufReadExt , BufReader } ;
5252use tokio:: time:: { sleep, Duration } ;
5353use tokio_tungstenite:: tungstenite:: Message ;
54- use tracing:: { debug, error, info, trace} ;
54+ use tracing:: { debug, error, info, trace, warn } ;
5555use tracing_subscriber:: { fmt, prelude:: * , registry, EnvFilter } ;
5656use zip:: write:: FileOptions ;
5757
@@ -637,6 +637,8 @@ impl Shuttle {
637637 }
638638 }
639639
640+ /// Ensures a project id is known, either by explicit --id/--name args or config file(s)
641+ /// or by asking user to link the project folder.
640642 pub async fn load_project (
641643 & mut self ,
642644 project_args : & ProjectArgs ,
@@ -646,30 +648,37 @@ impl Shuttle {
646648 trace ! ( "project arguments: {project_args:?}" ) ;
647649
648650 self . ctx . load_local_config ( project_args) ?;
649- // load project id from file if exists
651+ // load project id from args if given or from file if exists
650652 self . ctx . load_local_internal_config ( project_args) ?;
651653
652654 if let Some ( id) = project_args. id . as_ref ( ) {
653- // ensure ULID part is uppercase
654- if let Some ( suffix) = id. strip_prefix ( "proj_" ) {
655- // Soft (dumb) validation of ULID format in the id (ULIDs are 26 chars)
656- if suffix. len ( ) == 26 {
657- let proj_id_uppercase = format ! ( "proj_{}" , suffix. to_ascii_uppercase( ) ) ;
658- if * id != proj_id_uppercase {
659- eprintln ! ( "INFO: Converted project id to '{}'" , proj_id_uppercase) ;
660- self . ctx . set_project_id ( proj_id_uppercase) ;
661- }
655+ // Validate format of explicitly given project id and change the ULID to uppercase if it is lowercase
656+ if let Some ( proj_id_uppercase) = id. strip_prefix ( "proj_" ) . and_then ( |suffix| {
657+ // Soft (dumb) validation of ULID format (ULIDs are 26 chars)
658+ ( suffix. len ( ) == 26 ) . then_some ( format ! ( "proj_{}" , suffix. to_ascii_uppercase( ) ) )
659+ } ) {
660+ if * id != proj_id_uppercase {
661+ eprintln ! ( "INFO: Converted project id to '{}'" , proj_id_uppercase) ;
662+ self . ctx . set_project_id ( proj_id_uppercase) ;
662663 }
664+ } else {
665+ // TODO: eprintln a warning?
666+ warn ! ( "project id with bad format detected: '{id}'" ) ;
663667 }
664- // if linking, save and return
668+
669+ // if linking, save config
665670 if do_linking {
666671 eprintln ! ( "Linking to project {}" , self . ctx. project_id( ) ) ;
667672 self . ctx . save_local_internal ( ) ?;
668- return Ok ( ( ) ) ;
669673 }
670674 }
675+ if self . ctx . project_id_found ( ) {
676+ // --id takes prio over --name, and at this point we know the id, so we're done
677+ return Ok ( ( ) ) ;
678+ }
679+
680+ // translate project name to project id if a name was given
671681 if let Some ( name) = project_args. name . as_ref ( ) {
672- // translate project name to project id if a name was given
673682 let client = self . client . as_ref ( ) . unwrap ( ) ;
674683 trace ! ( %name, "looking up project id from project name" ) ;
675684 if let Some ( proj) = client
@@ -692,16 +701,33 @@ impl Shuttle {
692701 self . ctx . set_project_id ( proj. id ) ;
693702 }
694703 }
695- // if linking and project id known at this point, save and return
696- if do_linking && self . ctx . project_id_found ( ) {
704+ }
705+
706+ match ( self . ctx . project_id_found ( ) , do_linking) {
707+ // if project id is known and we are linking, save config
708+ ( true , true ) => {
697709 eprintln ! ( "Linking to project {}" , self . ctx. project_id( ) ) ;
698710 self . ctx . save_local_internal ( ) ?;
699- return Ok ( ( ) ) ;
700711 }
701- }
702- // if project id is still not known or an explicit linking is wanted, start the linking prompt
703- if !self . ctx . project_id_found ( ) || do_linking {
704- self . project_link_interactive ( ) . await ?;
712+ // if project id is known, we are done and nothing more to do
713+ ( true , false ) => ( ) ,
714+ // we still don't know the project id but want to link
715+ ( false , true ) => {
716+ self . project_link_interactive ( ) . await ?;
717+ }
718+ // we still don't know the project id
719+ ( false , false ) => {
720+ // if a name was given but no project was found, (incorrectly) set the id to the name so that an api error will be encountered
721+ // TODO: return an error here instead of letting an api error happen?
722+ if let Some ( name) = project_args. name . as_ref ( ) {
723+ warn ! ( "using project name as the id" ) ;
724+ self . ctx . set_project_id ( name. clone ( ) ) ;
725+ } else {
726+ // otherwise, we have to do an explicit linking
727+ trace ! ( "no project id found" ) ;
728+ self . project_link_interactive ( ) . await ?;
729+ }
730+ }
705731 }
706732
707733 Ok ( ( ) )
0 commit comments