@@ -26,35 +26,49 @@ use std::{fs, io::Write, path::Path, process::Command};
2626use tempfile:: { tempdir, TempDir } ;
2727use url:: Url ;
2828
29+ #[ derive( thiserror:: Error , Debug ) ]
30+ pub enum AutoyastError {
31+ #[ error( "I/O error: {0}" ) ]
32+ IO ( #[ from] std:: io:: Error ) ,
33+ #[ error( "Failed to run the agama-autoyast script: {0}" ) ]
34+ Execute ( #[ source] std:: io:: Error ) ,
35+ #[ error( "Failed to convert the AutoYaST profile: {0}" ) ]
36+ Evaluation ( String ) ,
37+ #[ error( "Unsupported AutoYaST format at {0}" ) ]
38+ UnsupportedFormat ( Url ) ,
39+ }
40+
2941/// Downloads and converts autoyast profile.
3042pub struct AutoyastProfileImporter {
3143 pub content : String ,
3244}
3345
3446impl AutoyastProfileImporter {
35- pub async fn read ( url : & Url ) -> anyhow :: Result < Self > {
47+ pub async fn read ( url : & Url ) -> Result < Self , AutoyastError > {
3648 let path = url. path ( ) ;
3749 if !path. ends_with ( ".xml" ) && !path. ends_with ( ".erb" ) && !path. ends_with ( '/' ) {
38- let msg = format ! ( "Unsupported AutoYaST format at {}" , url) ;
39- return Err ( anyhow:: Error :: msg ( msg) ) ;
50+ return Err ( AutoyastError :: UnsupportedFormat ( url. clone ( ) ) ) ;
4051 }
4152
4253 const TMP_DIR_PREFIX : & str = "autoyast" ;
4354 const AUTOINST_JSON : & str = "autoinst.json" ;
4455
4556 let tmp_dir = TempDir :: with_prefix ( TMP_DIR_PREFIX ) ?;
46- tokio:: process:: Command :: new ( "agama-autoyast" )
57+ let result = tokio:: process:: Command :: new ( "agama-autoyast" )
4758 . env ( "YAST_SKIP_PROFILE_FETCH_ERROR" , "1" )
4859 . args ( [ url. as_str ( ) , & tmp_dir. path ( ) . to_string_lossy ( ) ] )
49- . status ( )
60+ . output ( )
5061 . await
51- . context ( "Failed to run agama-autoyast" ) ?;
62+ . map_err ( AutoyastError :: Execute ) ?;
63+
64+ if !result. status . success ( ) {
65+ return Err ( AutoyastError :: Evaluation (
66+ String :: from_utf8_lossy ( & result. stderr ) . to_string ( ) ,
67+ ) ) ;
68+ }
5269
5370 let autoinst_json = tmp_dir. path ( ) . join ( AUTOINST_JSON ) ;
54- let content = fs:: read_to_string ( & autoinst_json) . context ( format ! (
55- "agama-autoyast did not produce {:?}" ,
56- autoinst_json
57- ) ) ?;
71+ let content = fs:: read_to_string ( & autoinst_json) ?;
5872 Ok ( Self { content } )
5973 }
6074}
0 commit comments