@@ -11,81 +11,68 @@ mod update;
1111mod util;
1212
1313use crate :: io:: { Io , PrintColor } ;
14+ use anyhow:: Context ;
1415use util:: { Client , ClientProduction } ;
1516
16- pub fn handle ( matches : & clap:: ArgMatches , io : & mut dyn Io ) {
17- let mut client = ClientProduction :: new ( matches. is_present ( "testmode" ) ) ;
17+ pub fn handle ( matches : & clap:: ArgMatches , io : & mut dyn Io ) -> anyhow :: Result < ( ) > {
18+ let mut client = ClientProduction :: new ( matches. is_present ( "testmode" ) ) ? ;
1819
1920 // Authorize the client and raise error if not logged in when required
2021 match matches. subcommand ( ) {
2122 Some ( ( "login" , _) ) => {
2223 if client. load_login ( ) . is_ok ( ) {
23- io. println (
24- "Already logged in. Please logout first with 'tmc logout'" ,
25- PrintColor :: Failed ,
26- ) ;
27- return ;
24+ anyhow:: bail!( "Already logged in. Please logout first with 'tmc logout'" , ) ;
2825 }
2926 }
3027 Some ( ( "test" , _) ) => ( ) ,
3128 _ => {
32- if client. load_login ( ) . is_err ( ) {
33- io. println (
34- "No login found. Login to use this command with 'tmc login'" ,
35- PrintColor :: Failed ,
36- ) ;
37- return ;
38- }
29+ client
30+ . load_login ( )
31+ . context ( "No login found. Login to use this command with 'tmc login'" ) ?;
3932 }
4033 } ;
4134
4235 // Check that organization is set
4336 if let Some ( ( "download" | "courses" , _) ) = matches. subcommand ( ) {
44- if util:: get_organization ( ) . is_none ( ) {
45- io. println (
46- "No organization found. Run 'tmc organization' first." ,
47- PrintColor :: Failed ,
48- ) ;
49- return ;
50- }
37+ util:: get_organization ( ) . context ( "No organization found. Run 'tmc organization' first." ) ?;
5138 } ;
5239
5340 match matches. subcommand ( ) {
5441 Some ( ( "login" , args) ) => {
5542 let interactive_mode = !args. is_present ( "non-interactive" ) ;
56- login:: login ( io, & mut client, interactive_mode)
43+ login:: login ( io, & mut client, interactive_mode) ? ;
5744 }
5845 Some ( ( "download" , args) ) => download:: download_or_update (
5946 io,
6047 & mut client,
6148 args. value_of ( "course" ) ,
6249 args. is_present ( "currentdir" ) ,
63- ) ,
50+ ) ? ,
6451 Some ( ( "update" , args) ) => {
65- update:: update ( io, & mut client, args. is_present ( "currentdir" ) ) ;
52+ update:: update ( io, & mut client, args. is_present ( "currentdir" ) ) ? ;
6653 }
6754 Some ( ( "organization" , args) ) => {
6855 let interactive_mode = !args. is_present ( "non-interactive" ) ;
69- organization:: organization ( io, & mut client, interactive_mode)
56+ organization:: organization ( io, & mut client, interactive_mode) ?
7057 }
71- Some ( ( "courses" , _) ) => courses:: list_courses ( io, & mut client) ,
58+ Some ( ( "courses" , _) ) => courses:: list_courses ( io, & mut client) ? ,
7259 Some ( ( "submit" , args) ) => {
73- submit:: submit ( io, & mut client, args. value_of ( "exercise" ) ) ;
60+ submit:: submit ( io, & mut client, args. value_of ( "exercise" ) ) ? ;
7461 }
7562 Some ( ( "exercises" , args) ) => {
7663 if let Some ( c) = args. value_of ( "course" ) {
77- exercises:: list_exercises ( io, & mut client, c) ;
64+ exercises:: list_exercises ( io, & mut client, c) ? ;
7865 } else {
79- io. println ( "argument for course not found" , PrintColor :: Normal ) ;
66+ io. println ( "argument for course not found" , PrintColor :: Normal ) ? ;
8067 }
8168 }
8269 Some ( ( "test" , args) ) => {
83- test:: test ( io, args. value_of ( "exercise" ) ) ;
70+ test:: test ( io, args. value_of ( "exercise" ) ) ? ;
8471 }
8572 Some ( ( "paste" , args) ) => {
86- paste:: paste ( io, & mut client, args. value_of ( "exercise" ) ) ;
73+ paste:: paste ( io, & mut client, args. value_of ( "exercise" ) ) ? ;
8774 }
88- Some ( ( "logout" , _) ) => logout:: logout ( io, & mut client) ,
75+ Some ( ( "logout" , _) ) => logout:: logout ( io, & mut client) ? ,
8976 Some ( ( "fetchupdate" , _) ) => {
9077 #[ cfg( target_os = "windows" ) ]
9178 crate :: updater:: process_update ( ) ;
@@ -95,11 +82,12 @@ pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
9582 crate :: updater:: cleartemp ( ) . unwrap ( ) ;
9683 }
9784 Some ( ( "elevateddownload" , _) ) => {
98- download:: elevated_download ( io, & mut client) ;
85+ download:: elevated_download ( io, & mut client) ? ;
9986 }
10087 Some ( ( "elevatedupdate" , _) ) => {
101- update:: elevated_update ( io, & mut client) ;
88+ update:: elevated_update ( io, & mut client) ? ;
10289 }
10390 _ => ( ) , // Unknown subcommand or no subcommand was given
10491 }
92+ Ok ( ( ) )
10593}
0 commit comments