From 834cce64bc79954dd3d3e940ccee591ea7d3f43f Mon Sep 17 00:00:00 2001 From: Sebastian Geisler Date: Sun, 7 Jan 2018 02:26:18 +0100 Subject: [PATCH] parse config file to find default remote If no --remote is specified the .cargo-remote.toml config file will be consulted to find a remote build server. If none is found an error is thrown. --- .gitignore | 5 ++++- Cargo.toml | 3 ++- src/main.rs | 35 ++++++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 3484d23..fdc3f30 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,7 @@ crashlytics-build.properties fabric.properties # project file -cargo-remote.iml \ No newline at end of file +cargo-remote.iml + +# test config file +.cargo-remote.toml diff --git a/Cargo.toml b/Cargo.toml index b7d4ffe..22ceaa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ structopt = "0.1.6" structopt-derive = "0.1.6" cargo_metadata = "0.4.0" log = "0.4.1" -simple_logger = "0.4.0" \ No newline at end of file +simple_logger = "0.4.0" +toml = "0.4.5" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2fed908..0ea8047 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,27 +7,26 @@ extern crate cargo_metadata; #[macro_use] extern crate log; extern crate simple_logger; +extern crate toml; + use std::process::{exit, Command, Stdio}; use std::ffi::OsString; use std::path::Path; +use std::fs::File; +use std::io::{BufReader, Read}; use structopt::StructOpt; +use toml::Value; + #[derive(StructOpt, Debug)] #[structopt(name = "cargo remote")] struct Opts { // workaround for "remote" argument when calling "cargo remote" _unused: String, - #[structopt(subcommand)] - command: Cmd, + command: String, #[structopt(short = "r", long = "remote", help = "remote ssh build server")] - remote: String -} - -#[derive(StructOpt, Debug)] -enum Cmd { - #[structopt(name="build", help = "Build cargo project remotely and copy back target folder")] - Build, + remote: Option } fn main() { @@ -46,12 +45,26 @@ fn main() { exit(-2); }, |project| { ( - Path::new(&project.manifest_path).parent().expect("Cargo.toml seems to have no parent directory?"), + Path::new(&project.manifest_path) + .parent() + .expect("Cargo.toml seems to have no parent directory?"), &project.name ) }); - let build_server = options.remote; + let build_server = options.remote.unwrap_or_else(|| { + let config_path = project_dir.join(".cargo-remote.toml"); + File::open(config_path).ok().and_then(|mut file| { + let mut config_file_string = "".to_owned(); + file.read_to_string(&mut config_file_string); + config_file_string.parse::().ok() + }).and_then(|value| { + value["remote"].as_str().map(str::to_owned) + }).unwrap_or_else(|| { + error!("No remote build server was defined (use config file or --remote flag)"); + exit(-3); + }) + }); match options.command { Build => {