@@ -6,6 +6,7 @@ use clap::{Parser, Subcommand};
66use reqwest:: Client ;
77
88use log:: * ;
9+ use std:: io:: Write ;
910use std:: process:: { Child , Command , Stdio } ;
1011use std:: string:: String ;
1112
@@ -137,15 +138,23 @@ async fn main() {
137138 }
138139}
139140
140- fn try_copy_clipboard ( ) -> Result < ( ) > {
141- let command = Command :: new ( "wl-copy" ) . stdin ( Stdio :: piped ( ) ) ;
141+ fn copy_clipboard ( data : String ) -> Result < ( ) , String > {
142+ let mut command = Command :: new ( "wl-copy" ) ;
143+ command. stdin ( Stdio :: piped ( ) ) ;
142144
143- let mut stdin = command. stdin . take ( ) . unwrap ( ) ;
144- stdin. drop ( stdin) ;
145+ let mut process = command. spawn ( ) . map_err ( |err| err. to_string ( ) ) ?;
146+ let mut stdin = process. stdin . take ( ) . unwrap ( ) ;
147+ stdin. write ( data. as_bytes ( ) ) . unwrap ( ) ;
145148
146- let status = command. status ( ) ?;
149+ let status = process. wait ( ) . map_err ( |err| err. to_string ( ) ) ?;
150+ if !status. success ( ) {
151+ return Err ( format ! (
152+ "wl-copy exited with status code: {}" ,
153+ status. code( ) . unwrap( )
154+ ) ) ;
155+ }
147156
148- if !status . success ( ) { }
157+ Ok ( ( ) )
149158}
150159
151160async fn get_access_token_via_device_code ( client : & Client ) -> ghauth:: AccessToken {
@@ -176,7 +185,9 @@ async fn get_device_code(client: &Client) -> ghauth::DeviceCode {
176185 eprintln ! ( "gh-login: Go to the link below and enter in the device code" ) ;
177186 eprintln ! ( "{}" , device_code. verification_uri) ;
178187 eprintln ! ( "device code: {}" , device_code. user_code) ;
179- try_copy_clipboard ( device_code. user_code ) ;
188+ if let Some ( error) = copy_clipboard ( device_code. user_code . to_string ( ) ) . err ( ) {
189+ warn ! ( "Could not copy to clipboard: {}" , error) ;
190+ }
180191 device_code
181192}
182193
0 commit comments