@@ -20,7 +20,6 @@ use verify::verify_command;
2020
2121use failure:: Error ;
2222use quicli:: prelude:: * ;
23- use std:: path;
2423use structopt:: StructOpt ;
2524
2625fn main ( ) -> CliResult {
@@ -41,16 +40,12 @@ fn main() -> CliResult {
4140fn run_floki_from_args ( args : & Cli ) -> Result < ( ) , Error > {
4241 debug ! ( "Got command line arguments: {:?}" , & args) ;
4342
44- let environ = environment:: Environment :: gather ( ) ?;
43+ let environ = environment:: Environment :: gather ( & args . config_file ) ?;
4544 debug ! ( "Got environment {:?}" , & environ) ;
4645
47- let ( floki_root, config_file) = match & args. config_file {
48- Some ( config_file) => ( environ. current_directory . clone ( ) , config_file. clone ( ) ) ,
49- None => locate_file_in_system ( find_floki_yaml ( & environ. current_directory ) ?) ?,
50- } ;
51- debug ! ( "Selected configuration file: {:?}" , & config_file) ;
46+ debug ! ( "Selected configuration file: {:?}" , & environ. config_file) ;
5247
53- let config = FlokiConfig :: from_file ( & config_file) ?;
48+ let config = FlokiConfig :: from_file ( & environ . config_file ) ?;
5449 verify_command ( args. local , & config) ?;
5550
5651 // Dispatch appropriate subcommand
@@ -65,93 +60,25 @@ fn run_floki_from_args(args: &Cli) -> Result<(), Error> {
6560 // Run a command in the floki container
6661 Some ( Subcommand :: Run { command } ) => {
6762 let inner_command = interpret:: command_in_shell ( config. shell . inner_shell ( ) , & command) ;
68- run_floki_container ( & environ, & floki_root , & config, inner_command)
63+ run_floki_container ( & environ, & config, inner_command)
6964 }
7065
7166 // Launch an interactive floki shell (the default)
7267 None => {
7368 let inner_command = config. shell . inner_shell ( ) . to_string ( ) ;
74- run_floki_container ( & environ, & floki_root , & config, inner_command)
69+ run_floki_container ( & environ, & config, inner_command)
7570 }
7671 }
7772}
7873
7974/// Launch a floki container running the inner command
8075fn run_floki_container (
8176 environ : & environment:: Environment ,
82- floki_root : & path:: Path ,
8377 config : & FlokiConfig ,
8478 inner_command : String ,
8579) -> Result < ( ) , Error > {
8680 config. image . obtain_image ( ) ?;
87-
8881 let subshell_command = command:: subshell_command ( & config. init , inner_command) ;
8982 debug ! ( "Running container with command '{}'" , & subshell_command) ;
90- interpret:: run_container ( & environ, & floki_root, & config, & subshell_command)
91- }
92-
93- /// Search all ancestors of the current directory for a floki.yaml file name.
94- fn find_floki_yaml ( current_directory : & path:: Path ) -> Result < path:: PathBuf , Error > {
95- current_directory
96- . ancestors ( )
97- . map ( |a| a. join ( "floki.yaml" ) )
98- . find ( |f| f. is_file ( ) )
99- . ok_or ( errors:: FlokiError :: ProblemFindingConfigYaml { } . into ( ) )
100- }
101-
102- /// Take a file path, and return a tuple consisting of it's directory and the file path
103- fn locate_file_in_system ( path : path:: PathBuf ) -> Result < ( path:: PathBuf , path:: PathBuf ) , Error > {
104- let dir = path
105- . parent ( )
106- . ok_or_else ( || errors:: FlokiInternalError :: InternalAssertionFailed {
107- description : format ! ( "config_file '{:?}' does not have a parent" , & path) ,
108- } ) ?
109- . to_path_buf ( ) ;
110- Ok ( ( dir, path) )
111- }
112-
113- #[ cfg( test) ]
114- mod test {
115- use super :: * ;
116- use std:: fs;
117- use tempdir;
118-
119- fn touch_file ( path : & path:: Path ) -> Result < ( ) , Error > {
120- fs:: create_dir_all (
121- path. parent ( )
122- . ok_or ( format_err ! ( "Unable to take parent of path" ) ) ?,
123- ) ?;
124- fs:: OpenOptions :: new ( ) . create ( true ) . write ( true ) . open ( path) ?;
125- Ok ( ( ) )
126- }
127-
128- #[ test]
129- fn test_find_floki_yaml_current_dir ( ) -> Result < ( ) , Error > {
130- let tmp_dir = tempdir:: TempDir :: new ( "" ) ?;
131- let floki_yaml_path = tmp_dir. path ( ) . join ( "floki.yaml" ) ;
132- touch_file ( & floki_yaml_path) ?;
133- assert_eq ! ( find_floki_yaml( & tmp_dir. path( ) ) ?, floki_yaml_path) ;
134- Ok ( ( ) )
135- }
136-
137- #[ test]
138- fn test_find_floki_yaml_ancestor ( ) -> Result < ( ) , Error > {
139- let tmp_dir = tempdir:: TempDir :: new ( "" ) ?;
140- let floki_yaml_path = tmp_dir. path ( ) . join ( "floki.yaml" ) ;
141- touch_file ( & floki_yaml_path) ?;
142- assert_eq ! (
143- find_floki_yaml( & tmp_dir. path( ) . join( "dir/subdir" ) ) ?,
144- floki_yaml_path
145- ) ;
146- Ok ( ( ) )
147- }
148-
149- #[ test]
150- fn test_find_floki_yaml_sibling ( ) -> Result < ( ) , Error > {
151- let tmp_dir = tempdir:: TempDir :: new ( "" ) ?;
152- let floki_yaml_path = tmp_dir. path ( ) . join ( "src/floki.yaml" ) ;
153- touch_file ( & floki_yaml_path) ?;
154- assert ! ( find_floki_yaml( & tmp_dir. path( ) . join( "include" ) ) . is_err( ) ) ;
155- Ok ( ( ) )
156- }
83+ interpret:: run_container ( & environ, & config, & subshell_command)
15784}
0 commit comments