11use tabled:: Table ;
22use colored:: Colorize ;
3- use std:: io;
3+ use std:: io:: { self , BufRead , Write } ;
44use std:: process:: Command ;
55use crate :: cli:: ClusterId ;
66use crate :: client:: CanineClient ;
@@ -41,12 +41,41 @@ pub async fn handle_list(client: &CanineClient) -> Result<(), Box<dyn std::error
4141 Ok ( ( ) )
4242}
4343
44+ fn select_cluster_name ( clusters : & [ crate :: client:: Cluster ] ) -> Result < String , Box < dyn std:: error:: Error > > {
45+ if clusters. is_empty ( ) {
46+ return Err ( "No clusters found" . into ( ) ) ;
47+ }
48+ println ! ( "\n Select a cluster:" ) ;
49+ for ( i, c) in clusters. iter ( ) . enumerate ( ) {
50+ println ! ( " [{}] {} ({})" , ( i + 1 ) . to_string( ) . cyan( ) , c. name, format!( "{}" , c. status) . dimmed( ) ) ;
51+ }
52+ print ! ( "\n {} " , "Enter number:" . bold( ) ) ;
53+ io:: stdout ( ) . flush ( ) . unwrap ( ) ;
54+ let mut input = String :: new ( ) ;
55+ io:: stdin ( ) . lock ( ) . read_line ( & mut input) ?;
56+ let choice: usize = input. trim ( ) . parse ( ) . map_err ( |_| "Invalid selection" ) ?;
57+ if choice < 1 || choice > clusters. len ( ) {
58+ return Err ( format ! ( "Selection out of range (1-{})" , clusters. len( ) ) . into ( ) ) ;
59+ }
60+ Ok ( clusters[ choice - 1 ] . name . clone ( ) )
61+ }
62+
63+ async fn resolve_cluster_name ( client : & CanineClient , id : & ClusterId ) -> Result < String , Box < dyn std:: error:: Error > > {
64+ if let Some ( ref name) = id. cluster {
65+ Ok ( name. clone ( ) )
66+ } else {
67+ let clusters = client. get_clusters ( ) . await ?. clusters ;
68+ select_cluster_name ( & clusters)
69+ }
70+ }
71+
4472pub async fn handle_download_kubeconfig (
4573 config : & CanineConfig ,
4674 client : & CanineClient ,
4775 id : & ClusterId ,
4876) -> Result < ( ) , Box < dyn std:: error:: Error > > {
49- let kubeconfig = client. download_kubeconfig_file ( & id. cluster ) . await ?;
77+ let cluster_name = resolve_cluster_name ( client, id) . await ?;
78+ let kubeconfig = client. download_kubeconfig_file ( & cluster_name) . await ?;
5079 let yaml = kubeconfig_to_yaml ( & kubeconfig. kubeconfig ) ?;
5180 config. save_kubeconfig ( yaml) ?;
5281 Ok ( ( ) )
@@ -66,7 +95,8 @@ pub async fn handle_connect(
6695 std:: process:: exit ( 1 ) ;
6796 }
6897 println ! ( "{} telepresence found" , "✓" . green( ) ) ;
69- let kubeconfig = client. download_kubeconfig_file ( & id. cluster ) . await ?;
98+ let cluster_name = resolve_cluster_name ( client, id) . await ?;
99+ let kubeconfig = client. download_kubeconfig_file ( & cluster_name) . await ?;
70100 let yaml = kubeconfig_to_yaml ( & kubeconfig. kubeconfig ) ?;
71101 config. save_kubeconfig ( yaml) ?;
72102
0 commit comments