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.
pull/1/head
Sebastian Geisler 6 years ago
parent b09ee0d93e
commit 834cce64bc

5
.gitignore vendored

@ -58,4 +58,7 @@ crashlytics-build.properties
fabric.properties fabric.properties
# project file # project file
cargo-remote.iml cargo-remote.iml
# test config file
.cargo-remote.toml

@ -8,4 +8,5 @@ structopt = "0.1.6"
structopt-derive = "0.1.6" structopt-derive = "0.1.6"
cargo_metadata = "0.4.0" cargo_metadata = "0.4.0"
log = "0.4.1" log = "0.4.1"
simple_logger = "0.4.0" simple_logger = "0.4.0"
toml = "0.4.5"

@ -7,27 +7,26 @@ extern crate cargo_metadata;
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate simple_logger; extern crate simple_logger;
extern crate toml;
use std::process::{exit, Command, Stdio}; use std::process::{exit, Command, Stdio};
use std::ffi::OsString; use std::ffi::OsString;
use std::path::Path; use std::path::Path;
use std::fs::File;
use std::io::{BufReader, Read};
use structopt::StructOpt; use structopt::StructOpt;
use toml::Value;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
#[structopt(name = "cargo remote")] #[structopt(name = "cargo remote")]
struct Opts { struct Opts {
// workaround for "remote" argument when calling "cargo remote" // workaround for "remote" argument when calling "cargo remote"
_unused: String, _unused: String,
#[structopt(subcommand)] command: String,
command: Cmd,
#[structopt(short = "r", long = "remote", help = "remote ssh build server")] #[structopt(short = "r", long = "remote", help = "remote ssh build server")]
remote: String remote: Option<String>
}
#[derive(StructOpt, Debug)]
enum Cmd {
#[structopt(name="build", help = "Build cargo project remotely and copy back target folder")]
Build,
} }
fn main() { fn main() {
@ -46,12 +45,26 @@ fn main() {
exit(-2); exit(-2);
}, |project| { }, |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 &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::<Value>().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 { match options.command {
Build => { Build => {

Loading…
Cancel
Save